Repository navigation
Run --execute in the worktree even under --no-cd - #4179
Merged
Merged
Conversation
`--no-cd` moved the `--execute` program back to the invoking directory along with the shell, because one `change_directory()` call both wrote the shell's cd directive and recorded the program's working directory in global state. Nothing decided that; the coupling fell out of the shared value, and #4043 documented it in the help and matched the header to it. The program's directory is now an argument: `handle_switch_output` returns it as `SwitchDisplayPaths::execute_dir`, and `execute()` takes it from the caller rather than from `OutputState`, which loses `target_dir` entirely. `--no-cd` writes no directive and leaves the shell where it stands; the program still starts in the worktree the switch selected, subdirectory position preserved. Since that directory is always one wt chose, the git-discovery scrub now applies unconditionally too. So `wt switch feature --no-cd -x code -- .` opens the worktree and leaves the terminal where it was — the request behind #4042, which the reporter had to work around by passing `{{ worktree_path }}` as an argument. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The chained builder leaves nothing to mutate outside `#[cfg(unix)]`, so `let mut cmd` tripped `-D unused_mut` on Windows. Shadow under the cfg instead. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
worktrunk-bot
approved these changes
Sep 17, 2026
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
--no-cdtook the--executeprogram back to the invoking directory along with the shell.wt switch feature --no-cd -x pwdprinted the directory you ran it from, so a program that means "act on the new worktree" — an editor, an agent — had to be handed{{ worktree_path }}explicitly.Nothing decided that. One
change_directory()call did two jobs: it wrote the shell's cd directive and it recorded the program's working directory inOutputState::target_dir, whichexecute()read back. Skipping the directive therefore also dropped the program's directory. The behavior first appears in the docs in #3977, which described what the shared value already did, and #4043 then matched theExecuting (--execute) @ …header to it.In #4042 the reporter wanted exactly this combination — open the worktree in Zed, leave the terminal where it was — and had to work around it by passing the path as an argument.
Solution
The program's directory becomes an argument rather than a leftover of the directive.
handle_switch_outputcomputes it once and returns it asSwitchDisplayPaths::execute_dir;execute()takes it from the caller;OutputStatelosestarget_dir, so the two decisions can't be re-coupled through global state.change_directory()now only moves the shell.--no-cdwrites no directive and leaves the shell where it stands. The program starts in the worktree the switch selected, with the subdirectory positionresolve_subdir_in_targetpreserves, exactly as under--cd.Two consequences worth reading in the diff:
GIT_DIRand friends, Inherited GIT_* discovery vars are forwarded into user hooks, misdirecting hook git commands (can pollute core.worktree) #3373) was conditional on having a target directory, so it didn't run under--no-cd. That directory is now always one wt chose, which is the condition CLAUDE.md sets for scrubbing, so the scrub is unconditional.--no-cd, where it previously read a bareExecuting (--execute):. The updated snapshot shows both halves:pwdprints the worktree, and the header names it.This changes documented behavior of a flag combination rather than any flag or config key.
--executemeans "run this against the worktree I just selected", and--no-cdis a statement about the shell; the old behavior made the second silently override the first.Thanks to @yajo for reporting #4042.
🤖 Generated with Claude Code