Skip to content

wt switch interactive picker with cd = false skips switch hooks #2837

Closed

Description

@worktrunk-bot

Bug

With [switch] cd = false (or --no-cd), the interactive picker prints the selected branch and exits without firing any switch hooks. The same wt switch <branch> --no-cd invocation with a branch arg fires hooks normally, so the picker path is inconsistent with the direct-arg path.

Surfaced in #2796 — @endigma's working cmux recipe needs a wt pick alias that runs the picker twice to force hooks to fire:

[aliases]
pick = """
b=$(wt switch)
[ -n "$b" ] && wt switch "$b"
"""

Reproduce

wt.toml:

[switch]
cd = false

[pre-switch]
trace = "echo pre-switch fired for {{ branch }} >&2"
  • wt switch other-branch → prints pre-switch fired for other-branch ✓
  • wt switch (picker, select other-branch) → prints other-branch, no hook output ✗

post-switch, pre-start, post-start are also skipped on the picker path.

Cause

src/commands/picker/mod.rs:767-777 early-returns on !change_dir, before run_pre_switch_hooks / plan_switch / execute_switch / the background-hook spawn that the change_dir = true branch flows into.

Comment on the branch says "read-only, no side effects" — that was the intent when #1330 added --no-cd support to the picker, but the same shape later bit #1704 (alt-r removal in the picker skipping pre-remove / post-remove).

Constraint

Pre-switch hooks can't run while skim still owns the terminal — they'd corrupt the picker frame and have nowhere to prompt. So the fix isn't "run hooks before the early-return"; it's something like "tear down skim first, then run the normal switch pipeline with change_dir = false". That removes the early-return and lets the picker share the same execute_switch path as the direct-arg case.

Workaround

The wt pick alias above. The double-invocation is the cost of routing the picker's output through the CLI a second time so it hits the non-picker code path.

Activity

  1. added
    bug
    on May 20, 2026
  2. worktrunk-bot commented on May 20, 2026

    @worktrunk-bot
    CollaboratorAuthor

    Verified the analysis: the early-return at src/commands/picker/mod.rs:767-777 does skip the entire hook + execute pipeline that the change_dir = true branch flows into (run_pre_switch_hooks → plan_switch → execute_switch → background hooks).

    One missing piece of context: the no-hooks behavior on the picker --no-cd path was an explicit design decision in #1445 ("When wt switch --no-cd opens the interactive picker (no branch argument), selecting a branch prints its name to stdout and exits — no switching, no cd directive, no hooks"), and test_switch_picker_no_cd_prints_branch_without_switching in tests/integration_tests/switch_picker.rs locks that contract in. So the fix is a contract change, not just a missing call: any patch that makes hooks fire needs to revisit #1445's intent and rework that test, and reason about what alt-c (create) with cd = false should do — currently it just prints "create" and no-ops, but a unified pipeline would actually create the worktree.

    Skipping a code change — the print-only-vs-fire-hooks-then-print question is a design call for a maintainer. The wt pick alias in the cmux recipe (#2836) is a reasonable workaround in the meantime.

  3. max-sixty commented on May 20, 2026

    @max-sixty
    Owner

    OK, I think we probably should run the switch hooks, even though we're not cd-ing?

    @endigma do you agree? or is the existing state what you would expect?

  4. endigma commented on May 20, 2026

    @endigma
    Contributor

    I would expect the interactive picker to work identically to when providing the branch as an argument, which is what the dumb pick alias is hacking it to do so it works

  5. max-sixty commented on May 20, 2026

    @max-sixty
    Owner

    oh, this is in a worse state than I realized, sorry; seems that picker + --no-cd doesn't do the things it should, because of #1445. Let me find a way of fixing while allowing something to get the branch name

  6. added a commit that references this issue on May 21, 2026
    ff4e481
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions