Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(shell): resolve fish's config directory once, so the wrapper lands where fish reads it (#4107)

Worktrunk Bot48afc26b1970
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dced8f9a9..80c0df8db 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@
### Fixed
+- **`wt config shell install fish` writes the wrapper where fish reads it under a custom `$XDG_CONFIG_HOME`**: the wrapper path was hardcoded to `~/.config/fish/functions/`, while the completion path resolved the XDG config directory — same tool, same command, two answers. With `$XDG_CONFIG_HOME` pointing anywhere but `~/.config`, install reported success for both files, fish autoloaded the completion and never saw the wrapper, so `wt switch` changed no directory and `wt config show` reported the integration as installed. The wrapper, the completion, and the directories `wt config shell uninstall` and `wt config show` scan now all come from one resolution of fish's `$__fish_config_dir`. On Unix an unset or default `$XDG_CONFIG_HOME` is unaffected; on Windows the fish completion moves off `%APPDATA%\fish\completions` onto the same path as the wrapper. The deprecated `conf.d` file install cleans up stays pinned to `~/.config/fish/conf.d`, the only place worktrunk ever wrote it.
+
- **`wt remove`, `wt merge`, and `wt step prune` no longer discard untracked files hidden by Git configuration**: `status.showUntrackedFiles = no` made the clean-worktree guard inherit a display preference, so a worktree containing only untracked files appeared clean and could be removed without `--force` — including by `wt step prune`, which removes unattended. Safety checks now always include untracked files and name the path that blocks removal. Auto-staging warnings for `wt merge`, `wt step commit`, and `wt step squash` likewise override the setting and enumerate every file that will be staged.
- **`wt config plugins opencode install` no longer writes the plugin into the directory it was run from**: an exported-but-empty `OPENCODE_CONFIG_DIR` was taken at face value, so the install target collapsed to the relative path `plugins/worktrunk.ts` — typically somewhere inside the user's repository. `is_plugin_installed()` then read the plugin back from that same relative path, so the install reported success while OpenCode never saw it. An empty value now reads as unset, as it already did for `$CLAUDE_CONFIG_DIR` and `$PI_CONFIG_DIR`; the documented precedence `$OPENCODE_CONFIG_DIR` > `$XDG_CONFIG_HOME/opencode` > `~/.config/opencode` is unchanged. ([#4084](https://github.com/max-sixty/worktrunk/pull/4084))
diff --git a/docs/src/content/docs/shell-integration.md b/docs/src/content/docs/shell-integration.md
index f0e3918c3..8cde50101 100644
--- a/docs/src/content/docs/shell-integration.md
+++ b/docs/src/content/docs/shell-integration.md
@@ -41,7 +41,7 @@ Invoke-Expression (& wt config shell init powershell | Out-String)
- **Bash**: adds a line to `~/.bashrc`
- **Zsh**: adds a line to `~/.zshrc` (or `$ZDOTDIR/.zshrc`)
-- **Fish**: creates `~/.config/fish/functions/wt.fish` and `~/.config/fish/completions/wt.fish`
+- **Fish**: creates `~/.config/fish/functions/wt.fish` and `~/.config/fish/completions/wt.fish` (under `$XDG_CONFIG_HOME/fish` when that is set)
- **Nushell** <span class="badge-experimental"></span>: creates `wt.nu` in Nushell's user vendor-autoload directory — the last entry of `$nu.vendor-autoload-dirs`, under `$nu.data-dir` (typically `~/.local/share/nushell/vendor/autoload` on Linux, `~/Library/Application Support/nushell/vendor/autoload` on macOS)
- **PowerShell** (Windows): creates both profile files if they don't exist:
- `Documents/PowerShell/Microsoft.PowerShell_profile.ps1` (PowerShell 7+)
diff --git a/plugins/worktrunk/skills/worktrunk/reference/shell-integration.md b/plugins/worktrunk/skills/worktrunk/reference/shell-integration.md
index 7c72d3890..2d41a6022 100644
--- a/plugins/worktrunk/skills/worktrunk/reference/shell-integration.md
+++ b/plugins/worktrunk/skills/worktrunk/reference/shell-integration.md
@@ -37,7 +37,7 @@ Invoke-Expression (& wt config shell init powershell | Out-String)
- **Bash**: adds a line to `~/.bashrc`
- **Zsh**: adds a line to `~/.zshrc` (or `$ZDOTDIR/.zshrc`)
-- **Fish**: creates `~/.config/fish/functions/wt.fish` and `~/.config/fish/completions/wt.fish`
+- **Fish**: creates `~/.config/fish/functions/wt.fish` and `~/.config/fish/completions/wt.fish` (under `$XDG_CONFIG_HOME/fish` when that is set)
- **Nushell** [experimental]: creates `wt.nu` in Nushell's user vendor-autoload directory — the last entry of `$nu.vendor-autoload-dirs`, under `$nu.data-dir` (typically `~/.local/share/nushell/vendor/autoload` on Linux, `~/Library/Application Support/nushell/vendor/autoload` on macOS)
- **PowerShell** (Windows): creates both profile files if they don't exist:
- `Documents/PowerShell/Microsoft.PowerShell_profile.ps1` (PowerShell 7+)
diff --git a/skills/worktrunk/reference/shell-integration.md b/skills/worktrunk/reference/shell-integration.md
index 7c72d3890..2d41a6022 100644
--- a/skills/worktrunk/reference/shell-integration.md
+++ b/skills/worktrunk/reference/shell-integration.md
@@ -37,7 +37,7 @@ Invoke-Expression (& wt config shell init powershell | Out-String)
- **Bash**: adds a line to `~/.bashrc`
- **Zsh**: adds a line to `~/.zshrc` (or `$ZDOTDIR/.zshrc`)
-- **Fish**: creates `~/.config/fish/functions/wt.fish` and `~/.config/fish/completions/wt.fish`
+- **Fish**: creates `~/.config/fish/functions/wt.fish` and `~/.config/fish/completions/wt.fish` (under `$XDG_CONFIG_HOME/fish` when that is set)
- **Nushell** [experimental]: creates `wt.nu` in Nushell's user vendor-autoload directory — the last entry of `$nu.vendor-autoload-dirs`, under `$nu.data-dir` (typically `~/.local/share/nushell/vendor/autoload` on Linux, `~/Library/Application Support/nushell/vendor/autoload` on macOS)
- **PowerShell** (Windows): creates both profile files if they don't exist:
- `Documents/PowerShell/Microsoft.PowerShell_profile.ps1` (PowerShell 7+)
diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rs
index 6876fa80a..a77c7bbc7 100644
--- a/src/commands/configure_shell.rs
+++ b/src/commands/configure_shell.rs
@@ -1327,7 +1327,12 @@ fn scan_for_uninstall(shell_filter: Option<Shell>) -> Result<UninstallScanResult
for &shell in &shells {
match shell {
Shell::Fish => {
- let functions_dir = home.join(".config").join("fish").join("functions");
+ // Uninstall follows fish's own config dir, so it scans the
+ // directory install writes to. A wrapper an older worktrunk
+ // left under `~/.config/fish` while `$XDG_CONFIG_HOME` pointed
+ // elsewhere is not chased: fish never loaded it, so it is not
+ // integration to remove.
+ let functions_dir = shell::fish_config_dir(&home).join("functions");
let confd_dir = home.join(".config").join("fish").join("conf.d");
let canonical =
@@ -1414,7 +1419,7 @@ fn scan_for_uninstall(shell_filter: Option<Shell>) -> Result<UninstallScanResult
let mut completion_results = Vec::new();
let mut completion_not_found = Vec::new();
if shells.contains(&Shell::Fish) {
- let completions_dir = home.join(".config").join("fish").join("completions");
+ let completions_dir = shell::fish_config_dir(&home).join("completions");
let completions =
scan_managed_files(&completions_dir, "fish", |c| c.contains(COMPLETION_MARKER))?;
if completions.is_empty() {
diff --git a/src/shell/detection.rs b/src/shell/detection.rs
index e09cca3fd..f7ee668fb 100644
--- a/src/shell/detection.rs
+++ b/src/shell/detection.rs
@@ -552,10 +552,14 @@ pub fn scan_for_detection_details(cmd: &str) -> Result<Vec<FileDetectionResult>,
.map(PathBuf::from)
.unwrap_or_else(|_| home.clone())
.join(".zshrc"),
- // Fish functions/ (current location)
- home.join(".config/fish/functions")
+ // Fish functions/ (current location) — fish's own config dir, so
+ // detection reads the file install wrote.
+ super::paths::fish_config_dir(&home)
+ .join("functions")
.join(format!("{cmd}.fish")),
- // Fish conf.d (legacy location - for detecting existing installs)
+ // Fish conf.d (legacy location - for detecting existing installs).
+ // Pinned to `~/.config`, matching `legacy_fish_conf_d_path`: that is
+ // where worktrunk wrote it, and `wt config show` compares the two.
home.join(".config/fish/conf.d").join(format!("{cmd}.fish")),
];
diff --git a/src/shell/mod.rs b/src/shell/mod.rs
index 4df6a648e..c8dc9e72c 100644
--- a/src/shell/mod.rs
+++ b/src/shell/mod.rs
@@ -18,7 +18,9 @@ pub use detection::{
BypassAlias, DetectedLine, FileDetectionResult, is_shell_integration_line,
is_shell_integration_line_for_uninstall_any_cmd, scan_for_detection_details,
};
-pub use paths::{home_dir_required, line_based_config_paths, nushell_autoload_candidates};
+pub use paths::{
+ fish_config_dir, home_dir_required, line_based_config_paths, nushell_autoload_candidates,
+};
pub use utils::{
AncestorShell, ZshStartupScope, ancestor_shell, current_shell, current_shell_name,
extract_filename_from_path, probe_zsh_compdef,
diff --git a/src/shell/paths.rs b/src/shell/paths.rs
index 12f299c29..b416375e3 100644
--- a/src/shell/paths.rs
+++ b/src/shell/paths.rs
@@ -3,7 +3,7 @@
//! This module handles locating shell configuration files (e.g., `.bashrc`, `.zshrc`)
//! and completion directories for different shells.
-use etcetera::base_strategy::{BaseStrategy, choose_base_strategy};
+use etcetera::base_strategy::{BaseStrategy, Xdg, choose_base_strategy};
use std::path::PathBuf;
use std::sync::OnceLock;
@@ -209,6 +209,34 @@ pub fn powershell_profile_paths(home: &std::path::Path) -> Vec<PathBuf> {
}
}
+/// The directory fish reads its configuration from — fish's own
+/// `$__fish_config_dir`: `$XDG_CONFIG_HOME/fish` when that variable holds an
+/// absolute path, otherwise `~/.config/fish`. `etcetera`'s [`Xdg`] strategy is
+/// that rule, so nothing about it is restated here.
+///
+/// [`Xdg`] rather than `choose_base_strategy`, which resolves `%APPDATA%` on
+/// Windows: fish has no `%APPDATA%` notion on any platform, so one directory
+/// holds the wrapper and the completion there too. It isn't one a Windows fish
+/// reads — `Xdg::new` takes its home from `std::env::home_dir`, which is
+/// `%USERPROFILE%` there and, since Rust 1.85, deliberately not the POSIX
+/// `$HOME` a Cygwin or MSYS fish runs under. The two strategies are the same
+/// everywhere else.
+///
+/// Every path fish itself reads goes through this one function — the wrapper
+/// (`config_paths`) and the completion (`completion_path`) — because they have
+/// to agree. Resolving the wrapper's directory separately from the completion's
+/// put the wrapper under `~/.config/fish` whenever `$XDG_CONFIG_HOME` pointed
+/// anywhere else, which fish never reads, while the completion landed in the
+/// directory it does read; install reported success for both and `wt` was never
+/// defined. `legacy_fish_conf_d_path` is deliberately not one of these — see
+/// its own note.
+pub fn fish_config_dir(home: &std::path::Path) -> PathBuf {
+ Xdg::new()
+ .map(|xdg| xdg.config_dir())
+ .unwrap_or_else(|_| home.join(".config"))
+ .join("fish")
+}
+
/// Rc/profile files scanned line-by-line for integration lines.
///
/// Bash/Zsh/PowerShell integration is one line in an rc file, so these paths
@@ -249,8 +277,7 @@ pub(super) fn config_paths(shell: super::Shell, cmd: &str) -> Result<Vec<PathBuf
// fixing the issue where Homebrew PATH setup in config.fish runs
// after conf.d/ files. See: https://github.com/max-sixty/worktrunk/issues/566
vec![
- home.join(".config")
- .join("fish")
+ fish_config_dir(&home)
.join("functions")
.join(format!("{}.fish", cmd)),
]
@@ -275,6 +302,15 @@ pub(super) fn config_paths(shell: super::Shell, cmd: &str) -> Result<Vec<PathBuf
/// This caused issues with Homebrew PATH setup (see issue #566). We now install to
/// `functions/{cmd}.fish` instead. This method returns the legacy path so install/uninstall
/// can clean it up.
+///
+/// Hardcoded to `~/.config`, unlike [`fish_config_dir`], because this names a
+/// file worktrunk *wrote*, not a file fish reads: the write was hardcoded for
+/// as long as it existed, and install removes what this resolves to whole and
+/// unread. Resolving it through `$XDG_CONFIG_HOME` would both stop finding the
+/// stranded file and start deleting a `conf.d/{cmd}.fish` in the directory fish
+/// actually loads — one worktrunk never wrote and whose contents are never
+/// read. Uninstall's directory scan is gated on worktrunk-managed content and
+/// carries no such constraint.
pub(super) fn legacy_fish_conf_d_path(cmd: &str) -> Result<PathBuf, std::io::Error> {
let home = home_dir_required()?;
Ok(home
@@ -311,16 +347,9 @@ pub(super) fn completion_path(shell: super::Shell, cmd: &str) -> Result<PathBuf,
.join(cmd)
}
super::Shell::Zsh => home.join(".zfunc").join(format!("_{}", cmd)),
- super::Shell::Fish => {
- let config_home = strategy
- .as_ref()
- .map(|s| s.config_dir())
- .unwrap_or_else(|| home.join(".config"));
- config_home
- .join("fish")
- .join("completions")
- .join(format!("{}.fish", cmd))
- }
+ super::Shell::Fish => fish_config_dir(&home)
+ .join("completions")
+ .join(format!("{}.fish", cmd)),
super::Shell::Nushell => {
// Nushell completions are defined inline in the init script.
// Return the canonical vendor-autoload path (same as config).
diff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rs
index 8fa7fa2d1..e1da6a228 100644
--- a/tests/integration_tests/configure_shell.rs
+++ b/tests/integration_tests/configure_shell.rs
@@ -3244,3 +3244,78 @@ fn test_nushell_install_target_is_a_vendor_autoload_dir(repo: TestRepo, temp_hom
vendor-autoload-dirs:\n{dirs}"
);
}
+
+/// A non-default `XDG_CONFIG_HOME` sends the fish wrapper and the fish
+/// completion to the same fish config directory.
+///
+/// Regression guard: `config_paths` hardcoded `~/.config/fish` while
+/// `completion_path` resolved the XDG config dir, so with the variable pointing
+/// anywhere else worktrunk wrote the wrapper where fish never looks and the
+/// completion where it does — install reported success for both and `wt` was
+/// never defined as a function.
+#[rstest]
+#[cfg(unix)]
+fn test_configure_shell_fish_honors_xdg_config_home(repo: TestRepo, temp_home: TempDir) {
+ let xdg_config = temp_home.path().join("xdg-config");
+ fs::create_dir_all(&xdg_config).unwrap();
+
+ let mut cmd = wt_command();
+ repo.configure_wt_cmd(&mut cmd);
+ set_temp_home_env(&mut cmd, temp_home.path());
+ // After `set_temp_home_env`, which pins the variable to `$HOME/.config` —
+ // the one value that makes the hardcode and the XDG lookup agree.
+ cmd.env("XDG_CONFIG_HOME", &xdg_config);
+ cmd.env("SHELL", "/bin/fish");
+ cmd.args(["config", "shell", "install", "fish", "--yes"])
+ .current_dir(repo.root_path());
+
+ let output = cmd.output().expect("install command should run");
+ assert!(
+ output.status.success(),
+ "install failed: stdout={}, stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr),
+ );
+
+ let wrapper = xdg_config.join("fish/functions/wt.fish");
+ let completion = xdg_config.join("fish/completions/wt.fish");
+ assert!(
+ wrapper.exists(),
+ "wrapper should land under $XDG_CONFIG_HOME: {wrapper:?}"
+ );
+ assert!(
+ completion.exists(),
+ "completion should land under $XDG_CONFIG_HOME: {completion:?}"
+ );
+ assert!(
+ !temp_home
+ .path()
+ .join(".config/fish/functions/wt.fish")
+ .exists(),
+ "nothing should be written to ~/.config/fish when $XDG_CONFIG_HOME points elsewhere"
+ );
+
+ // Uninstall scans the same directory install wrote to, so what install
+ // creates is what uninstall can remove.
+ let mut uninstall = wt_command();
+ repo.configure_wt_cmd(&mut uninstall);
+ set_temp_home_env(&mut uninstall, temp_home.path());
+ uninstall.env("XDG_CONFIG_HOME", &xdg_config);
+ uninstall.env("SHELL", "/bin/fish");
+ uninstall
+ .args(["config", "shell", "uninstall", "fish", "--yes"])
+ .current_dir(repo.root_path());
+
+ let output = uninstall.output().expect("uninstall command should run");
+ assert!(
+ output.status.success(),
+ "uninstall failed: stdout={}, stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr),
+ );
+ assert!(!wrapper.exists(), "uninstall should remove {wrapper:?}");
+ assert!(
+ !completion.exists(),
+ "uninstall should remove {completion:?}"
+ );
+}