Repository navigation
fix(exec): expand the command's arguments only once - #352
Open
ferdingler wants to merge 2 commits into
Open
ferdingler wants to merge 2 commits into
ferdingler wants to merge 2 commits into
Conversation
Collaborator
Author
|
@strandly-the-agent Please review this PR |
strandly-the-agent
approved these changes
Nov 21, 2026
strandly-the-agent
left a comment
Collaborator
There was a problem hiding this comment.
Approve. The fix does what it says and I found no regression: across about 480 bash / main / PR comparisons, nothing that main got right is wrong on this branch.
One thing to decide before merge: Fixes #261 will close that issue, but its command half is still there. command echo $(echo once >> count; echo x) still runs the substitution twice (src/exec.rs:3516 and 3550-3554). I filed it as #364; the same change you made here would fix it, in this PR or a follow-up.
What I verified
Head b92a56e against base dbb6189, bash 5.2.15 as the reference.
cargo test --workspace --all-targets: 2733 passed, 0 failed, 0 ignored.cargo fmt --checkclean.cargo clippy --workspace --all-targets: no warnings on the changed lines.cargo test --test differential: 108 passed, 17 known differences, 0 skipped. The new case is one of the 108.- The new case fails on base on exactly the first two checks (
oncetwice,2 2), and its.expectedmatches a fresh bash recording. - Words built as
SingleQuotedare safe downstream:run_pipelineonly uses the raw words to expand them, and single-quoted parts are neither split nor globbed (src/exec.rs:586-588). Dispatch and xtrace use the expanded strings. - Env prefixes and redirect targets were expanded once before and still are.
- Compared on bash, base and head: command names (functions, aliases, keywords,
export/declarewithname=value,exec "$@",exec '', not found), arguments (quotes, backslashes, newlines, globs with odd file names, tilde, empty and spaced"$@", IFS,set -u), shell termination and exit status, MCP sessions, and 7,000 fuzzed word lists. - The fix also repairs a case the description doesn't mention:
printf 'a\nb\n' | (exec echo got $(head -n 1))printedgoton main and now printsgot alike bash.
Not run: the Python and Node.js suites locally (the change is Rust-only and CI is green on them), WASM.
Appendix: non-blocking (3)
- Pre-existing, filed #363: an expansion error on an
execline (exec echo "${PORT:?msg}") leavesnounset_errorset, so the next call in the same MCP session is skipped with exit 2 and no output. Same on main; this PR only stops the message printing twice. - Pre-existing, already tracked in #294:
command -v X >/dev/nullstill prints X. - Optional test additions, all passing on head by hand: a stdin-reading substitution (
echo data | (exec echo "$(cat)"), which printed an empty line on main),exec "$@", and a top-levelexec cmdfollowed by a line that must not run. Every line in the new case is in a subshell.
10 tasks done
This branch has not been deployed
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.
Description
exec cmd argsexpanded its arguments twice, so anything with a side effect ran twice.(exec echo $(echo once >> count; echo x))wroteonceto the file two times, andexec echo $((n += 1))printed 2 instead of 1. Bash runs them once.Now
execruns the command with the arguments it already expanded instead of expanding them again. Quoting is unchanged: double spaces inside quotes survive, a single-quoted$HOMEstays literal, and empty arguments are kept.I added a differential case covering the double run, arithmetic side effects, and those quoting cases. On main it failed on the first two, and with the change it passes along with the full test suite. I didn't add
exec -a, which is tracked separately in #323.Related Issues
Fixes #261
Documentation PR
N/A
Type of Change
Bug fix
Testing
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.