Skip to content

fix(switch): resolve symbolic target (-, @, ^) before pre-switch hooks - #2310

Merged
max-sixty merged 1 commit into
mainfrom
fix/issue-2309
Apr 20, 2026
Merged

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

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

When wt switch - (or @/^) triggers a pre-switch hook, template variables point at the source worktree instead of the destination, and {{ target_worktree_path }} is undefined entirely — hooks that reference it fail with undefined value, blocking the switch.

From #2309:

worktree_name = bar            # expected: foo (destination)
base = bar
target = -                     # raw argument, not resolved
worktree_path = ...worktrees/bar   # expected: ...worktrees/foo
base_worktree_path = ...worktrees/bar
target_worktree_path = (not available with 'switch -')

Root cause: run_pre_switch_hooks in src/commands/handle_switch.rs passes the raw target_branch (-) into the hook context and worktree_for_branch. There is no branch literally named -, so the Active-override branch is skipped and target_worktree_path is never set. Symbolic resolution happens later in plan_switch, after pre-switch hooks have already run.

Solution

Resolve -/@/^ via Repository::resolve_worktree_name before constructing the pre-switch hook context, then feed the resolved name into both CommandContext and worktree_for_branch. If resolution fails (e.g., no previous branch in history), fall through to the raw argument so plan_switch surfaces its canonical error — no new error path.

Testing

  • New regression test test_pre_switch_vars_with_dash_shortcut in tests/integration_tests/user_hooks.rs establishes switch history, installs a pre-switch hook that references {{ target }}, {{ target_worktree_path }}, {{ worktree_path }}, and {{ worktree_name }}, and asserts all four reflect the actual destination after wt switch -. Fails on main, passes with the fix.
  • All existing pre-switch tests pass (7/7).
  • Full non-nushell integration suite (1586 tests) passes; the 10 shell_wrapper::*::case_4 failures reproduce on main — nushell is not installed in this sandbox.

Closes #2309 — automated triage

Pre-switch hooks used the raw target argument when building template
vars, so `wt switch -` left `{{ target_worktree_path }}` undefined and
kept `{{ target }}`/`{{ worktree_path }}`/`{{ worktree_name }}` pointing
at the source. Hooks that reference `{{ target_worktree_path }}` failed
with "undefined value" errors, blocking the switch entirely.

Resolve `-`/`@`/`^` via `resolve_worktree_name` before constructing the
hook context. If resolution fails (e.g., no previous branch), fall
through to the raw argument so `plan_switch` surfaces its canonical
error.

Closes #2309

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit d97a6c4 into main Apr 20, 2026
23 checks passed
@max-sixty
max-sixty deleted the fix/issue-2309 branch April 20, 2026 02:31
max-sixty pushed a commit that referenced this pull request Aug 28, 2026
…ame-repo PRs (#3941)

Fixes #3934.

`wt switch pr:<n>` resolved the PR through the forge inside
`plan_switch`, which runs *after* `pre-switch` — so the hook got the raw
`pr:3933` token as `branch` and `target`, and no `target_worktree_path`
even when the PR's branch was already checked out. Separately,
`pr_number` / `pr_url` were read back off `CreationMethod::ForkRef`,
which a same-repo PR never produces, so the reporter's output shows both
unset in *every* hook although the help text promises them. This
resolves `pr:`/`mr:` before the hooks (the rule `-`/`@`/`^` already
follow, #2310) and gives the PR identity its own field, carried from
resolution through the plan into `pre-switch`, `pre-start`,
`post-start`, and `post-switch` alike.

Verified with two integration tests that fail on `main` (the
`pre-switch` hook aborts the switch with `undefined value`) and pass
here; full unit + integration suites, clippy `--all-targets
--all-features`, rustdoc, and pre-commit are green locally.

<details><summary>What each variable does now</summary>

For `wt switch pr:3933` where the PR's branch is
`feat/issue-3922-list-sort`:

| Variable | Before | After |
|---|---|---|
| `branch` / `target` in `pre-switch` | `pr:3933` |
`feat/issue-3922-list-sort` |
| `target_worktree_path` in `pre-switch` | unset even when that branch
had a worktree | set when it does |
| `pr_number` / `pr_url` (same-repo PR, all hooks) | unset | `3933` /
the PR URL |
| `pr_number` / `pr_url` in `pre-switch` (fork PR) | unset | set |

`worktree_path` in `pre-switch` is deliberately unchanged: on a switch
that *creates* a worktree there is no destination directory yet, so it
stays on the source. The help text claimed it was "the destination"
without that caveat —
[`src/cli/mod.rs`](https://github.com/max-sixty/worktrunk/blob/4c5e87eff3ebec31cdf6be1ad905bfda0bceeee8/src/cli/mod.rs#L1704)
now says which case is which and points at `pre-start` for work that
needs the new worktree (which is what the reporter's own `mise-trust`
hook already does).

</details>

<details><summary>Ordering consequences</summary>

The forge lookup and its `git fetch` now happen before `pre-switch`
runs, so a hook that aborts the switch aborts it after the PR was
fetched. That is inherent to the fix — the branch name is the forge's
answer. The lookup is still bounded to the argument form that asked for
it (`resolve_ref_shortcut_target` returns `None` for anything that isn't
`pr:`/`mr:`), and the resolved target is threaded into `plan_switch`, so
the forge is queried exactly once as before.

The hook-approval gate used to be what surfaced a malformed
`.config/wt.toml` before provider selection —
`configured_forge_platform` reports an unparsable config as "unset",
which would route an intended `forge.platform` override to the wrong
CLI. With the resolution now first, that config load is explicit at the
top of `resolve_ref_shortcut_target` rather than an accident of ordering
(`test_switch_pr_malformed_project_config_bails_before_provider_selection`
covers it and caught the regression).

`CreationMethod::ForkRef::ref_url` became dead once the identity moved
to its own field, so it is removed rather than left with an `#[allow]`.
`SwitchResult::Created`'s `pr_number` / `pr_url` are removed for the
same reason: the pipeline applies the identity from the resolved
argument, which is the only channel that also serves a switch onto an
existing worktree.

</details>

---------

Co-authored-by: worktrunk-bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent/missing variables in pre-switch hook triggered by wt switch -

2 participants