Skip to content

fix(switch): resolve a worktree path for pre-switch hook variables - #4493

Merged
max-sixty merged 1 commit into
mainfrom
fix/issue-4492
Oct 17, 2026
Merged

max-sixty merged 1 commit into
mainfrom
fix/issue-4492

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

run_pre_switch_hooks found the destination with worktree_for_branch, so it only matched a branch name. A worktree path, which is what the interactive picker passes, matched nothing. branch and target then held the raw path, while worktree_path, worktree_name, upstream, and commit described the source worktree. The hook now runs the same resolve_selector ladder that plan_switch uses: branch first, then the argument as a worktree path, and branch-only under --create. With that change, wt switch ../repo.feature renders the same variables as wt switch feature. A detached worktree can only be named by path. For one, branch and target stay unset, matching #4009, and commit comes from the destination's HEAD instead of the source's.

test_pre_switch_vars_with_path_argument reproduces the report: on main it renders branch=<path> … wt_name=repo target_wt= commit=, and it passes with this change. test_pre_switch_vars_with_detached_path_argument covers the detached case. The existing switch, picker, and user-hook integration tests still pass (474), and clippy is clean.

Closes #4492 — automated triage

run_pre_switch_hooks looked the destination up by branch name only, so a
worktree path argument — what the picker passes — left branch/target as
the raw path and worktree_path, worktree_name, upstream, and commit
describing the source. It now runs the same resolve_selector ladder as
plan_switch, honoring --create's branch-only reading. A detached
destination leaves branch/target unset and takes commit from its own HEAD.

