Skip to content

fix: don't cd into a directory removed before wt exits - #4602

Merged
max-sixty merged 4 commits into
mainfrom
execute-removed-cd
Oct 26, 2026
Merged

max-sixty merged 4 commits into
mainfrom
execute-removed-cd

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

The shell wrapper acts on the cd directive only after wt exits, and work that runs in between can remove the directory it names. wt switch -x writes the directive before starting the program, so a program that removes its own worktree (an agent session that ends with wt remove) left the wrapper to cd into a missing directory: fish printed cd: The directory '…' does not exist and the wrapper returned failure. A hook or alias whose nested wt switch targets a worktree that a later step removes hits the same thing.

As wt finishes, finish_command now reads the directive file and empties it when the directory it names no longer exists, so the shell stays where it started. The outermost wt finishes last, after everything it runs in the foreground, so this one check covers every writer — the -x program, hooks, aliases, and nested wt — without touching the five shell wrappers. It is best-effort: if the file can't be read or emptied, the wrapper's own cd error still names the directory.

One behavior change: a hook that writes a directive naming a path that doesn't exist (a typo, say) used to surface a cd error; the shell now stays put silently. test_concurrent_hooks_log_labels_and_pass_cd_directive wrote a dummy CD target, so it now writes an existing directory.

Regression tests cover the -x case (test_switch_execute_that_removes_worktree_withdraws_cd) and the nested-hook case (test_directive_to_worktree_removed_by_later_step_is_withdrawn); both fail without the change.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 3 commits October 26, 2026 10:24
The shell wrapper acts on the cd directive only after wt exits, and work
that runs in between can remove the directory it names. `wt switch -x`
writes the directive before starting the program, so a program that
removes its own worktree (a dispatched agent session ending with
`wt remove`) left the wrapper to `cd` into a missing directory: the
shell printed a cd error and the wrapper returned failure. A hook or
alias whose nested wt switches to a worktree that a later step removes
does the same.

As wt finishes, it now reads the directive file and empties it when the
directory it names no longer exists, leaving the shell where it started.
The check runs in the outermost wt, after everything it runs in the
foreground, so it covers whoever wrote the directive.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Windows refuses to delete a directory a running process holds as its
cwd, so an --execute program there can't remove the worktree it runs in.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A valid worktree path ending in whitespace is mistaken for a missing target, so a successful wt switch leaves the shell in its original directory. I reproduced this with an existing worktree at feature ; the inline suggestion preserves the raw path.

Comment thread src/output/global.rs Resolved
The withdraw check trimmed all surrounding whitespace before testing the
directory, so a directive naming an existing directory that ends in a
space was taken for a missing one and withdrawn. Strip only the line
terminator, as bash and zsh do when they read the file.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 8ee0a70 into main Oct 26, 2026
40 checks passed
@max-sixty
max-sixty deleted the execute-removed-cd branch October 26, 2026 22:35
max-sixty added a commit that referenced this pull request Oct 27, 2026
wt wrote the `cd` target followed by a newline, and each reader removed
it its own way: fish, PowerShell and Nushell trimmed all surrounding
whitespace, bash and zsh dropped trailing newlines, and wt's existence
check from #4602 stripped line terminators. So a directory whose name
ends in a space worked in bash and zsh but not in the other three.

wt now writes the bare path, and every reader takes the contents as-is:
fish reads with `string collect`, PowerShell and Nushell drop their
trims, and wt's existence check strips nothing. I checked all five
wrappers `cd` into a directory named `dir ` (fish failed before).

A hand-written directive ending in a newline (a hook using `echo`) is
now read with the newline included; wt's own writes and the tests
already use the bare path.

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.

2 participants