Skip to content

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

fix(shell): honour $ZDOTDIR only when it is an absolute path (#4085)

Worktrunk Bot7d0aff7c763d
diff --git a/src/shell/detection.rs b/src/shell/detection.rs
index f7ee668fb..0bd8a5ef7 100644
--- a/src/shell/detection.rs
+++ b/src/shell/detection.rs
@@ -9,7 +9,7 @@ use std::fs;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
-use super::paths::{home_dir_required, powershell_profile_paths};
+use super::paths::{home_dir_required, powershell_profile_paths, zsh_config_dir};
/// Detect if a line contains shell integration for a specific command.
///
@@ -548,10 +548,7 @@ pub fn scan_for_detection_details(cmd: &str) -> Result<Vec<FileDetectionResult>,
home.join(".profile"),
// Zsh
home.join(".zshrc"),
- std::env::var("ZDOTDIR")
- .map(PathBuf::from)
- .unwrap_or_else(|_| home.clone())
- .join(".zshrc"),
+ zsh_config_dir(&home).join(".zshrc"),
// Fish functions/ (current location) — fish's own config dir, so
// detection reads the file install wrote.
super::paths::fish_config_dir(&home)
diff --git a/src/shell/paths.rs b/src/shell/paths.rs
index b416375e3..da9342e48 100644
--- a/src/shell/paths.rs
+++ b/src/shell/paths.rs
@@ -237,6 +237,28 @@ pub fn fish_config_dir(home: &std::path::Path) -> PathBuf {
.join("fish")
}
+/// The directory worktrunk reads and writes zsh's rc file in: `$ZDOTDIR` when
+/// it holds an absolute path, otherwise `$HOME`.
+///
+/// zsh's rule is `$ZDOTDIR`, or `$HOME` when it is unset. The absolute-only
+/// guard on top is the same one this module already applies to
+/// `$XDG_DATA_HOME` ([`nushell_data_dir_fallback`]) and, through etcetera's
+/// [`Xdg`], to `$XDG_CONFIG_HOME` ([`fish_config_dir`]).
+///
+/// A non-absolute value resolves against the current directory, and that
+/// directory is `wt`'s: zsh resolves one against *zsh's* own startup
+/// directory, which `wt` has no way to know at install time. Honouring it
+/// would send install, and the whole-file rewrite `wt config shell uninstall`
+/// performs, to a file neither side meant — a `.zshrc` in a dotfiles checkout
+/// that happened to be the invocation directory. `$HOME` is also where
+/// `wt config show` reads back from, so install and detection agree.
+pub(super) fn zsh_config_dir(home: &std::path::Path) -> PathBuf {
+ std::env::var_os("ZDOTDIR")
+ .map(PathBuf::from)
+ .filter(|path| path.is_absolute())
+ .unwrap_or_else(|| home.to_path_buf())
+}
+
/// 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,12 +271,7 @@ pub fn line_based_config_paths(shell: super::Shell, home: &std::path::Path) -> V
// Use .bashrc - sourced by interactive shells (login shells should source .bashrc)
vec![home.join(".bashrc")]
}
- super::Shell::Zsh => {
- let zdotdir = std::env::var("ZDOTDIR")
- .map(PathBuf::from)
- .unwrap_or_else(|_| home.to_path_buf());
- vec![zdotdir.join(".zshrc")]
- }
+ super::Shell::Zsh => vec![zsh_config_dir(home).join(".zshrc")],
super::Shell::PowerShell => powershell_profile_paths(home),
super::Shell::Fish | super::Shell::Nushell => Vec::new(),
}
diff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rs
index e1da6a228..863911643 100644
--- a/tests/integration_tests/configure_shell.rs
+++ b/tests/integration_tests/configure_shell.rs
@@ -3245,6 +3245,110 @@ fn test_nushell_install_target_is_a_vendor_autoload_dir(repo: TestRepo, temp_hom
);
}
+/// A `ZDOTDIR` that isn't an absolute path is ignored, and the zsh integration
+/// line lands in `$HOME/.zshrc`.
+///
+/// Regression guard: the value used to be taken at face value, so an empty one
+/// collapsed zsh's config path to the relative `.zshrc` and a bare `dotfiles`
+/// to `dotfiles/.zshrc` — either way install appended the integration line
+/// under whatever directory `wt` was run from, a dotfiles checkout being the
+/// obvious way to have a `.zshrc` sitting there, while the file zsh reads went
+/// untouched. `wt config shell uninstall` rewrites rc files whole, so the same
+/// resolution decides which file that rewrite lands on.
+#[rstest]
+#[case::empty("")]
+#[case::relative("dotfiles")]
+fn test_configure_shell_non_absolute_zdotdir_uses_home(
+ #[case] zdotdir: &str,
+ repo: TestRepo,
+ temp_home: TempDir,
+) {
+ let home_zshrc = temp_home.path().join(".zshrc");
+ fs::write(&home_zshrc, "# Existing config\n").unwrap();
+
+ // A decoy `.zshrc` where the unguarded value would have resolved: under the
+ // invocation directory, joined with `ZDOTDIR` itself.
+ let run_dir = temp_home.path().join("work");
+ let decoy_zshrc = run_dir.join(zdotdir).join(".zshrc");
+ fs::create_dir_all(decoy_zshrc.parent().unwrap()).unwrap();
+ fs::write(&decoy_zshrc, "# Decoy\n").unwrap();
+
+ let mut cmd = wt_command();
+ repo.configure_wt_cmd(&mut cmd);
+ set_temp_home_env(&mut cmd, temp_home.path());
+ cmd.env("SHELL", "/bin/zsh");
+ cmd.env("ZDOTDIR", zdotdir);
+ cmd.env("WORKTRUNK_TEST_COMPINIT_CONFIGURED", "1");
+ cmd.args(["config", "shell", "install", "zsh", "--yes"]);
+ cmd.current_dir(&run_dir);
+
+ 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 home_contents = fs::read_to_string(&home_zshrc).unwrap();
+ assert!(
+ home_contents.contains("config shell init zsh"),
+ "integration line should land in $HOME/.zshrc, got:\n{home_contents}"
+ );
+ assert_eq!(
+ fs::read_to_string(&decoy_zshrc).unwrap(),
+ "# Decoy\n",
+ "the .zshrc under the invocation directory must be left alone"
+ );
+}
+
+/// An absolute `ZDOTDIR` is honoured: the zsh integration line lands under it,
+/// not under `$HOME`.
+///
+/// The companion to the non-absolute cases above, and the one that makes them
+/// mean something. Every other zsh test points `ZDOTDIR` at `$HOME` itself or
+/// at `/dev/null` for shell isolation, so without this case a `zsh_config_dir`
+/// that ignored the variable outright — the over-tightening the guard invites
+/// — would pass the whole suite.
+#[rstest]
+fn test_configure_shell_absolute_zdotdir_is_honoured(repo: TestRepo, temp_home: TempDir) {
+ let home = canonical_temp_home(&temp_home);
+ // Distinct from `$HOME`, so only honouring `ZDOTDIR` reaches it.
+ let zdotdir = home.join("zsh-config");
+ fs::create_dir_all(&zdotdir).unwrap();
+ fs::write(zdotdir.join(".zshrc"), "# Existing config\n").unwrap();
+ // A decoy at the fallback, so the assertion separates the two answers.
+ fs::write(home.join(".zshrc"), "# Decoy\n").unwrap();
+
+ let mut cmd = wt_command();
+ repo.configure_wt_cmd(&mut cmd);
+ set_temp_home_env(&mut cmd, temp_home.path());
+ cmd.env("SHELL", "/bin/zsh");
+ cmd.env("ZDOTDIR", &zdotdir);
+ cmd.env("WORKTRUNK_TEST_COMPINIT_CONFIGURED", "1");
+ cmd.args(["config", "shell", "install", "zsh", "--yes"]);
+ cmd.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 zdotdir_contents = fs::read_to_string(zdotdir.join(".zshrc")).unwrap();
+ assert!(
+ zdotdir_contents.contains("config shell init zsh"),
+ "integration line should land in $ZDOTDIR/.zshrc, got:\n{zdotdir_contents}"
+ );
+ assert_eq!(
+ fs::read_to_string(home.join(".zshrc")).unwrap(),
+ "# Decoy\n",
+ "$HOME/.zshrc must be left alone when $ZDOTDIR is absolute"
+ );
+}
+
/// A non-default `XDG_CONFIG_HOME` sends the fish wrapper and the fish
/// completion to the same fish config directory.
///