Closes #4492
@worktrunk-bot worktrunk-bot added the automated-fix Automated CI fix label Oct 17, 2026
@max-sixty
max-sixty merged commit d14f217 into main Oct 17, 2026
37 of 47 checks passed
@max-sixty
max-sixty deleted the fix/issue-4492 branch October 17, 2026 19:50
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot can you survey our handling of paths vs branches; evaluate:

  • how we pass these through; we should be resolving at a boundary and then passing a canonical shape through (probably the path)
  • how consistently we do this across commands
  • any remaining bugs we have in this area

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Summary: the boundary already exists and most commands use it. Repository::resolve_worktree / resolve_selector (worktrees.rs#L501-L567) turns a typed token into ResolvedWorktree { path, branch }. The gaps are in two places. Some callers narrow that result to just a branch or just a path and then look the other half up again. And wt switch resolves twice. I reproduced two bugs from these gaps on main at d14f217.

Bugs

1. A pre-switch hook sees origin/<branch> while the switch lands on <branch>. plan_switch strips a remote prefix when no local branch matches (switch.rs#L632-L642). run_pre_switch_hooks builds its own selector and skips that step (switch.rs#L1600-L1624). Reproduced with wt switch origin/feature, where feature already has a worktree:

PRE branch=origin/feature target=origin/feature target_wt=
▲ Worktree for feature @ …/repo.feature, …

wt switch origin/remote-only has the same mismatch: the hook gets origin/remote-only, and the command creates remote-only. This is the same kind of bug as #4492, just on a step #4493 didn't cover. The plan_switch comment says the picker passes remote names in this form; I only reproduced it from the CLI.

2. wt step promote <path> promotes the wrong worktree when a branch is checked out twice, and leaves both worktrees detached. resolve_target_branch turns the path into a branch with require_selected_branch. It then finds the worktree again by branch name, taking the first match (promote.rs#L348-L354). Setup: dup checked out in A and, via git worktree add --force, in B. Running wt step promote …/B operated on A and failed partway through:

✗ branch exchange failed at: switch main
  fatal: 'dup' is already used by worktree at '…/B'

After that, both the main worktree and A were on detached HEAD. Worktrunk never creates duplicate checkouts, but AGENTS.md says a path names exactly that case. Promote can't exchange a branch that is checked out somewhere else anyway, so it should refuse up front when the branch has more than one checkout, whichever spelling was used.

Consistency across commands

Command Resolves via Passes on
wt remove resolve_worktree RemoveTarget::WorktreePath, deduped by WorktreeId
wt merge --branch, step commit --branch CommandEnv::for_selector → require_worktree path; branch read back by path
step diff --branch, copy-ignored --from/--to, relocate require_worktree path
config state … --branch require_selected_branch branch (state is keyed by branch, so this is right)
step promote require_selected_branch branch, then looked up again by name (bug 2)
wt switch expand_selector + remote strip + resolve_selector in plan_switch; a separate copy in run_pre_switch_hooks SwitchPlan with path + branch; the hook copy differs (bug 1)
wt switch --base its own order: ref, then unique remote, then worktree path branch (fine, since a base is a ref)
picker turns the row into an absolute path string, which switch resolves again works, but it's a round trip

wt remove is the model to copy. It resolves once, dedupes on identity, and acts on the path from then on.

Structural suggestions

  • Resolve once in wt switch. Move resolve_switch_target and resolve_selector ahead of the pre-switch hooks, and give both the hook and plan_switch that one result instead of the raw identifier. That fixes bug 1 and makes it impossible to repeat. --base resolution sits in the same function and can hit the forge, so it may need splitting out. offer_bare_repo_worktree_path_fix also reads the raw identifier: given a path, its example worktree path is built from the sanitized path. That's cosmetic, and resolving first fixes it too.
  • Return the whole resolution from the narrowing helpers. require_worktree drops the branch, so for_selector looks it up again by path (correct, but done twice). require_selected_branch drops the path, and that is where promote goes wrong. A require_worktree that returns (path, Option<branch>), or the ResolvedWorktree::Worktree variant itself, would let promote and for_selector use what was resolved. require_selected_branch would then be left only for callers whose state really is keyed by branch.
  • The picker could hand switch the resolved target directly instead of a path string. It works today because an absolute path can't be a valid branch name, so the branch-first lookup always misses. That's low priority.

Smaller observation: copy-ignored --from <path> uses the raw argument as the trace label for its git ls-files call (copy_ignored.rs#L70). It only shows up in -vv logs.

I can open fix PRs for bugs 1 and 2: switch resolving once, and promote resolving to a worktree and refusing duplicate checkouts. Say if you want them split or combined with the helper changes.

@max-sixty

Copy link
Copy Markdown
Owner

OK will fix those on my end

max-sixty added a commit that referenced this pull request Oct 18, 2026
…hrough (#4496)

Follow-up to the survey on #4493. Several commands resolved a worktree
argument, kept only the branch or only the path, and looked the other
half up again. Each re-lookup could disagree with the first. This
resolves the argument once and passes the resolved worktree through.

| Case | Before | After |
|---|---|---|
| `wt switch origin/feature` (`feature` has a worktree) | pre-switch
hook got `branch=origin/feature` and no `target_worktree_path` | hook
gets `branch=feature` and the worktree path |
| `wt step promote <second copy>` of a branch checked out twice | acted
on the first copy, failed partway, left the main worktree and that copy
detached | refuses before touching anything, listing both checkouts |
| the same with no argument from the second copy, or with the default
branch checked out twice | same partial failure | same refusal |
| `wt switch @` from a second copy or a detached worktree | landed in
the first copy, or errored | `already_at` the current worktree |
| bare-repo worktree-path warning for `@` / `-` / `pr:N` | skipped
through a hand-kept list of shortcuts | names the branch the shortcut
resolves to; the list is gone |

### `wt switch`

`SwitchTarget` holds the resolved destination (an existing worktree, or
a branch to create one for) plus the forge's answer for `pr:`/`mr:`. The
bare-repo warning, the pre-switch hooks, and `plan_switch` all read it,
so the hooks no longer run their own copy of resolution. Arguments other
than `pr:`/`mr:` and `--create` names go through
`Repository::resolve_worktree`, which is what makes `@` resolve by path;
the remote-prefix strip applies to a `BranchOnly` result.

A pre-switch hook can change what the argument names: the fetch-if-stale
hook the docs suggest makes a remote-only branch appear. So when a hook
actually ran (`execute_hook` now reports whether one did), the pipeline
locates the argument again through the same `SwitchTarget::locate`, on a
fresh `Repository`, and plans against that. The forge is not queried
twice. `--base` and `--create` validation still run after the hooks.

Two behaviour changes follow. Naming errors (`wt switch -` with no
history, a path holding no worktree) now fail before the pre-switch
hooks run rather than after. And when a hook ran, the rest of the switch
reads repository state as the hook left it, `.config/wt.toml` included.

### Promote and the other `--branch` callers

`Repository::require_worktree` returns the branch along with the path,
so `CommandEnv::for_selector`, `copy-ignored`, and promote use the
resolved pair instead of looking one up from the other. Promote resolves
every route (argument, `@`, `^`) to a worktree and refuses when either
branch is checked out more than once.

### Not covered

`wt switch -` still records the previous location as a branch name, so
it can't return to the second copy of a duplicated branch or to a
detached worktree. Changing what the history records is a separate
change.

### Testing

New integration tests cover each row above, plus a pre-switch hook that
fetches, with both `late` and `origin/late`. The hook-fetch tests also
pass on `main`; they pin the behaviour that resolving before the hooks
could break. Every other new test fails on `main`. The full pre-merge
gate passes locally. Codex and a separate Claude agent reviewed it, the
latter twice.

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.

pre-switch hook variables resolve to the source worktree when switching by path (and from the picker)

2 participants