Repository navigation
Conversation
…egistered wt switch <branch> (without --create) only runs pre-start/post-start hooks when it has to register a new worktree. A branch whose worktree is already registered takes the existing-worktree path and only runs post-switch. So if an earlier --create failed partway through pre-start, the branch and a partial worktree registration are both left behind, and the BranchAlreadyExists hint's suggested recovery (switch without --create) reports success without finishing that setup. BranchAlreadyExists now carries the already-registered worktree path, if any, and its hint names the idempotent command that does finish setup (wt -C <path> hook pre-start) alongside the existing switch suggestion.
worktrunk-bot
left a comment
There was a problem hiding this comment.
The mechanism is right and the snapshot test pins it; reusing worktree_for_branch and suggest_command_in_dir rather than open-coding either is the right call. Three things to weigh before it lands.
The clause fires on the ordinary collision, not the failed-setup one. existing_worktree is Some whenever a worktree is registered for the branch — which is the normal state of any branch you've already switched to, not evidence that setup failed. The snapshots in this PR show it: switch_create_existing_error, switch_create_existing_with_execute, switch_create_existing_with_execute_metachars, and the shell-wrapper command_failure test all pick up the recovery aside, and in none of them did a hook fail. So wt switch -c <branch> on a branch you already have a worktree for — a routine typo — now carries a ~60-character conditional that almost never applies, while the case it's for is the rare one. The PR body calls that "occasionally superfluous"; it's the other way round. Worth a maintainer's call, since CLAUDE.md's "Consolidate or remove existing prose and machinery before adding another explanation, mode, or special case" points the other way.
"safe to re-run" is a promise wt can't make. pre-start runs whatever the project config names. Re-running npm install is harmless; re-running cp .env.example .env over an edited file, or a git apply, is not. #4143 qualified it — "idempotent when hooks are written to be" — and the hint drops the qualifier, so it reads as wt vouching for commands it doesn't control.
The command no longer ends the line. The writing-user-outputs skill is explicit — "End with the command for easy copying", with // BAD - command not at end (hard to copy) as the counter-example — and for two suggestions in one hint, "put the more commonly needed command last for easy terminal copying". wt switch feat now sits mid-line behind a parenthetical. The suggestion below uses the semicolon form that skill documents, which also restores the ordering and drops the unqualified promise; it needs cargo insta test --accept for the four snapshots.
That last one is mechanical either way. The first is the one I'd want decided before the rest matters — if the aside shouldn't ride on every collision, the alternative is gating it on something that actually distinguishes a half-finished worktree, which is the persistent marker #4143 raises and this PR reasonably declines to build.
…very command last
…#4705) When `pre-start` fails during `wt switch --create`, the error now names the command that retries setup, with the same variables the switch used: ``` ✗ pre-start project:env failed (exit code 1) ↳ To retry setup, run wt -C ~/code/myproject.feature-auth hook pre-start --base-worktree-path=/home/me/code/myproject ``` The failed switch leaves the new branch and worktree in place, and a later `wt switch <branch>` only runs `post-switch` because the worktree already exists, so nothing re-runs setup unless the user knows to. This failure is the one point where wt knows setup didn't finish, so the hint goes here instead of on the later `Branch already exists` collision (#4172 put it there, so it showed on every ordinary branch conflict). At this site the hint replaces the `--no-hooks` suggestion. Re-running `wt switch --create --no-hooks` would stop at `Branch … already exists`, which is the trap #4143 reported. Every other hook failure still shows the `--no-hooks` hint as before. A manual `wt hook pre-start` binds `base` and `base_worktree_path` to the worktree it runs in and knows no PR. So the retry passes the switch's own `--base`, `--base-worktree-path`, `--pr-number`, and `--pr-url`, but only those the frozen pre-start templates reference, because `wt hook` forwards an unreferenced `--KEY=VALUE` into `{{ args }}`. With that, a hook like `cp {{ base_worktree_path }}/.env .` reruns the same setup. A hook that references none of them gets a bare `wt -C <path> hook pre-start`. Verified by: - `test_user_pre_start_failure_retry_reruns_same_setup`: a hook copies a file from `{{ base_worktree_path }}` and fails; running the hinted command afterwards copies from the original worktree. Without the flag, the manual run would read from the new worktree. - A unit test of the flag selection, including shell quoting. - The inline error-render snapshot and the two `pre-start` failure integration snapshots. Closes #4143 --------- Co-authored-by: worktrunk-bot <[email protected]>
Closes #4143
wt switch without --create only runs pre-start/post-start hooks when it has to
register a new worktree (the SwitchPlan::Create path in plan_switch). A branch whose worktree
is already registered takes the SwitchPlan::Existing path instead, which only runs
post-switch.
So when wt switch -c fails partway through pre-start, the branch and a partial
worktree registration both stick around. The BranchAlreadyExists error's hint then suggests
re-running without --create, but that switch takes the Existing path, reports success, and
never re-runs pre-start. The recovery the tool recommends hands back a worktree that looks
ready and isn't.
GitError::BranchAlreadyExists now carries existing_worktree: Option, the worktree
path already registered for the branch if any, looked up at the one place the error is
raised (resolve_switch_target in src/commands/worktree/switch.rs, via the existing
Repository::worktree_for_branch helper). When it's Some(path), the hint names the recovery
command from the issue's own suggested fix:
This is the more conservative of the issue's two suggested fixes. There's no persistent
marker recording whether a worktree's pre-start ever completed (the issue's other
suggestion), so the code can't tell "this worktree's setup failed" from "this worktree has
always been fine", only "a worktree is registered for this branch or not." The hint is worded
to not presume failure, so it stays accurate, just occasionally superfluous, in the common
case where the branch's worktree is already healthy. A real completion marker would be a
bigger, separate design decision that felt out of scope for closing this specific gap.
Review from the repo's own bot after the first push caught two real things and one design
question I left open on purpose:
That's not something this tool can promise, pre-start runs whatever the project config
names, and re-running something like a file copy over an edited file is not always
harmless. Dropped the claim.
harder to copy from a terminal. Reordered so the plain switch command (the common case)
ends the line, per this repo's own writing-user-outputs convention.
since that's the far more common case than a failed setup, not just the rare one. I didn't
change the trigger condition, that's a design call the maintainer should make, not
something to decide unilaterally in this PR.
Tested:
confirming the hint text via cargo insta.
pop) to separate this change's effect from this machine's pre-existing baseline. With
--features shell-integration-tests: 2273 passed / 86 failed both before and after, same
numbers exactly, only the intended snapshot lines differ (confirmed via cargo insta's diff
view before accepting each one, four snapshots total: the three switch.rs
test_switch_create_existing_* tests, all in the same fixture shape, plus one
shell-integration-tests-gated command_failure test).
to fish/nu/pwsh not being installed on this sandbox, plus one credential.helper leak from
this host's real gitconfig) is present identically before and after, confirmed by the same
stash/pop comparison.