Skip to content

Commit 61001ff

Browse files
committed
fix(cli): honor XDG_CONFIG_HOME on Linux and clarify TOKSCALE_CONFIG_DIR docs
- Linux config resolution now flows through dirs::config_dir() instead of hardcoding $HOME/.config, so users with a custom XDG_CONFIG_HOME pointing the user config root elsewhere are no longer silently sent to the wrong directory. macOS still overrides dirs::config_dir() to reach $HOME/.config/tokscale, since dirs returns Application Support there and the docs already promised .config. - README env-var rows for TOKSCALE_CONFIG_DIR now mention star-cache.json alongside settings.json (the override controls every file under the config root, not just settings), soften 'absolute path' to 'absolute path recommended; relative paths resolve against the process CWD' matching what get_config_dir() actually accepts, and explicitly note the legacy macOS fallback is suppressed when the override is set. - New regression test linux_honors_xdg_config_home_when_set proves the XDG_CONFIG_HOME case. The existing Unix default test now also unsets XDG_CONFIG_HOME so it stays hermetic regardless of the host shell.
1 parent 6b893c0 commit 61001ff

5 files changed

Lines changed: 50 additions & 9 deletions

File tree

README.ja.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ Tokscaleは設定を`~/.config/tokscale/settings.json`に保存します:
499499
| 変数 | デフォルト | 説明 |
500500
|----------|---------|-------------|
501501
| `TOKSCALE_NATIVE_TIMEOUT_MS` | `300000`(5分) | `nativeTimeoutMs` 設定をオーバーライド |
502-
| `TOKSCALE_CONFIG_DIR` | unset | 設定ディレクトリ(`settings.json` の保存場所)をオーバーライドします。絶対パス。CI サンドボックスや非デフォルトの場所を固定したい場合に便利です。 |
502+
| `TOKSCALE_CONFIG_DIR` | unset | 設定ディレクトリ(`settings.json` `star-cache.json` の保存場所)をオーバーライドします。絶対パス推奨;相対パスはプロセス CWD を基準に解決されます。CI サンドボックスや非デフォルトの場所を固定したい場合に便利です。設定されている場合、tokscale は macOS のレガシーパス(`~/Library/Application Support/tokscale/`)にフォールバックしません|
503503

504504
```bash
505505
# 例:非常に大きなデータセット用にタイムアウトを増加

README.ko.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ Tokscale은 설정을 `~/.config/tokscale/settings.json`에 저장합니다:
498498
| 변수 | 기본값 | 설명 |
499499
|----------|---------|-------------|
500500
| `TOKSCALE_NATIVE_TIMEOUT_MS` | `300000` (5분) | `nativeTimeoutMs` 설정 오버라이드 |
501-
| `TOKSCALE_CONFIG_DIR` | unset | 설정 디렉토리(`settings.json` 위치)를 오버라이드합니다. 절대 경로. CI 샌드박스나 비기본 위치를 고정할 때 유용합니다. |
501+
| `TOKSCALE_CONFIG_DIR` | unset | 설정 디렉토리(`settings.json` `star-cache.json` 위치)를 오버라이드합니다. 절대 경로 권장; 상대 경로는 프로세스 CWD 기준으로 해석됩니다. CI 샌드박스나 비기본 위치를 고정할 때 유용합니다. 설정되면 tokscale은 macOS 레거시 경로(`~/Library/Application Support/tokscale/`)로 폴백하지 않습니다. |
502502

503503
```bash
504504
# 예시: 매우 큰 데이터셋에 대한 타임아웃 증가

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ Environment variables override config file values. For CI/CD or one-off use:
515515
|----------|---------|-------------|
516516
| `TOKSCALE_NATIVE_TIMEOUT_MS` | `300000` (5 min) | Overrides `nativeTimeoutMs` config |
517517
| `TOKSCALE_EXTRA_DIRS` | unset | One-off extra session roots as `client:/abs/path,client:/abs/path` |
518-
| `TOKSCALE_CONFIG_DIR` | unset | Overrides the config directory (where `settings.json` lives). Absolute path. Useful for CI sandboxes or pinning a non-default location. |
518+
| `TOKSCALE_CONFIG_DIR` | unset | Overrides the config directory (where `settings.json` and `star-cache.json` live). Absolute path recommended; relative paths resolve against the process CWD. Useful for CI sandboxes or pinning a non-default location. When set, tokscale will not fall back to the legacy macOS `~/Library/Application Support/tokscale/` path. |
519519

