Repository navigation
fix(output): name the destination when a removal deletes the caller's directory - #4163
Conversation
… 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
left a comment
There was a problem hiding this comment.
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.
Problem
wt mergeandwt removedelete the worktree the caller is standing in, thenprint a bare
▲ Cannot change directory — shell integration not installed. Noline of that run names a path, and
--format=jsoncarries only the targetbranch — so a caller without shell integration is left in a deleted directory
with nothing to
cdto.It is the only cd warning that withholds the destination.
wt switchto anexisting worktree names it inside the same warning
(
Worktree for main @ ~/code/myproject, but cannot change directory — …), andwt switch --createnames it in the success line directly above. The removalcase, 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 cleanremoval. The removal prunes the admin entry that
was_worktree_of(
src/git/recover.rs:194) matches on, and in the default sibling layout thedeleted 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. Anrm -rfof the sameworktree leaves a prunable entry and recovers fine — which is what the
remove_dir_all-based unit tests inrecover.rssimulate.Solution
print_switch_message_if_changednow uses the switch-to-existing shape italready had the pieces for — it computes
path_displayanddest_branchandwas printing them only on the shell-integration-active path:
The
git wtbranch of the same function gets the destination too, so all fourrows of the "After Merge/Remove" table in the
output::shell_integrationspecmatch the switch-to-existing table. That spec docstring is updated with the
new wording and with why this section names a path where
--createdoes 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 thedeeper half of #4162 and turns on whether the strict
was_worktree_ofpredicate (tightened for the false match in #3552) should accept a path
matched against the user's
worktree-pathtemplate, 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_destinationmerges from inside theworktree 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 integrationis otherwiseunchanged apart from the re-texted snapshots.
Closes #4162 — automated triage