worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

wt switch <branch> skips pre-start, so the suggested recovery from a failed setup reports success on an unusable worktree

Open ee-prog opened this issue 2 months ago • 2 comments

wt v0.77.0, macOS arm64 (Homebrew).

When a pre-start hook fails, wt switch -c exits non-zero but leaves the branch and a partially-made worktree behind. Re-running wt switch -c then refuses, and suggests dropping --create:

✗ Branch feat already exists
↳ To switch to the existing branch, run without --create: wt switch feat

Following that suggestion exits 0 and reports success, but pre-start never runs, so the worktree stays in whatever state the failed setup left it:

$ wt switch feat --no-cd
○ Switched to worktree for feat @ /tmp/repro.feat
$ echo $?
0

In our case pre-start is what installs node_modules, so the recovery the tool recommends hands you a worktree that looks ready and cannot build or test. The failure then surfaces much later as a module-not-found from whatever runs next.

Repro

mkdir repro && cd repro && git init -b main .
mkdir -p .config
printf '[[pre-start]]\ndeps = "exit 1"\n' > .config/wt.toml
git add -A && git commit -m init
wt config approvals add --yes
wt switch -c feat --no-cd     # exit 1; branch + worktree remain
wt switch feat --no-cd        # exit 0, "Switched to worktree"; pre-start does not run

Expected

Either wt switch runs pre-start for a worktree whose setup has not completed, or the "run without --create" hint points at the command that finishes it (wt -C <path> hook pre-start), which does work and is idempotent when hooks are written to be.

A marker on the worktree recording whether pre-start completed would let switch decide without re-running hooks on every switch.

ee-prog avatar Sep 16 '26 18:09 ee-prog

Reproduced on main (da03501), exactly as written — including the part that makes this more than a missing hint: wt switch feat behaves differently depending on whether a worktree already exists, and the hint says the same thing either way.

With no worktree for the branch, wt switch <branch> creates one and does run pre-start:

$ wt switch other --no-cd
◎ Running pre-start project:deps @ ~/repro.other
  echo RAN-PRESTART
RAN-PRESTART
✓ Created worktree for other @ ~/repro.other

With a worktree already there — the state a failed pre-start leaves — it only moves:

$ wt switch feat --no-cd
○ Switched to worktree for feat @ ~/repro.feat

That split is structural, not incidental: switch_post_hook_types selects pre-start + post-start + post-switch for a create and post-switch alone otherwise. So "To switch to the existing branch, run without --create" is promising one of two materially different outcomes, and the one you get is the one that skips setup.

I haven't opened a PR, because both remedies you list are product calls rather than defects with a single correct fix. What the code does settle:

Your second option is cheaper than it looks, but only half of it. BranchAlreadyExists is constructed with repo in hand, and the lookup it would need, repo.worktree_for_branch(), already appears a few lines above under the non---create guard. So distinguishing the two outcomes in that hint needs no new state at all. What it still can't say is whether setup finished — only which of the two switches you're about to get.

The marker is a larger surface than one flag. Per-branch authoritative state already exists (worktrunk.state.<branch>.* in git config), and the state module spec requires every new category to appear in both wt config state get and wt config state clear plus their after_long_help blocks — so it lands as a documented user-facing concept. The behavioural questions a maintainer would have to settle first: does --no-hooks count as complete; what happens when the hook set changes after a worktree was marked complete; is a failed hook incomplete forever or until the user overrides; and does switch then re-run automatically or refuse with a hint. Relevant to deriving it instead of storing it: the only durable record of the failed run today is .git/wt/logs/commands.jsonl, whose entries carry cmd, exit, label, ts, wt and no branch — #3699 asks for exactly that missing key.

In the meantime wt -C <path> hook pre-start does work as the completion step — it re-runs the project's pre-start commands against the existing worktree and exits non-zero when they fail, so it composes into a script. Worth knowing that the other hint in this flow (To skip pre-start hooks, re-run with --no-hooks) is under discussion in #4142, so the two hint lines in your repro are both live questions.

Which of the two remedies to take is a maintainer's call, not mine.

worktrunk-bot avatar Sep 16 '26 19:09 worktrunk-bot

@worktrunk-bot please open a PR that prints the recovery hint where pre-start fails during wt switch --create, such as "To retry setup, run wt hook pre-start", since that's the one moment we know setup failed. #4172's hint at the later collision fired on every ordinary branch conflict.

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

max-sixty avatar Nov 05 '26 05:11 max-sixty