Cut `wt switch --execute` over to an argv input model (B2 Unit 2)
Background
wt switch -x/--execute <cmd> currently treats its payload as a shell command string. With shell integration active, wt writes that string to the EXEC directive file and the shell wrapper evaluates it — bash/zsh source, fish eval, nushell ^sh -c, PowerShell Invoke-Expression. Because the payload is shell text, wt must escape it, and the escaping has to match whichever shell evaluates it.
That escaping is the bug source. #2815 partially fixed PowerShell mis-quoting; #2843 ("B1") made the escaper fully shell-aware (POSIX / PowerShell / fish), keyed on WORKTRUNK_SHELL. B1 is a correctness fix but it only covers more dialects — it does not remove the bug class.
The plan (B2)
Stop treating -x as a shell string. -x becomes a program name; the -- trailing args become verbatim argv elements; there is no implicit shell. The EXEC directive file becomes NUL-separated argv data that every wrapper splits and execs identically. No shell evaluates the payload, so there is no quoting dialect and no escaping anywhere — the entire bug class is gone.
This is a breaking change to a protected CLI interface (CLI flags). The documented -x 'code …' / -x 'tmux …' forms, and every -x value using &&, $VAR, globs, or pipelines, stop working. Migration is -x <prog> -- <args>, or -x sh -- -c '<snippet>' to opt back into a shell explicitly.
Locked decisions
- Input model — argv.
-xprogram + verbatim--argv; no implicit shell. EXEC file =program\0arg1\0arg2. -xresolution — strictexecvp.argv[0]is resolved only as a PATH binary, identically on every wrapper and on the non-integration path.-x <shell-function-or-alias>fails loudly; shell resolution is explicit via-x sh -- -c '…'. One canonical code path.- Two-release deprecation. Release N warns; release N+1 cuts over.
- Legacy skew — no bridge. At cutover, B1's per-shell escaper is deleted outright. A B2 binary running under a not-yet-regenerated pre-B2 wrapper briefly regains the pre-B1 quoting bug until the next shell start regenerates the wrapper. Accepted — it mirrors the existing one-release directive-file skew.
History & status
- B1 — #2843. Shell-aware escaper. Shipped.
- Unit 1 — #2852. The deprecation warning. Shipped. A bare program token (
git,npm) survives the argv cutover unchanged → no warning. A value with shell syntax (echo hi && ls) → a warning plus a concrete, shell-aware migration hint. The classifier (is_clean_program_token) is purely structural and Windows-aware. - Unit 2 — this issue. The cutover. Not started — planned for a future release, once Unit 1's deprecation warning has shipped in a release and users have had a cycle to migrate.
Unit 2 scope
switch.rsexec block: build an argvVec<String>(-xvalue + each--arg, no escaping); drop the shell-escape and string-join.- New EXEC protocol: a
WORKTRUNK_DIRECTIVE_EXEC_ARGV_FILEenv var — a B2 wrapper sets it, and wt then writes NUL-separated argv. - Rewrite all five shell wrappers to split the file on NUL and run the argv as a child of the interactive shell, forcing external (PATH-binary) lookup for strict-
execvpparity. - Non-integration path → direct
Cmdspawn, sharing one canonical exec helper. - Delete B1 entirely: the
ShellEscapeModePowerShell/Fish arms, the escapers, andWORKTRUNK_SHELLwith its wrapper exports. - Reject an empty
-xat validation. - Retire the Unit 1 deprecation warning.
Feedback wanted
If you use wt switch -x with shell syntax — &&, $VAR, globs, pipelines — and the argv model would make your workflow worse, please comment here. Concrete cases will shape the migration path and the timing of the cutover.
This was written by Claude Code on behalf of Maximilian Roos
I love that you inform users of changes so diligently, offering them a chance to comment on the changes. You rock. Worktrunk rocks.
I often use wt switch -c some_new_worktree -x "code . && cd -", which:
- I run from a VSCode terminal
- Creates a new worktree and opens a separate VSCode window
- Returns the terminal to its prior location
Would these changes disallow something like that? If so, would a simple function wrapper work?
Thanks for all you do 💚
@omgreenfield thanks
@worktrunk-bot what do we show now when someone runs that? do we show a reformed command?
Yes — the deprecation warning (Unit 1, #2852 + #2863) reconstructs the command line and shows a reformed -x that preserves today's behavior, wrapped for the active shell. For wt switch -c some_new_worktree -x "code . && cd -":
▲ --execute will change in a future release: it will run a single program, with arguments after --, not a shell command line
↳ Comment at https://github.com/max-sixty/worktrunk/issues/2860 if the new single-program form would make a workflow worse; to run this command line unchanged, pass it to a shell: --execute sh -- -c 'code . && cd -'
(Under fish the suggestion is --execute fish -- -c 'code . && cd -'; under PowerShell, --execute pwsh -- -Command 'code . && cd -'.)
The reformed command works today and will keep working after the cutover, so @omgreenfield's workflow is preserved verbatim by copy-pasting the suggestion. The migration is purely textual — no behavior change.
ok, @omgreenfield lmk if you see anything different!
@max-sixty I do see a recommended command, but it isn't sufficient for this workflow.
- Previous command:
wt switch "$@" -x "code . && z -" - Suggested command:
wt switch "$@" -x sh -- -c "code . && z -" - Note:
ziszoxide, for all intents and purposes,z -iscd -
Issues
- Any goodies defined after shell boot (e.g.
~/.zshrc,~/.oh-my-zsh/custom/*.zsh, etc.) are inaccessible
- Options:
- Change to
wt switch "$@" -x sh -- -c "code . && cd -", but see issue 2 - Change to
wt switch "$@" -x zsh -- -c "source ~/.zshrc && code . && z -", but that's a good amount of overhead
- Change to
- In fairness, this is not a good example, but I imagine there are other examples where having access to all those goodies is helpful
- It simply can't perform
cd -. It's a new process.◎ Executing (--execute): sh -c 'code . && cd -' sh: line 0: cd: OLDPWD not set
I imagine I could come up with a work-around, but for this kind of workflow, the change is decidedly less convenient.
The only alternative I can think of is to allow both methods of --execute, but that comes with maintenance overhead.
I'll leave the decision making up to you. Thanks again for your time.
@omgreenfield's cd - finding is correct, and it's worth being precise about the mechanism — my earlier "purely textual, no behavior change" was right for code . but wrong for cd -.
Today the EXEC directive is run inside the interactive shell, not as a child: bash/zsh source "$exec_file" (templates/zsh.zsh), fish eval, PowerShell Invoke-Expression. Because code . && cd - is sourced into your zsh, the cd - mutates your shell's cwd and returns the terminal — and OLDPWD is set, because the wrapper's own builtin cd to the new worktree set it just before.
The suggested sh -- -c 'code . && cd -' breaks this even today, before any cutover: sh -c spawns a child process, so cd - runs in that child and can't touch the interactive shell — and OLDPWD isn't set there, which is exactly the cd: OLDPWD not set you saw. (Nushell already runs the directive via ^sh -c, so cd - never propagated there either.)
So this isn't a quoting/escaping detail that the migration hint can paper over — it's the core semantic of B2. The argv model runs the program as a child of the interactive shell by design, and no -x form can mutate the parent shell's cwd. The directory-returning half of this workflow depends on the sourced-string behavior that B2 removes.
Two things this surfaces, both for @max-sixty to weigh — I won't prescribe:
- The deprecation hint currently reconstructs
-x sh -- -c '<snippet>'as "preserved verbatim." For snippets that rely on running in the interactive shell (cd,pushd, shell functions,source), that's not faithful — the hint should either say so or skip those cases rather than imply equivalence. - What @omgreenfield actually wants here — open the new worktree in a separate VSCode window but leave the terminal where it was — is "run a program against the new worktree without switching the terminal." That's orthogonal to
-x's shell-vs-argv question; it's about whether the terminal follows the switch at all. Might be cleaner served by a switch-without-cd option than by anything-xdoes.
@worktrunk-bot would --no-cd work?
Yes — --no-cd fits this workflow cleanly, and it's a better fit than the sourced-string trick because it removes the half that B2 breaks rather than papering over it.
The key insight: z -/cd - exists only to undo the cd that wt switch performs. --no-cd means the terminal never moves in the first place, so there's nothing to undo — the entire && z - half just disappears. And because nothing needs to mutate the parent shell's cwd anymore, the argv model handles it fine: no OLDPWD, no source, no shell.
One adjustment: with --no-cd the shell stays at the original cwd, so a bare code . would open the old directory. Point it at the new worktree explicitly with {{ worktree_path }}:
Today (string form):
wt switch "$@" -c some_new_worktree --no-cd -x 'code {{ worktree_path }}'
After the B2 cutover (argv form):
wt switch "$@" -c some_new_worktree --no-cd -x code -- '{{ worktree_path }}'
Both create the worktree, open it in a separate VSCode window, and leave the terminal where it was. code is a PATH binary, so it works under strict execvp.
Scope caveat so we don't over-claim: this solves the "open a program against the new worktree without moving the terminal" shape — which is exactly @omgreenfield's case. It does not recover the general "run something that mutates my interactive shell" capability (cd, pushd, source, shell functions in the live session). B2 still removes that, and nothing argv-shaped brings it back. But for this workflow, --no-cd is the answer.
ok! @omgreenfield lmk if passing --no-cd works for you!
@max-sixty ha, it's so simple! Love it. Thanks for the help.