Skip to content

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

Merged
max-sixty merged 1 commit into
mainfrom
fix/issue-2643
May 8, 2026
Merged

max-sixty merged 1 commit into
mainfrom
fix/issue-2643

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

Reported in #2643. When a user has zoxide (or another tool that aliases cd) initialized before worktrunk's shell integration, wt switch reports success but the shell does not change directory:

○ Switched to worktree for ...
zoxide: no match found

zsh expands aliases at parse time. When eval "$(wt config shell init zsh)" runs after eval "$(zoxide init zsh)", the cd -- token inside the generated wt() function body is rewritten to __zoxide_z --. zoxide's __zoxide_z only resolves database entries, not arbitrary absolute paths, so the directory change fails. The same shape applies to bash when expand_aliases is set.

Solution

Replace cd -- "$(<"$cd_file")" with builtin cd -- "$(<"$cd_file")" in templates/zsh.zsh and templates/bash.sh. The builtin keyword forces the shell builtin and bypasses any user-defined cd alias or function. fish, nushell, and powershell templates are unaffected — fish aliases are functions resolved at call time, and nushell/powershell don't have alias-at-parse-time semantics on cd.

Testing

Added test_shell_integration_cd_alias_does_not_break_switch in tests/integration_tests/e2e_shell.rs (parametrized over zsh and bash). The test writes the init code to a file, sets up an __zoxide_z-style alias, sources the init file, and verifies that wt switch --create actually changes $PWD. The test fails on the unfixed templates and passes after the change.

Snapshot updates in init_zsh, init_bash, and shell_init_with_custom_prefix reflect the new template content.


Closes #2643 — automated triage

When a user has a tool like zoxide initialized in their shell *before*
worktrunk's shell integration (e.g. `eval "$(zoxide init zsh)"` then
`eval "$(wt config shell init zsh)"`), zoxide aliases `cd` to
`__zoxide_z`. zsh expands aliases at parse time, so the `cd --` token
inside the `wt()` function body is rewritten to `__zoxide_z --` when the
init code is evaluated. zoxide then rejects the absolute path and `wt
switch` reports success but the shell does not change directory.

Use `builtin cd --` so the directive-file `cd` always invokes the shell
builtin regardless of any user `cd` alias or function. The same fix
applies to the bash template, which has the same shape when
`expand_aliases` is set.

Closes #2643

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 3ddfc84 into main May 8, 2026
23 checks passed
@max-sixty
max-sixty deleted the fix/issue-2643 branch May 8, 2026 15:15
@max-sixty max-sixty mentioned this pull request May 10, 2026
3 tasks done
max-sixty added a commit that referenced this pull request May 10, 2026
Bump worktrunk to **0.49.0** — minor bump (semver-checks reports a
breaking change in `GitError::UncommittedChanges`, which gained a
`dirty_files: Vec<String>` field).

## Highlights

**Improved**
- New `codename(n)` template filter — deterministic friendly worktree
names from a ~1.26M-combo pool (`{{ branch | codename(2) }}` →
`malleable-opah`).
[#2641](#2641), thanks
@endigma
- Picker preview disk cache for Log / BranchDiff / UpstreamDiff, with
background refresh for stale Log decorations.
[#2628](#2628),
[#2646](#2646)
- Picker shares one `default_branch_sha` lookup across BranchDiff items
instead of forking N rev-parse subprocesses.
[#2658](#2658)
- "X has uncommitted changes" errors now list the dirty files inline.
[#2653](#2653)

**Fixed**
- `wt switch` integrates with `cd` aliases like zoxide (uses `builtin
cd`). [#2644](#2644), thanks
@xkumiyu for reporting
- Empty hook tables no longer panic during `wt switch --create`.
[#2635](#2635), thanks @topit
for reporting
- `Repository::current_worktree()` and five sibling helpers no longer
silently leak the process CWD when called via `Repository::at(p)`.
[#2652](#2652)
- `WorkingTree::is_linked` tolerates non-git CWDs (e.g. Nix sandbox).
[#2625](#2625), thanks
@DArtagan for reporting
- Picker no longer overlays `collect` warnings on the active TUI; clears
its frame on exit in inline mode.
[#2627](#2627),
[#2626](#2626)
- Failed `git log` no longer poisons the picker's preview disk cache.
[#2651](#2651)
- `wt step prune` says "removing branch" for branch-only candidates.
[#2619](#2619)
- Claude Code Windows integration uses a `wt.sh` wrapper to avoid
colliding with Windows Terminal's `wt.exe`.
[#1754](#1754), thanks
@lucaspimentel

**Documentation**
- cmux workspace integration recipe.
[#1907](#1907), thanks
@alvistar

See
[CHANGELOG.md](https://github.com/max-sixty/worktrunk/blob/release/CHANGELOG.md)
for the full list including internal CI / test-isolation changes.

## Test plan

- [x] `cargo run -- hook pre-merge --yes` (tests + lints clean)
- [x] `cargo semver-checks check-release -p worktrunk` (one breaking
change confirmed → minor bump)
- [x] CHANGELOG entries verified by subagent against actual diffs

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: wt switch fails with zoxide: no match found when cd is aliased to __zoxide_z

2 participants