Repository navigation
fix: don't cd into a directory removed before wt exits - #4602
Merged
Merged
Conversation
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
left a comment
Collaborator
There was a problem hiding this comment.
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.
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]>
worktrunk-bot
approved these changes
Oct 26, 2026
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]>
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.
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 -xwrites the directive before starting the program, so a program that removes its own worktree (an agent session that ends withwt remove) left the wrapper tocdinto a missing directory: fish printedcd: The directory '…' does not existand the wrapper returned failure. A hook or alias whose nestedwt switchtargets a worktree that a later step removes hits the same thing.As wt finishes,
finish_commandnow 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-xprogram, 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 owncderror 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
cderror; the shell now stays put silently.test_concurrent_hooks_log_labels_and_pass_cd_directivewrote a dummyCDtarget, so it now writes an existing directory.Regression tests cover the
-xcase (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.