Skip to content

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

fix(shell): use builtin cd to bypass cd aliases (e.g. zoxide) (#2644)

Worktrunk Bot3ddfc84f58f6
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_bash.snap b/src/shell/snapshots/worktrunk__shell__tests__init_bash.snap
index 94c7cbe9b..e42b01e5e 100644
--- a/src/shell/snapshots/worktrunk__shell__tests__init_bash.snap
+++ b/src/shell/snapshots/worktrunk__shell__tests__init_bash.snap
@@ -39,9 +39,12 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
command "${WORKTRUNK_BIN:-wt}" "${args[@]}" || exit_code=$?
fi
- # cd file holds a raw path (no shell escaping needed)
+ # cd file holds a raw path (no shell escaping needed).
+ # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
+ # `alias cd=__zoxide_z`) that would otherwise be substituted into this
+ # function body when it's defined. See #2643.
if [[ -s "$cd_file" ]]; then
- cd -- "$(<"$cd_file")"
+ builtin cd -- "$(<"$cd_file")"
local cd_exit=$?
if [[ $exit_code -eq 0 ]]; then
exit_code=$cd_exit
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_zsh.snap b/src/shell/snapshots/worktrunk__shell__tests__init_zsh.snap
index d6a97effe..dd7888814 100644
--- a/src/shell/snapshots/worktrunk__shell__tests__init_zsh.snap
+++ b/src/shell/snapshots/worktrunk__shell__tests__init_zsh.snap
@@ -42,9 +42,12 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
command "${WORKTRUNK_BIN:-wt}" "${args[@]}" || exit_code=$?
fi
- # cd file holds a raw path (no shell escaping needed)
+ # cd file holds a raw path (no shell escaping needed).
+ # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
+ # `alias cd=__zoxide_z`) that would otherwise be substituted into this
+ # function body when it's defined. See #2643.
if [[ -s "$cd_file" ]]; then
- cd -- "$(<"$cd_file")"
+ builtin cd -- "$(<"$cd_file")"
local cd_exit=$?
if [[ $exit_code -eq 0 ]]; then
exit_code=$cd_exit
diff --git a/src/shell/snapshots/worktrunk__shell__tests__shell_init_with_custom_prefix.snap b/src/shell/snapshots/worktrunk__shell__tests__shell_init_with_custom_prefix.snap
index c1e864cac..71ce20aff 100644
--- a/src/shell/snapshots/worktrunk__shell__tests__shell_init_with_custom_prefix.snap
+++ b/src/shell/snapshots/worktrunk__shell__tests__shell_init_with_custom_prefix.snap
@@ -39,9 +39,12 @@ if command -v custom >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
command "${WORKTRUNK_BIN:-custom}" "${args[@]}" || exit_code=$?
fi
- # cd file holds a raw path (no shell escaping needed)
+ # cd file holds a raw path (no shell escaping needed).
+ # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
+ # `alias cd=__zoxide_z`) that would otherwise be substituted into this
+ # function body when it's defined. See #2643.
if [[ -s "$cd_file" ]]; then
- cd -- "$(<"$cd_file")"
+ builtin cd -- "$(<"$cd_file")"
local cd_exit=$?
if [[ $exit_code -eq 0 ]]; then
exit_code=$cd_exit
diff --git a/templates/bash.sh b/templates/bash.sh
index b6521c683..996935c76 100644
--- a/templates/bash.sh
+++ b/templates/bash.sh
@@ -35,9 +35,12 @@ if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
command "${WORKTRUNK_BIN:-{{ cmd }}}" "${args[@]}" || exit_code=$?
fi
- # cd file holds a raw path (no shell escaping needed)
+ # cd file holds a raw path (no shell escaping needed).
+ # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
+ # `alias cd=__zoxide_z`) that would otherwise be substituted into this
+ # function body when it's defined. See #2643.
if [[ -s "$cd_file" ]]; then
- cd -- "$(<"$cd_file")"
+ builtin cd -- "$(<"$cd_file")"
local cd_exit=$?
if [[ $exit_code -eq 0 ]]; then
exit_code=$cd_exit
diff --git a/templates/zsh.zsh b/templates/zsh.zsh
index 53a01ab49..d013d8ba9 100644
--- a/templates/zsh.zsh
+++ b/templates/zsh.zsh
@@ -38,9 +38,12 @@ if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
command "${WORKTRUNK_BIN:-{{ cmd }}}" "${args[@]}" || exit_code=$?
fi
- # cd file holds a raw path (no shell escaping needed)
+ # cd file holds a raw path (no shell escaping needed).
+ # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
+ # `alias cd=__zoxide_z`) that would otherwise be substituted into this
+ # function body when it's defined. See #2643.
if [[ -s "$cd_file" ]]; then
- cd -- "$(<"$cd_file")"
+ builtin cd -- "$(<"$cd_file")"
local cd_exit=$?
if [[ $exit_code -eq 0 ]]; then
exit_code=$cd_exit
diff --git a/tests/integration_tests/e2e_shell.rs b/tests/integration_tests/e2e_shell.rs
index db89a7032..38fcab5b5 100644
--- a/tests/integration_tests/e2e_shell.rs
+++ b/tests/integration_tests/e2e_shell.rs
@@ -139,6 +139,67 @@ fn test_bash_shell_integration_switch_existing_worktree(repo: TestRepo) {
);
}
+/// Reproduction for #2643: when `cd` is aliased before worktrunk's shell
+/// integration is loaded (e.g., zoxide's `eval "$(zoxide init zsh)"` runs
+/// first), the `wt()` function's `cd` token is alias-expanded at eval time.
+/// This rewrites the function body to call `__zoxide_z -- "$path"`, which
+/// queries zoxide's database instead of changing to the absolute path, so
+/// `wt switch` reports success but the shell stays put.
+///
+/// The init code is written to a file and `source`d so that the file is
+/// parsed *after* the alias takes effect, matching the real-world
+/// `eval "$(wt config shell init zsh)"` flow.
+#[rstest]
+#[case("zsh")]
+#[case("bash")]
+fn test_shell_integration_cd_alias_does_not_break_switch(#[case] shell: &str, repo: TestRepo) {
+ let init_code = generate_init_code(&repo, shell);
+ let bin_path = wt_bin_dir();
+ let init_file = repo.home_path().join("wt-init.sh");
+ std::fs::write(&init_file, &init_code).unwrap();
+ let init_path = init_file.display();
+
+ // bash only expands aliases in non-interactive shells when
+ // expand_aliases is set; zsh expands them unconditionally.
+ let pre_init = match shell {
+ "bash" => "shopt -s expand_aliases\n",
+ _ => "",
+ };
+
+ let script = format!(
+ r#"
+ {}
+ {}
+ __zoxide_z() {{
+ echo "zoxide: no match found" >&2
+ return 1
+ }}
+ alias cd='__zoxide_z'
+ source "{}"
+ wt switch --create zoxide-branch
+ echo "__PWD_AFTER_SWITCH__ $PWD"
+ "#,
+ path_export_syntax(shell, &bin_path),
+ pre_init,
+ init_path,
+ );
+
+ let output = execute_shell_script(&repo, shell, &script);
+ let after_switch = extract_pwd_marker(&output, "__PWD_AFTER_SWITCH__").unwrap();
+
+ assert!(
+ after_switch.contains("zoxide-branch"),
+ "Shell should cd into zoxide-branch worktree even when cd is aliased, saw: {}\nFull output:\n{}",
+ after_switch,
+ output,
+ );
+ assert!(
+ !output.contains("zoxide: no match found"),
+ "cd should bypass the alias; got zoxide error in output:\n{}",
+ output,
+ );
+}
+
fn extract_pwd_marker(output: &str, marker: &str) -> Option<String> {
output
.lines()
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 17c9e079e..ed2814c02 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -13,10 +13,15 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -32,13 +37,17 @@ info:
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
@@ -81,9 +90,12 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
command "${WORKTRUNK_BIN:-wt}" "${args[@]}" || exit_code=$?
fi
- # cd file holds a raw path (no shell escaping needed)
+ # cd file holds a raw path (no shell escaping needed).
+ # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
+ # `alias cd=__zoxide_z`) that would otherwise be substituted into this
+ # function body when it's defined. See #2643.
if [[ -s "$cd_file" ]]; then
- cd -- "$(<"$cd_file")"
+ builtin cd -- "$(<"$cd_file")"
local cd_exit=$?
if [[ $exit_code -eq 0 ]]; then
exit_code=$cd_exit
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 52e18dfc6..cdf3a74c8 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -13,10 +13,15 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -32,13 +37,17 @@ info:
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
@@ -84,9 +93,12 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
command "${WORKTRUNK_BIN:-wt}" "${args[@]}" || exit_code=$?
fi
- # cd file holds a raw path (no shell escaping needed)
+ # cd file holds a raw path (no shell escaping needed).
+ # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
+ # `alias cd=__zoxide_z`) that would otherwise be substituted into this
+ # function body when it's defined. See #2643.
if [[ -s "$cd_file" ]]; then
- cd -- "$(<"$cd_file")"
+ builtin cd -- "$(<"$cd_file")"
local cd_exit=$?
if [[ $exit_code -eq 0 ]]; then
exit_code=$cd_exit