Repository navigation
refactor(for-each): exec argv directly, drop implicit shell - #2465
Conversation
Closes #2461 Co-Authored-By: Claude <[email protected]>
Each post-`--` element is template-expanded and exec'd directly via `Cmd::new(program).args(rest)`. Shell features (pipes, redirects, $VAR) require explicit `sh -c '<snippet>'`. Replaces the arity-dependent rule where one arg was a shell snippet and multiple args were shell-escaped and joined into `sh -c`. The arity rule fixed #2461 but kept two execution shapes behind one command. Direct exec is one rule: argv is argv. Templates expand into argv elements without shell escaping. The #2461 quoting bug becomes structurally impossible. Co-Authored-By: Claude <[email protected]>
# Conflicts: # src/commands/for_each.rs
Direct exec surfaces `(os error 2)` on Unix and `program not found` on Windows. Add an insta filter so `test_for_each_spawn_fails` matches on both platforms. Co-Authored-By: Claude <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
The up alias recipe in docs/content/extending.md and skills/worktrunk/reference/extending.md still uses the old single-arg shell-snippet form, which is exactly the shape this PR breaks. step.md got the sh -c rewrite but extending.md was missed:
# docs/content/extending.md:122-130 (and skills/worktrunk/reference/extending.md:126-134)
[aliases]
up = '''
git fetch --all --prune && wt step for-each -- '
git rev-parse --verify @{u} >/dev/null 2>&1 || exit 0
g=$(git rev-parse --git-dir)
test -d "$g/rebase-merge" -o -d "$g/rebase-apply" && exit 0
git rebase @{u} --no-autostash || git rebase --abort
''''After this PR, wt step for-each exec's its sole post--- argument directly as a program, so this recipe will fail with a "no such file or directory" spawn error in every worktree. The fix is the same shape used elsewhere in the PR — wrap the snippet in sh -c:
[aliases]
up = '''
git fetch --all --prune && wt step for-each -- sh -c '
git rev-parse --verify @{u} >/dev/null 2>&1 || exit 0
g=$(git rev-parse --git-dir)
test -d "$g/rebase-merge" -o -d "$g/rebase-apply" && exit 0
git rebase @{u} --no-autostash || git rebase --abort
''''(Both files are outside the PR diff, so happy to push the fix as a follow-up commit if useful — say the word.)
…ntics The up recipe relied on the old single-arg form where for-each silently spawned `sh -c <arg>`. After exec'ing argv directly, the snippet would be treated as a program name and fail to spawn. Wrap it in `sh -c '...'` to match step.md and the rest of the PR.
|
Pushed in 58efb8e — wrapped the |
The python3 -c 'import sys; print(sys.argv[1:])' 'a b' example was testing the argv-quoting contract, not showing a typical use. Swap in git status --short — a workflow users actually run. The contract is covered by test_for_each_preserves_argv_quoting. Co-Authored-By: Claude <[email protected]>
Cuts v0.47.0. Highlights: - **Integration detection fix** across `wt list`, `wt remove`, `wt merge`, `wt step prune` for the local/upstream-diverged case (#2507, #2513, #2515). - **Faster shell-command dispatch** (~10ms per invocation, 1.49× on trivial aliases) via event-driven signal forwarding (#2537, #2538), plus parallel config load (#2543) and merged cold-start prewarm rev-parse (#2541). - **`wt switch <number>` suggests `pr:N` / `mr:N`** instead of just `--create` (#2516). - **Library API breaking changes**: `RefSnapshot` cutover (#2528, #2530) and the `for-each` direct-exec change (#2465). Full list in `CHANGELOG.md`. See `CHANGELOG.md` for the complete entry. > _This was written by Claude Code on behalf of @max-sixty_ --------- Co-authored-by: Claude <[email protected]>
Problem
wt step for-eachrebuilt the post---command withargs.join(" ")before passing it tosh -c. That dropped the user's quoting and argv boundaries, so anything with spaces,;, or shell metacharacters inside an argv element broke. The reproduction from the issue:Solution
Drop the implicit shell. The post-
--argv is the program and its arguments, exec'd directly viaCmd::new(program).args(rest). Templates expand into argv elements withshell_escape=false. Users wanting shell features (pipes, redirects,$VAR, globs) writesh -c '<snippet>'explicitly — the same pattern asxargs,find -exec,kubectl exec --, anddocker run.This started as the smaller fix that landed on the previous commit (shell-escape each argv element before joining, with an arity branch — one arg meant "shell snippet," many args meant "argv to escape and re-join"). On review the arity asymmetry felt wrong; the design discussion concluded that direct exec is one rule with no quoting recovery pipeline, and the #2461 bug becomes structurally impossible.
for-eachis still tagged[experimental], so the small breakage of the documented snippet form is acceptable.After the fix:
What changes for users
wt step for-each -- 'git status | wc -l'wt step for-each -- sh -c 'git status | wc -l'wt step for-each -- 'echo Branch: {{ branch }}'wt step for-each -- echo 'Branch: {{ branch }}'(or wrap insh -c)wt step for-each -- python3 -c '...' 'a b'✗ brokenwt step for-each -- python3 -c '...' 'a b'✓ worksThe
for-eachhelp/docs are rewritten around the simpler model (one rule, two example blocks).Testing
test_for_each_preserves_argv_quotingcovers the exact reproduction from the issue and is now exercising a load-bearing property of the design rather than a join bug.test_for_each_aborts_on_signal_exitrewritten to usesh -c '<snippet>'for the shell features it needs.test_for_each_json_spawn_failuresimplified — direct exec means a missing program is enough; the PATH/symlink dance for forcing sh-spawn failure is no longer needed.cargo test --test integration for_each— 16 passed.cargo run -- hook pre-merge --yes— 3403 passed, 0 skipped, all lints green.Closes #2461 — automated triage