Skip to content

refactor(for-each): exec argv directly, drop implicit shell - #2465

Merged
max-sixty merged 6 commits into
mainfrom
fix/issue-2461
Apr 30, 2026
Merged

max-sixty merged 6 commits into
mainfrom
fix/issue-2461

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

wt step for-each rebuilt the post--- command with args.join(" ") before passing it to sh -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:

$ wt step for-each -- python3 -c 'import sys; print(sys.argv[1:])' 'a b'
sh: 1: Syntax error: word unexpected (expecting ")")

Solution

Drop the implicit shell. The post--- argv is the program and its arguments, exec'd directly via Cmd::new(program).args(rest). Templates expand into argv elements with shell_escape=false. Users wanting shell features (pipes, redirects, $VAR, globs) write sh -c '<snippet>' explicitly — the same pattern as xargs, find -exec, kubectl exec --, and docker 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-each is still tagged [experimental], so the small breakage of the documented snippet form is acceptable.

After the fix:

$ wt step for-each -- python3 -c 'import sys; print(sys.argv[1:])' 'a b'
['a b']

$ wt step for-each -- sh -c 'git status | wc -l'
[per-worktree counts...]

What changes for users

Before After
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 in sh -c)
wt step for-each -- python3 -c '...' 'a b' ✗ broken wt step for-each -- python3 -c '...' 'a b' ✓ works

The for-each help/docs are rewritten around the simpler model (one rule, two example blocks).

Testing

  • test_for_each_preserves_argv_quoting covers 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_exit rewritten to use sh -c '<snippet>' for the shell features it needs.
  • test_for_each_json_spawn_failure simplified — 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

Comment thread src/commands/for_each.rs Resolved
max-sixty and others added 2 commits April 30, 2026 10:33
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
@max-sixty max-sixty changed the title fix(for-each): preserve argv quoting through sh -c refactor(for-each): exec argv directly, drop implicit shell Apr 30, 2026
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 worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Pushed in 58efb8e — wrapped the up snippet in sh -c and re-ran test_docs_are_in_sync so skills/worktrunk/reference/extending.md is regenerated from the docs source.

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]>
@max-sixty
max-sixty merged commit 6eaf349 into main Apr 30, 2026
23 checks passed
@max-sixty
max-sixty deleted the fix/issue-2461 branch April 30, 2026 19:37
@max-sixty max-sixty mentioned this pull request May 3, 2026
max-sixty added a commit that referenced this pull request May 3, 2026
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]>
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.

wt step for-each loses quoting and argument boundaries after --

2 participants