Skip to content

fix(exec): expand the command's arguments only once - #352

Open
ferdingler wants to merge 2 commits into
strands-agents:mainfrom
ferdingler:fix/exec-double-expansion-261
Open

ferdingler wants to merge 2 commits into
strands-agents:mainfrom
ferdingler:fix/exec-double-expansion-261

Conversation

@ferdingler

Copy link
Copy Markdown
Collaborator

Description

exec cmd args expanded its arguments twice, so anything with a side effect ran twice. (exec echo $(echo once >> count; echo x)) wrote once to the file two times, and exec echo $((n += 1)) printed 2 instead of 1. Bash runs them once.

Now exec runs the command with the arguments it already expanded instead of expanding them again. Quoting is unchanged: double spaces inside quotes survive, a single-quoted $HOME stays 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

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 --check clean. 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 (once twice, 2 2), and its .expected matches a fresh bash recording.
  • Words built as SingleQuoted are safe downstream: run_pipeline only 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/declare with name=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)) printed got on main and now prints got a like 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 exec line (exec echo "${PORT:?msg}") leaves nounset_error set, 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/null still 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-level exec cmd followed by a line that must not run. Every line in the new case is in a subshell.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] exec CMD ARGS expands its arguments twice, so substitutions run twice

2 participants