Repository navigation
fix(switch): resolve symbolic target (-, @, ^) before pre-switch hooks - #2310
Merged
Merged
Conversation
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]>
This was referenced Aug 28, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withundefined value, blocking the switch.From #2309:
Root cause:
run_pre_switch_hooksinsrc/commands/handle_switch.rspasses the rawtarget_branch(-) into the hook context andworktree_for_branch. There is no branch literally named-, so the Active-override branch is skipped andtarget_worktree_pathis never set. Symbolic resolution happens later inplan_switch, after pre-switch hooks have already run.Solution
Resolve
-/@/^viaRepository::resolve_worktree_namebefore constructing the pre-switch hook context, then feed the resolved name into bothCommandContextandworktree_for_branch. If resolution fails (e.g., no previous branch in history), fall through to the raw argument soplan_switchsurfaces its canonical error — no new error path.Testing
test_pre_switch_vars_with_dash_shortcutintests/integration_tests/user_hooks.rsestablishes 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 afterwt switch -. Fails onmain, passes with the fix.shell_wrapper::*::case_4failures reproduce onmain— nushell is not installed in this sandbox.Closes #2309 — automated triage