Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor(config): read system config from one fixed path per platform (#4169)
Worktrunk Bot501d0172742a
diff --git a/docs/src/content/docs/config.md b/docs/src/content/docs/config.mdindex 1fabb011c..61b300c8e 100644--- a/docs/src/content/docs/config.md+++ b/docs/src/content/docs/config.md@@ -665,7 +665,6 @@ $ WORKTRUNK_COMMIT__GENERATION__COMMAND="echo 'test: automated commit'" wt merge| `WORKTRUNK_CONFIG_PATH` | Override user config file location || `WORKTRUNK_SYSTEM_CONFIG_PATH` | Override system config file location || `WORKTRUNK_PROJECT_CONFIG_PATH` | Override project config file location (defaults to `.config/wt.toml`); relative paths resolve from the worktree root |-| `XDG_CONFIG_DIRS` | Colon-separated system config directories (default: `/etc/xdg`) || `WORKTRUNK_MAX_CONCURRENT_COMMANDS` | Max parallel git commands (default: 32). Lower if hitting file descriptor limits. || `WORKTRUNK_VERBOSE` | Verbosity level (`0`/`1`/`2`), like `-v`/`-vv` but applied everywhere — including shell completion, which no flag can reach || `RUST_LOG` | Logging directive (e.g. `worktrunk=debug`); overrides the verbosity baseline for what reaches stderr |diff --git a/plugins/worktrunk/skills/worktrunk/reference/config.md b/plugins/worktrunk/skills/worktrunk/reference/config.mdindex facf030f7..9ba21ecb6 100644--- a/plugins/worktrunk/skills/worktrunk/reference/config.md+++ b/plugins/worktrunk/skills/worktrunk/reference/config.md@@ -657,7 +657,6 @@ $ WORKTRUNK_COMMIT__GENERATION__COMMAND="echo 'test: automated commit'" wt merge| `WORKTRUNK_CONFIG_PATH` | Override user config file location || `WORKTRUNK_SYSTEM_CONFIG_PATH` | Override system config file location || `WORKTRUNK_PROJECT_CONFIG_PATH` | Override project config file location (defaults to `.config/wt.toml`); relative paths resolve from the worktree root |-| `XDG_CONFIG_DIRS` | Colon-separated system config directories (default: `/etc/xdg`) || `WORKTRUNK_MAX_CONCURRENT_COMMANDS` | Max parallel git commands (default: 32). Lower if hitting file descriptor limits. || `WORKTRUNK_VERBOSE` | Verbosity level (`0`/`1`/`2`), like `-v`/`-vv` but applied everywhere — including shell completion, which no flag can reach || `RUST_LOG` | Logging directive (e.g. `worktrunk=debug`); overrides the verbosity baseline for what reaches stderr |diff --git a/skills/worktrunk/reference/config.md b/skills/worktrunk/reference/config.mdindex facf030f7..9ba21ecb6 100644--- a/skills/worktrunk/reference/config.md+++ b/skills/worktrunk/reference/config.md@@ -657,7 +657,6 @@ $ WORKTRUNK_COMMIT__GENERATION__COMMAND="echo 'test: automated commit'" wt merge| `WORKTRUNK_CONFIG_PATH` | Override user config file location || `WORKTRUNK_SYSTEM_CONFIG_PATH` | Override system config file location || `WORKTRUNK_PROJECT_CONFIG_PATH` | Override project config file location (defaults to `.config/wt.toml`); relative paths resolve from the worktree root |-| `XDG_CONFIG_DIRS` | Colon-separated system config directories (default: `/etc/xdg`) || `WORKTRUNK_MAX_CONCURRENT_COMMANDS` | Max parallel git commands (default: 32). Lower if hitting file descriptor limits. || `WORKTRUNK_VERBOSE` | Verbosity level (`0`/`1`/`2`), like `-v`/`-vv` but applied everywhere — including shell completion, which no flag can reach || `RUST_LOG` | Logging directive (e.g. `worktrunk=debug`); overrides the verbosity baseline for what reaches stderr |diff --git a/src/cli/mod.rs b/src/cli/mod.rsindex 298851d2b..e0907554e 100644--- a/src/cli/mod.rs+++ b/src/cli/mod.rs@@ -2632,7 +2632,6 @@ $ WORKTRUNK_COMMIT__GENERATION__COMMAND="echo 'test: automated commit'" wt merge| `WORKTRUNK_CONFIG_PATH` | Override user config file location || `WORKTRUNK_SYSTEM_CONFIG_PATH` | Override system config file location || `WORKTRUNK_PROJECT_CONFIG_PATH` | Override project config file location (defaults to `.config/wt.toml`); relative paths resolve from the worktree root |-| `XDG_CONFIG_DIRS` | Colon-separated system config directories (default: `/etc/xdg`) || `WORKTRUNK_MAX_CONCURRENT_COMMANDS` | Max parallel git commands (default: 32). Lower if hitting file descriptor limits. || `WORKTRUNK_VERBOSE` | Verbosity level (`0`/`1`/`2`), like `-v`/`-vv` but applied everywhere — including shell completion, which no flag can reach || `RUST_LOG` | Logging directive (e.g. `worktrunk=debug`); overrides the verbosity baseline for what reaches stderr |diff --git a/src/commands/config/show.rs b/src/commands/config/show.rsindex 26ef2eec1..859352ef5 100644--- a/src/commands/config/show.rs+++ b/src/commands/config/show.rs@@ -12,7 +12,7 @@ use color_print::cformat;use serde::{Serialize, de::DeserializeOwned};use worktrunk::config::{ConfigError, ConfigFileKind, ConfigParseError, LoadError, ProjectConfig, UserConfig,- default_system_config_path, require_config_path, system_config_path,+ require_config_path, system_config_path,};use worktrunk::git::remote_ref::azure::azure_devops_extension_installed;use worktrunk::git::{ErrorExt, ForgeKind, Repository, WorktrunkError};@@ -196,7 +196,7 @@ fn handle_config_show_json() -> anyhow::Result<()> {(None, false, None, None)};- let system_path = system_config_path().or_else(default_system_config_path);+ let system_path = system_config_path();let system_exists = system_path.as_ref().is_some_and(|p| p.exists());let system_invalid = if let Some(path) = system_path.as_deref().filter(|_| system_exists) {match std::fs::read_to_string(path) {@@ -691,7 +691,7 @@ fn render_diagnostics(out: &mut String, repo: Option<&Repository>) -> anyhow::Re/// Render system config when present, returning whether it is invalid.fn render_system_config(out: &mut String) -> Option<bool> {- let system_path = system_config_path()?;+ let system_path = system_config_path().filter(|path| path.exists())?;let _ = writeln!(out,@@ -818,7 +818,7 @@ fn render_user_config(}fn render_system_config_hint(out: &mut String) -> anyhow::Result<()> {- if let Some(path) = default_system_config_path() {+ if let Some(path) = system_config_path() {writeln!(out,"{}",diff --git a/src/config/mod.rs b/src/config/mod.rsindex 836931f0c..c810a0613 100644--- a/src/config/mod.rs+++ b/src/config/mod.rs@@ -232,8 +232,8 @@ pub use user::{CommitConfig, CommitGenerationConfig, CopyIgnoredConfig, ListColumnConfig, ListConfig,MergeConfig, RemoveConfig, ResolvedConfig, StageMode, StepConfig, SwitchConfig,SwitchPickerConfig, UserConfig, UserProjectOverrides, config_path, config_path_for_display,- default_config_path, default_system_config_path, require_config_path, set_config_overrides,- set_config_path, system_config_path, valid_user_config_keys,+ default_config_path, require_config_path, set_config_overrides, set_config_path,+ system_config_path, valid_user_config_keys,};#[cfg(test)]diff --git a/src/config/user/mod.rs b/src/config/user/mod.rsindex e567d421d..733fe3bf8 100644--- a/src/config/user/mod.rs+++ b/src/config/user/mod.rs@@ -63,8 +63,8 @@ fn cli_config_overrides() -> &'static [String] {// Re-export public typespub use merge::Merge;pub use path::{- config_path, config_path_for_display, default_config_path, default_system_config_path,- require_config_path, set_config_path, system_config_path,+ config_path, config_path_for_display, default_config_path, require_config_path,+ set_config_path, system_config_path,};pub use resolved::ResolvedConfig;pub use schema::valid_user_config_keys;diff --git a/src/config/user/path.rs b/src/config/user/path.rsindex 4f49b77ea..9e6b0e6cf 100644--- a/src/config/user/path.rs+++ b/src/config/user/path.rs@@ -117,108 +117,32 @@ pub fn default_config_path() -> Option<PathBuf> {Some(strategy.config_dir().join("worktrunk").join("config.toml"))}-/// Get the system-wide config file path, if one exists.+/// The system-wide config file's location, whether or not the file exists.////// System config provides organization-wide defaults that user config overrides.-/// Returns the first existing config file found in the system config directories.+/// `WORKTRUNK_SYSTEM_CONFIG_PATH` overrides the location; otherwise it is one+/// fixed path per platform, as git's `--system` file is:+/// - macOS: `/Library/Application Support/worktrunk/config.toml`+/// - Windows: `%PROGRAMDATA%\worktrunk\config.toml`+/// - other Unix: `/etc/xdg/worktrunk/config.toml`///-/// Priority:-/// 1. WORKTRUNK_SYSTEM_CONFIG_PATH environment variable (for testing/overrides)-/// 2. Each directory in $XDG_CONFIG_DIRS (colon-separated, checked in order)-/// 3. Platform-specific default:-/// - Linux: /etc/xdg/worktrunk/config.toml (XDG default)-/// - macOS: /Library/Application Support/worktrunk/config.toml-/// - Windows: %PROGRAMDATA%\worktrunk\config.toml-pub fn system_config_path() -> Option<PathBuf> {- // Priority 1: Explicit environment variable override- if let Ok(path) = std::env::var("WORKTRUNK_SYSTEM_CONFIG_PATH") {- let path = resolve_input_path(path);- if path.exists() {- return Some(path);- }- return None;- }-- // Priority 2+3: Check XDG_CONFIG_DIRS (if set), otherwise platform defaults.- // When XDG_CONFIG_DIRS is set, system_config_dirs() returns only those dirs- // (per XDG spec, no fallback to platform defaults).- //- // Deliberately unguarded, unlike `config_path()`: this resolves a- // machine-wide file (`/etc/xdg`, `/Library/Application Support`) rather than- // the developer's own config.- for dir in &system_config_dirs() {- let path = dir.join("worktrunk").join("config.toml");- if path.exists() {- return Some(path);- }- }-- None-}--/// The expected system config path for the current platform.+/// `XDG_CONFIG_DIRS` is deliberately not consulted: an administrator deploys to+/// the fixed path, and a search path read from the environment would bring the+/// spec's parsing rules with it for a case no one has asked for.///-/// Used by `wt config show` to display where to put a system config file.-/// Mirrors the lookup order in `system_config_path()` so the displayed-/// path matches where the tool actually looks.-pub fn default_system_config_path() -> Option<PathBuf> {+/// Deliberately unguarded, unlike `config_path()`: this resolves a machine-wide+/// file rather than the developer's own config.+pub fn system_config_path() -> Option<PathBuf> {if let Ok(path) = std::env::var("WORKTRUNK_SYSTEM_CONFIG_PATH") {return Some(resolve_input_path(path));}- system_config_dirs()- .first()- .map(|dir| dir.join("worktrunk").join("config.toml"))-}--/// System config directories in priority order.-///-/// On Unix, checks `XDG_CONFIG_DIRS` first. When set, it defines the search-/// path exclusively (per XDG spec) — no fallback to platform defaults.-/// Otherwise, returns platform-specific defaults (macOS: `/Library/Application-/// Support`, Windows: `%PROGRAMDATA%`, Unix: `/etc/xdg`).-fn system_config_dirs() -> Vec<PathBuf> {- #[cfg(unix)]- if let Ok(dirs_str) = std::env::var("XDG_CONFIG_DIRS") {- let dirs: Vec<PathBuf> = dirs_str- .split(':')- .filter(|d| !d.is_empty())- .map(PathBuf::from)- .collect();- if !dirs.is_empty() {- return dirs;- }- }-- platform_default_dirs()-}--/// Platform-specific default system config directories.-///-/// Returns directories in priority order — the first existing config file wins.-/// On macOS, the native `/Library/Application Support/` is checked before the-/// XDG fallback `/etc/xdg/`.-#[allow(clippy::vec_init_then_push)]-fn platform_default_dirs() -> Vec<PathBuf> {- let mut dirs = Vec::new();-#[cfg(target_os = "macos")]- {- // macOS native system-wide config location (checked first)- dirs.push(PathBuf::from("/Library/Application Support"));- }-- #[cfg(target_os = "windows")]- {- // Windows: %PROGRAMDATA% (typically C:\ProgramData)- if let Ok(program_data) = std::env::var("PROGRAMDATA") {- dirs.push(PathBuf::from(program_data));- }- }-- // XDG default: /etc/xdg (standard on Linux, fallback on macOS/other Unix)- #[cfg(unix)]- dirs.push(PathBuf::from("/etc/xdg"));+ let dir = Some(PathBuf::from("/Library/Application Support"));+ #[cfg(windows)]+ let dir = std::env::var_os("PROGRAMDATA").map(PathBuf::from);+ #[cfg(not(any(target_os = "macos", windows)))]+ let dir = Some(PathBuf::from("/etc/xdg"));- dirs+ Some(dir?.join("worktrunk").join("config.toml"))}diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex b9bc0e3ea..bf7537c5c 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -645,9 +645,8 @@ pub fn base_path() -> &'static PathBuf {/// config directory) are already absolute./// - `WORKTRUNK_PROJECT_CONFIG_PATH` resolves from the worktree root, its/// documented base, because the project config is per-worktree.-/// - `WORKTRUNK_BIN` and `XDG_CONFIG_DIRS` are never opened by wt: the first is-/// expanded by the generated shell wrapper, and the XDG spec already requires-/// the second to be absolute.+/// - `WORKTRUNK_BIN` is never opened by wt: the generated shell wrapper+/// expands it.pub fn resolve_input_path(path: impl AsRef<Path>) -> PathBuf {let path = crate::path::expand_tilde(path.as_ref());match BASE_PATH.get() {diff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rsindex 11e198f83..302c15c60 100644--- a/tests/integration_tests/config_show.rs+++ b/tests/integration_tests/config_show.rs@@ -472,79 +472,6 @@ fn test_config_show_absent_system_config_is_a_user_config_hint(repo: TestRepo, t);}-#[rstest]-fn test_system_config_found_via_xdg_config_dirs(repo: TestRepo) {- // Create system config in a custom XDG directory- let xdg_dir = tempfile::tempdir().unwrap();- let config_dir = xdg_dir.path().join("worktrunk");- fs::create_dir_all(&config_dir).unwrap();- fs::write(- config_dir.join("config.toml"),- r#"worktree-path = "/xdg-org/{{ repo }}/{{ branch | sanitize }}"-"#,- )- .unwrap();-- // Use XDG_CONFIG_DIRS instead of WORKTRUNK_SYSTEM_CONFIG_PATH- let mut cmd = wt_command();- repo.configure_wt_cmd(&mut cmd);- cmd.env_remove("WORKTRUNK_SYSTEM_CONFIG_PATH");- cmd.env("XDG_CONFIG_DIRS", xdg_dir.path());- cmd.arg("list")- .arg("--format=json")- .current_dir(repo.root_path());-- let output = cmd.output().unwrap();- assert!(output.status.success());-- let stdout = String::from_utf8_lossy(&output.stdout);- let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();- let worktrees = json["items"].as_array().unwrap();-- for wt in worktrees {- if wt["is_primary"].as_bool() == Some(false) {- let path = wt["path"].as_str().unwrap();- assert!(- path.contains("/xdg-org/"),- "Expected XDG_CONFIG_DIRS system config, got: {path}"- );- }- }-}--#[rstest]-fn test_system_config_xdg_dirs_set_but_no_config_found(repo: TestRepo) {- // When XDG_CONFIG_DIRS is set but contains no worktrunk config,- // system config should be None (no fallback to platform defaults)- let empty_xdg_dir = tempfile::tempdir().unwrap();-- let mut cmd = wt_command();- repo.configure_wt_cmd(&mut cmd);- cmd.env_remove("WORKTRUNK_SYSTEM_CONFIG_PATH");- cmd.env("XDG_CONFIG_DIRS", empty_xdg_dir.path());- cmd.arg("list")- .arg("--format=json")- .current_dir(repo.root_path());-- let output = cmd.output().unwrap();- assert!(output.status.success());-- // Without system config, worktree paths should use the default template- let stdout = String::from_utf8_lossy(&output.stdout);- let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();- let worktrees = json["items"].as_array().unwrap();-- for wt in worktrees {- if wt["is_primary"].as_bool() == Some(false) {- let path = wt["path"].as_str().unwrap();- assert!(- !path.contains("/xdg-org/"),- "Should not use XDG system config path, got: {path}"- );- }- }-}-/// Test that `config show` displays empty system config with a hint#[rstest]fn test_config_show_empty_system_config(mut repo: TestRepo, temp_home: TempDir) {diff --git a/tests/snapshots/integration__integration_tests__help__help_config_long.snap b/tests/snapshots/integration__integration_tests__help__help_config_long.snapindex f742aba4b..cd6c5ef7d 100644--- a/tests/snapshots/integration__integration_tests__help__help_config_long.snap+++ b/tests/snapshots/integration__integration_tests__help__help_config_long.snap@@ -642,7 +642,6 @@ Override the LLM command in CI to use a mock:[2mWORKTRUNK_CONFIG_PATH[0m Override user config file location[2mWORKTRUNK_SYSTEM_CONFIG_PATH[0m Override system config file location[2mWORKTRUNK_PROJECT_CONFIG_PATH[0m Override project config file location (defaults to [2m.config/wt.toml[0m); relative paths resolve from the worktree root- [2mXDG_CONFIG_DIRS[0m Colon-separated system config directories (default: [2m/etc/xdg[0m)[2mWORKTRUNK_MAX_CONCURRENT_COMMANDS[0m Max parallel git commands (default: 32). Lower if hitting file descriptor limits.[2mWORKTRUNK_VERBOSE[0m Verbosity level ([2m0[0m/[2m1[0m/[2m2[0m), like [2m-v[0m/[2m-vv[0m but applied everywhere — including shell completion, which no flag can reach[2mRUST_LOG[0m Logging directive (e.g. [2mworktrunk=debug[0m); overrides the verbosity baseline for what reaches stderr