Skip to content

fix(output): name the destination when a removal deletes the caller's directory - #4163

Merged
max-sixty merged 2 commits into
mainfrom
fix/issue-4162
Sep 17, 2026
Merged

max-sixty merged 2 commits into
mainfrom
fix/issue-4162

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

wt merge and wt remove delete the worktree the caller is standing in, then
print a bare ▲ Cannot change directory — shell integration not installed. No
line of that run names a path, and --format=json carries only the target
branch — so a caller without shell integration is left in a deleted directory
with nothing to cd to.

It is the only cd warning that withholds the destination. wt switch to an
existing worktree names it inside the same warning
(Worktree for main @ ~/code/myproject, but cannot change directory — …), and
wt switch --create names it in the success line directly above. The removal
case, which is the one that actually strands the caller, named nothing.

The stranding is not theoretical: the recovery wt itself suggests for an
externally-deleted worktree, wt switch ^, does not work after wt's own clean
removal. The removal prunes the admin entry that was_worktree_of
(src/git/recover.rs:194) matches on, and in the default sibling layout the
deleted path is not a prefix of any surviving worktree, so recovery declines
the repository and the command fails with
fatal: Unable to read current working directory. An rm -rf of the same
worktree leaves a prunable entry and recovers fine — which is what the
remove_dir_all-based unit tests in recover.rs simulate.

Solution

print_switch_message_if_changed now uses the switch-to-existing shape it
already had the pieces for — it computes path_display and dest_branch and
was printing them only on the shell-integration-active path:

▲ Worktree for main @ ~/code/myproject, but cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install

The git wt branch of the same function gets the destination too, so all four
rows of the "After Merge/Remove" table in the output::shell_integration spec
match the switch-to-existing table. That spec docstring is updated with the
new wording and with why this section names a path where --create does not.

This re-texts 54 merge/remove/prune/hook snapshots; no other behavior changes.

It does not make wt switch ^ recover after a clean removal — that is the
deeper half of #4162 and turns on whether the strict was_worktree_of
predicate (tightened for the false match in #3552) should accept a path
matched against the user's worktree-path template, per the idea in #1109 /
#1168. That is a design call, so this PR does the part that un-strands the
caller either way.

Testing

test_merge_cwd_removed_warning_names_destination merges from inside the
worktree the merge removes and asserts the cd warning names a destination
ending in the primary worktree's directory. Before the change it fails on the
bare message; after it passes. cargo test --test integration is otherwise
unchanged apart from the re-texted snapshots.

Closes #4162 — automated triage

… directory

wt merge and wt remove printed a bare "Cannot change directory —
{reason}" when shell integration was inactive. Every other cd warning
puts a path in front of the reader: wt switch to an existing worktree
names it in the same warning, and wt switch --create names it in the
success line directly above. A removal named none, and it is the one
case that has just deleted the directory the caller is standing in.

Use the switch-to-existing shape here too, so the caller has somewhere
to cd back to.

Closes #4162

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Self-review. The behavior change is right and minimal — the two warning strings
now carry the destination the shell-integration-active line already printed, and
the early-return structure is untouched. Two rationale claims in the new prose
are overstated, though, and one pre-existing statement of the old format is now
the only wrong one left in the file. I'm pushing both prose fixes.

The "no other line prints a path" claim is false when a post-merge /
post-remove hook is configured.
The new doc on
print_switch_message_if_changed, the After Merge/Remove section of the
output::shell_integration spec, and the new test's doc comment all assert it
absolutely. post_hook_display_path (src/output/global.rs) returns the
destination whenever shell integration is not active — precisely the stranding
case — so the hook announcement names it. This PR's own
merge_cwd_removed_hint_no_recovery.snap shows the line:
◎ Running post-merge: project @ _REPO_, two lines below the warning. The
change is still the right one (the default hook-free run prints nothing), but
stated as an invariant the claim is one a later change could lean on and be
wrong.

handle_switch_output's doc still states the old format for both switch
results.
It says **Warning format:** Cannot change directory — {reason} and
scopes that to "both Existing and Created", but the Existing branch has
printed Worktree for X @ path, but cannot change directory — {reason} since
before this PR (search handle_switch_existing_output). It's outside the diff
and predates it, but this PR makes the removal path the third caller of the
long shape, leaving that block as the file's only remaining description of the
short one.

Nothing else: no new deletion or widened one (no data-safety hold), the 54
re-texted snapshots are exactly the merge/remove/prune sites, and the six
--create snapshots that keep the bare warning are correct to — their success
line already names the path.

…ee run

The new rationale on the removal cd warning asserted that no other line of a
`wt merge` / `wt remove` run prints a path to move to. `post_hook_display_path`
returns the destination whenever shell integration is inactive — exactly the
stranding case — so a configured post-merge / post-remove hook announcement
names it, as `merge_cwd_removed_hint_no_recovery.snap` shows. Scope the claim
in all three places that make it.

Also fix `handle_switch_output`'s warning-format doc, which described both
switch results with the bare `Cannot change directory` shape; `Existing` has
printed the long `Worktree for X @ path, but …` shape since before this branch.
@max-sixty
max-sixty merged commit 386e57b into main Sep 17, 2026
36 of 37 checks passed
@max-sixty
max-sixty deleted the fix/issue-4162 branch September 17, 2026 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleted-CWD recovery fails when the worktree was removed by wt merge/wt remove — works for external deletion

2 participants