Repository navigation
fix(xargs): accept attached option values and stop at the command - #208
Conversation
507aa82 to
020aae0
Compare
|
@strandly-the-agent Please review this PR |
There was a problem hiding this comment.
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 echoprintsaandb.-I{},-d,and--all work.echo f | xargs head -n 1now passes-n 1tohead.- 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 at020aae0. - No 🔴 or 🟡 findings. One ⚪ nit and some older gaps that also exist on main are below the fold.
✅ What I checked
- Commits: PR
020aae0, maind4c57a7. 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),--, barexargs --,xargs -n1with no command (defaults toecho). Child flags reach the child unchanged ingrep -n,head -n 1,sort -n,echo -n 1,echo -0andecho -d ,. - Unchanged and still matching GNU:
xargs -- -n1 echotreats-n1as the command (exit 127). - Byte slicing is safe:
arg.get(..2)only matches ASCII-I/-n/-d, soarg[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
-nlast,iruns past the end andargs[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). Thexargs rmcase intests/command_check.rspasses. - Differential test: removing the
known-differencemarker fromxargs_n1.shis required. The harness fails once a marked case matches bash, andcargo test --test differentialpasses. - 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.-Isplits input on whitespace instead of lines.printf 'a b\n' | xargs -I{} echo [{}]prints[a]and[b]; GNU prints[a b].-0n1bundling,-r,-t,-L,-Pand--nullaren't supported. Missing or empty values are ignored rather than rejected.- Exit status 123 when a child fails is already covered by #223.
|
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 |
Description
xargs only understood its options when the value was a separate word. So
printf 'a b\n' | xargs -n1 echoexited 127 with no output, because it tried to run a command called-n1. GNU printsaandb. It also kept parsing options after the command name, so it ate the command's own flags:echo f | xargs head -n 1printed the whole file instead of the first line.Now
-n,-Iand-daccept attached values (-n1,-I{},-d,), and option parsing stops at the command or at--, like GNU. No new options. Unknown options like-rstill run as a command named-rand 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 {},-0and plainxargs cmdstill 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.
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.