Skip to content

fix(xargs): forward the command's stderr instead of dropping it - #213

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

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/xargs-child-stderr

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

xargs only passed along what the command printed to stdout and threw away its stderr. So printf 'nosuch\n' | xargs cat printed nothing and exited 1, and xargs nosuchcmd exited 127 without saying the command wasn't found. An agent gets a failure with no explanation.

Now xargs forwards the command's stderr to its own stderr, so it follows any redirect on xargs: 2>/dev/null stays silent, 2>file writes to the file, and 2>&1 merges it into stdout. With -n 1, each run's output and errors still come out in order. xargs's exit status is still wrong (GNU returns 123 when any run fails), and I left that for a follow-up.

I compared these cases against GNU xargs and added tests for each one. Most of them fail on main and pass with the change, and the full test suite, fmt and clippy are clean.

$ printf 'nosuch\n' | xargs cat
# before: no output, exit 1
# after:  strands-shell: cat: no such file or directory: /home/lash/nosuch

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

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 04:57
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 04:57

@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. I found no blocking or should-fix issues. xargs now passes each run's stderr through its own fd 2, so it follows 2>/dev/null, 2>file, 2>&1 and 2>&- the same way the rest of the shell does. Child output is never printed twice, and none of it goes missing, including when xargs is nested.

@ferdingler, nothing for you to change. One thing to know before you rely on the ordering: within a single run, stdout still comes out before stderr. This is the same buffering the whole shell uses, not something this PR introduced (details below).

What I verified
  • Head 6f14911. cargo test --test shell_integration xargs passes 26/26.
  • The same tests on origin/main. 6 of the 8 new tests fail there: forwarded, command_not_found, to_file, merged_into_pipe, keeps_order_per_invocation, replace_..._forwarded. The other two (to_dev_null, stdout_unaffected) pass on main, so they work as regression guards. That means the tests check real behaviour, not just what a mock returns.
  • Behaviour on main vs this PR, built from both branches:
    • Errors appear exactly once in each of these setups: piped on (| cat), merged (2>&1 | cat -n), stderr-only into a pipe (2>&1 >/dev/null | …), inside a subshell, from a function that writes >&2, and with xargs nested in xargs.
    • 2>&-: io::stderr().ok() gives None, so errors are dropped quietly. Stdout and later runs keep going, the same as on main.
    • … | xargs -n 1 cat 2>&1 | head -1 over 3000 inputs: xargs stops on the broken pipe, matching GNU with its default SIGPIPE handling.
Appendix: not part of this PR (pre-existing, none newly filed)
  • Ordering within one run under 2>&1. Take a function f that prints o1, writes e1 to stderr, then prints o2. printf 'a\n' \| xargs f 2>&1 prints o1 o2 e1; GNU would print o1 e1 o2. Plain f 2>&1 \| cat, with no xargs, does the same on main, because a captured run's stdout and stderr are kept in separate buffers. The pipeline-script path in src/exec.rs near line 3560 flushes them in the same order. Separate runs (-n 1) still come out in the right order, as the PR says.
  • printf '/tmp/xf /nope /tmp/xf\n' \| xargs cat 2>&1 prints x and then the error, but not the second x. The cause is cat itself stopping at its first missing file: plain cat /tmp/xf /nope /tmp/xf does the same on main.
  • find -exec drops the child's stderr in exactly the same way (src/builtins/find.rs:411, :537). #227 tracks it.
  • Attached option values. xargs -n1 / -I{} (the #207 repros) still exit 127, because attached values aren't parsed. This PR at least makes that visible (-n1: command not found). #208 tracks it.
  • Exit status 123 when a run fails is #223, which the PR already defers.
Review routing

The change is 7 lines of logic plus tests, with no public API or model-facing text. I ran one correctness/tests pass with fresh context, plus my own probes comparing main, this PR and GNU. I skipped the API pass (no public surface) and the adversarial pass, since the risky paths (closed fd, broken pipe, nesting) were already probed directly.

@ferdingler
ferdingler merged commit ef070c6 into strands-agents:main Nov 12, 2026
30 of 59 checks passed
@ferdingler
ferdingler deleted the fix/xargs-child-stderr branch November 12, 2026 17:17
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