Skip to content

fix(xargs): accept attached option values and stop at the command - #208

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/xargs-option-parsing
Nov 12, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/xargs-option-parsing

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

xargs only understood its options when the value was a separate word. So printf 'a b\n' | xargs -n1 echo exited 127 with no output, because it tried to run a command called -n1. GNU prints a and b. It also kept parsing options after the command name, so it ate the command's own flags: echo f | xargs head -n 1 printed the whole file instead of the first line.

Now -n, -I and -d accept attached values (-n1, -I{}, -d,), and option parsing stops at the command or at --, like GNU. No new options. Unknown options like -r still run as a command named -r and exit 127, same as before; I left that for a follow-up.

I compared the attached forms and the child-flag cases (grep -n, head -n 1) against GNU xargs and they match. The new tests fail on main and pass with the change, and the existing tests for -n 1, -I {}, -0 and plain xargs cmd still pass.

Related Issues

Part of #207

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • 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
ferdingler marked this pull request as ready for review November 12, 2026 19:31
@ferdingler
ferdingler force-pushed the fix/xargs-option-parsing branch from 507aa82 to 020aae0 Compare November 12, 2026 19:31
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 19:31
@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 works and nothing regressed. I ran about 120 xargs cases through GNU xargs, this PR and main. Every case where the PR's output differs from main's matches GNU, or was already broken in the separate-word form (-n 1) on main.

  • printf 'a b\n' | xargs -n1 echo prints a and b. -I{}, -d, and -- all work. echo f | xargs head -n 1 now passes -n 1 to head.
  • All 9 new tests fail on main and pass here. Full cargo test --workspace --all-targets: 2208 passed, 0 failed. CI is green on all 30 checks at 020aae0.
  • No 🔴 or 🟡 findings. One ⚪ nit and some older gaps that also exist on main are below the fold.
✅ What I checked
  • Commits: PR 020aae0, main d4c57a7. I compared against GNU findutils 4.9.0 xargs running under bash.
  • Now matches GNU: -n1, -n+1, -I{}, -Ié, -d,, -d ' ', -0 -n1, -n1 -0, -n1 -n2 (last one wins), --, bare xargs --, xargs -n1 with no command (defaults to echo). Child flags reach the child unchanged in grep -n, head -n 1, sort -n, echo -n 1, echo -0 and echo -d ,.
  • Unchanged and still matching GNU: xargs -- -n1 echo treats -n1 as the command (exit 127).
  • Byte slicing is safe: arg.get(..2) only matches ASCII -I/-n/-d, so arg[2..] (xargs.rs:54) always starts on a character boundary. Non-ASCII values (-dé, -d€, -Ié) don't panic.
  • Missing value at the end: with -n last, i runs past the end and args[i.min(args.len())..] (xargs.rs:68) handles it.
  • Command checks still apply: child commands still go through crate::exec::execute (xargs.rs:108, :127). The xargs rm case in tests/command_check.rs passes.
  • Differential test: removing the known-difference marker from xargs_n1.sh is required. The harness fails once a marked case matches bash, and cargo test --test differential passes.
  • Evidence uploaded: xargs-gnu-matrix.txt (the GNU/PR/main matrix), xargs-gnu-matrix.sh (the script behind it), cargo-test-workspace.log.
Appendix: non-blocking (1 nit + older gaps)

⚪ Bad values in the new attached form are ignored silently (xargs.rs:62-63). echo a b | xargs -n0 echo and xargs -nX echo exit 0 with no batch limit, while GNU says invalid number and exits 1. On main these failed loudly, but only by accident (exit 127, -n0: command not found). The separate form -n X already behaved this way, so this is consistent with existing behaviour. Easy to tighten later if you want it:

"-n" => match v.parse::<usize>() {
    Ok(n) if n > 0 => max_args = n,
    _ => return Err(format!("xargs: invalid number \"{v}\" for -n option").into()),
},

Gaps that also exist on main (the separate form behaves the same on main). I didn't open issues; they fit under #207's xargs item:

  • xargs -d '\n' splits on a literal backslash, where GNU reads the escape and splits on newlines.
  • -I splits input on whitespace instead of lines. printf 'a b\n' | xargs -I{} echo [{}] prints [a] and [b]; GNU prints [a b].
  • -0n1 bundling, -r, -t, -L, -P and --null aren't supported. Missing or empty values are ignored rather than rejected.
  • Exit status 123 when a child fails is already covered by #223.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for checking the wider GNU matrix and the command checks. I agree that numeric validation should be tightened in a follow-up so the attached and separate forms reject bad, missing, and empty values consistently. This PR keeps their existing value semantics and fixes how the options reach the parser.

The delimiter escapes and -I line splitting fit under the remaining xargs work in #207. Child exit-status mapping is already covered by #223.

@ferdingler
ferdingler merged commit bab76b7 into strands-agents:main Nov 12, 2026
31 checks passed
@ferdingler
ferdingler deleted the fix/xargs-option-parsing branch November 12, 2026 23:04
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.

2 participants