520520
```bash
521521
# Example: Increase timeout for very large datasets

README.zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ Tokscale 将设置存储在 `~/.config/tokscale/settings.json`:
499499
| 变量 | 默认值 | 描述 |
500500
|----------|---------|-------------|
501501
| `TOKSCALE_NATIVE_TIMEOUT_MS` | `300000`(5 分钟) | 覆盖 `nativeTimeoutMs` 配置 |
502-
| `TOKSCALE_CONFIG_DIR` | unset | 覆盖配置目录(`settings.json` 所在位置)。绝对路径。适用于 CI 沙箱或固定到非默认位置。 |
502+
| `TOKSCALE_CONFIG_DIR` | unset | 覆盖配置目录(`settings.json` `star-cache.json` 所在位置)。建议使用绝对路径;相对路径将基于进程 CWD 解析。适用于 CI 沙箱或固定到非默认位置。设置后,tokscale 不会回退到 macOS 旧路径(`~/Library/Application Support/tokscale/`|
503503

504504
```bash
505505
# 示例:为非常大的数据集增加超时时间

crates/tokscale-cli/src/paths.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ use std::path::PathBuf;
1313
/// Resolve the tokscale config dir, honoring `TOKSCALE_CONFIG_DIR` first.
1414
///
1515
/// Resolution order:
16-
/// 1. `TOKSCALE_CONFIG_DIR` (absolute path, taken verbatim).
17-
/// 2. macOS / Linux: `$HOME/.config/tokscale`.
18-
/// 3. Windows (and any other platform): `dirs::config_dir().join("tokscale")`.
19-
/// 4. Last-ditch fallback: `./.tokscale` so a missing HOME never panics.
16+
/// 1. `TOKSCALE_CONFIG_DIR` taken verbatim. Absolute paths are recommended;
17+
/// relative paths are accepted and resolved against the process CWD.
18+
/// 2. macOS: `$HOME/.config/tokscale` (overrides `dirs::config_dir()`,
19+
/// which would return `~/Library/Application Support/` and split state
20+
/// across two roots — see module docs).
21+
/// 3. Linux: `dirs::config_dir().join("tokscale")` so XDG_CONFIG_HOME is
22+
/// honored. Falls through to `$HOME/.config/tokscale` when neither
23+
/// `XDG_CONFIG_HOME` nor `HOME` resolve.
24+
/// 4. Windows (and any other platform): `dirs::config_dir().join("tokscale")`.
25+
/// 5. Last-ditch fallback: `./.tokscale` so a missing HOME never panics.
2026
pub fn get_config_dir() -> PathBuf {
2127
if let Ok(custom) = std::env::var("TOKSCALE_CONFIG_DIR") {
2228
return PathBuf::from(custom);
2329
}
2430

25-
#[cfg(any(target_os = "macos", target_os = "linux"))]
31+
#[cfg(target_os = "macos")]
2632
{
2733
if let Some(home) = dirs::home_dir() {
2834
return home.join(".config").join("tokscale");
@@ -93,8 +99,10 @@ mod tests {
9399
fn unix_default_is_dot_config_tokscale_under_home() {
94100
let prev_override = env::var_os("TOKSCALE_CONFIG_DIR");
95101
let prev_home = env::var_os("HOME");
102+
let prev_xdg = env::var_os("XDG_CONFIG_HOME");
96103
unsafe {
97104
env::remove_var("TOKSCALE_CONFIG_DIR");
105+
env::remove_var("XDG_CONFIG_HOME");
98106
env::set_var("HOME", "/tmp/tokscale-home-test");
99107
}
100108
assert_eq!(
@@ -110,6 +118,39 @@ mod tests {
110118
Some(v) => env::set_var("HOME", v),
111119
None => env::remove_var("HOME"),
112120
}
121+
match prev_xdg {
122+
Some(v) => env::set_var("XDG_CONFIG_HOME", v),
123+
None => env::remove_var("XDG_CONFIG_HOME"),
124+
}
125+
}
126+
}
127+
128+
#[test]
129+
#[serial]
130+
#[cfg(target_os = "linux")]
131+
fn linux_honors_xdg_config_home_when_set() {
132+
// XDG_CONFIG_HOME is the documented way to relocate the user
133+
// config directory on Linux. Hardcoding `$HOME/.config` would
134+
// silently break setups that point this elsewhere.
135+
let prev_override = env::var_os("TOKSCALE_CONFIG_DIR");
136+
let prev_xdg = env::var_os("XDG_CONFIG_HOME");
137+
unsafe {
138+
env::remove_var("TOKSCALE_CONFIG_DIR");
139+
env::set_var("XDG_CONFIG_HOME", "/tmp/tokscale-xdg-config");
140+
}
141+
assert_eq!(
142+
get_config_dir(),
143+
PathBuf::from("/tmp/tokscale-xdg-config/tokscale"),
144+
);
145+
unsafe {
146+
match prev_override {
147+
Some(v) => env::set_var("TOKSCALE_CONFIG_DIR", v),
148+
None => env::remove_var("TOKSCALE_CONFIG_DIR"),
149+
}
150+
match prev_xdg {
151+
Some(v) => env::set_var("XDG_CONFIG_HOME", v),
152+
None => env::remove_var("XDG_CONFIG_HOME"),
153+
}
113154
}
114155
}
115156

0 commit comments

Comments
 (0)