Skip to content

fix: keep going past a failing operand in cat and mkdir - #354

Open
ferdingler wants to merge 4 commits into
strands-agents:mainfrom
ferdingler:fix/335-continue-past-failing-operand
Open

ferdingler wants to merge 4 commits into
strands-agents:mainfrom
ferdingler:fix/335-continue-past-failing-operand

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

cat and mkdir gave up at the first operand that failed. cat missing f printed the error and nothing from f, and mkdir nodir/a b never created b. GNU reports the bad operand, handles the rest, and exits 1. The issue also lists tee, rmdir, chmod, cp, sed -i and wc, which do the same thing.

Now cat and mkdir report each operand they can't handle, carry on with the others, and exit 1 at the end. The error text is unchanged. I fixed only these two to keep the change small. The other commands each need their own edit, so I'll send them separately.

I added differential cases for both commands and integration tests that check every failure is reported and the exit status is 1. On main they failed because the later operands produced no output. With the change they pass, and so does the full test suite.

Related Issues

Part of #335 (fixes cat and mkdir; other commands are follow-ups)

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.

Verdict: approve, with one should-fix item and no blockers. cat and mkdir now carry on past failing operands and matched bash + coreutils 9.1 on stdout and exit status in every operand mix tried, except the case in the inline comment. Single-operand and all-success runs are unchanged from main.

  • 🟡 cat hides a second kind of failure (inline): once one operand has failed to open, a later read or write error prints nothing and the remaining operands are skipped.
  • ⚪ A hang that rm already has on main now also reaches cat and mkdir: 65 or more failing operands in a later pipeline stage. The cause is in the pipeline's stderr draining, not in this diff. Filed #366.

Non-blocking question: several errors don't name the operand (strands-shell: cat: is a directory), so cat d1 f d2 prints two identical lines. With six more commands to convert, would a shared reporter that takes stderr once and prefixes the operand, like Errors in rm.rs, be worth doing first?

What I verified, appendix

Verified at head 4946e4b against base dbb6189 (Linux aarch64, debug builds)

  • cargo test --workspace --all-targets: 2735 passed, 0 failed. cargo fmt --check clean. cargo clippy --workspace --all-targets exits 0 with no warnings in cat.rs or mkdir.rs.
  • The new tests fail without the fix: at acd2f40 both differential cases fail on stdout and both integration tests fail on a stdout mismatch.
  • Both .expected files match what bash 5.2.15 + coreutils 9.1 print for the .sh cases, byte for byte.
  • make_dir is the old -p walk moved unchanged (same lines once indentation is stripped).
  • Differential runs, base / head / bash: about 200 scripts on the virtual filesystem and 31 on a direct bind (missing, directory, "", permission denied, dangling and looping symlinks, file as parent, -p partial failures, cat -n numbering across a failure), plus exit status under set -e, &&/||, $(…), pipelines and functions. In-shell 2>&1 keeps errors and file contents in bash's order.
  • Not run: pytest tests/python, npm test, WASM, non-Linux.

Appendix, non-blocking (6)

  • Hang, filed #366. grep line lines | mkdir <65 failing operands> returns on main and never returns on this head, even with [limits] timeout. grep line lines | rm <65 missing> does the same on main.
  • Test gap: mkdir -p "" c takes the early return at mkdir.rs:48, so no test has a failure inside the -p walk followed by another operand. echo f > file; mkdir -p file/x y behaves correctly on this head; one more line in the differential case would pin it.
  • With stderr closed (cat missing f 2>&-) both commands still stop at the first failure. Same on main, and rm does the same.
  • When the CLI's own stdout and stderr share a host fd (strands-shell -c 'cat -n f m1 f m2 f' 2>&1), later error lines arrive after later file contents. Not filed: cosmetic, and library callers get the streams separately.
  • The template box "I have reviewed and understand every line of code in this PR" is unchecked; AGENTS.md:137 asks for it.
  • Pre-existing on main, found while testing: mkdir -p f on a regular file exits 0 (filed #372, in the code this PR moves); touch and ls stop at the second failing operand (added to #335); cmd >> out 2>> out loses stdout (filed #373); timeout doesn't stop cat /dev/zero > /dev/null (filed #374); stderr inside $( ) is dropped by the CLI (already #370).

Comment thread src/commands/cat.rs Outdated
Comment on lines 73 to 74
let mut r = io::take_reader(fd)?;
cat_stream(&mut r, &mut w, number, &mut lineno).await?;

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.

🟡 After one operand fails to open, a later read or write error is dropped silently and the rest are skipped

cat -n missing bin t, where bin is not valid UTF-8, prints only the error for missing and exits 1: nothing about bin, and nothing from t. On its own, cat -n bin does report invalid utf-8 sequence…. The cause is the two ? on these lines. Once the loop has taken stderr for the first failure, the error they return goes back to the shell, which can't take stderr a second time to print it (src/exec.rs:4331). The same path hides file size limit exceeded, so cat missing s s s s > out leaves a truncated out and reports only missing.

main also prints just the missing error for these inputs, because it stopped there. So this is a gap in the fix, not a regression.

Suggestion: report these errors through ew too, instead of returning them. A read error can then continue like an open error; a write error to stdout should end the loop, otherwise it repeats for every remaining operand.

Reproduction details

Head 4946e4b, base dbb6189, debug builds, default virtual filesystem, Linux aarch64. Compared with bash 5.2.15 + coreutils 9.1.

printf "\377\376\n" > bin; echo ok > t; cat -n missing bin t; echo rc=$?
output
head strands-shell: cat: no such file or directory: /home/lash/missing, rc=1
base same as head
bash error for missing, numbered lines 1 and 2, rc=1

Control on head: cat -n bin t prints strands-shell: cat: invalid utf-8 sequence of 1 bytes from index 0, rc=1.

Write error, with a config of [limits] / max_file_size = 100:

echo 0123456789abcdef0123456789abcdef0123456789abcdef > s
cat s s s s > out1; echo "rc=$? out1=$(wc -c < out1)"
cat missing s s s s > out2; echo "rc=$? out2=$(wc -c < out2)"
first cat second cat
head file size limit exceeded, rc=1 out1=98 only the missing error, rc=1 out2=98
base same only the missing error, rc=1 out2=0

I also tried the simple fix on top of 4946e4b in a scratch worktree: report every cat_stream error through ew and continue. cat -n missing bin t nope t then prints both ok lines and all three errors with rc=1. Under the size limit, file size limit exceeded is printed once per remaining operand, which is why a write error should stop the loop.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 4c3154b, with the tests first in 2a58b3f. Opening, reading and writing are now reported separately. A read error (or an open error) goes through the same stderr writer and cat moves on to the next operand. A write error to stdout is reported once and ends the loop, like GNU. New integration tests: cat -n missing bin t now reports both errors, prints ok, and exits 1. Under max_file_size(10), cat missing s s s nope > out reports missing and file size limit exceeded once, then stops; nope is never tried. Both tests failed before the fix.

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.

Verified fixed at 4c3154b: cat -n missing bin t nope t reports all three errors and prints both ok lines, and under the size limit cat missing s s s s > out reports missing and file size limit exceeded once, then stops.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

On the Fixes #335 point: done, the body now says Part of #335, so the issue stays open for the other commands.

@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.

Follow-up at 4c3154b: the cat finding is fixed and I found nothing new. Approve, no open items from me.

  • 🟡 Later read or write error dropped: fixed. A read error is reported and cat moves on; a write error is reported once and ends the loop. Both new tests fail at 2a58b3f and pass here.
  • The Fixes #335 item from my first review was already out of date when I posted it, since the body had been changed to Part of #335 by then. I removed it from that review.
What I verified

Head 4c3154b against the previous head 4946e4b (Linux aarch64, debug builds).

  • cargo test --workspace --all-targets: 2737 passed, 0 failed. cargo fmt --check clean. cargo clippy --workspace --all-targets exits 0 with no warnings in cat.rs.
  • At 2a58b3f, cargo test --test shell_integration -- cat_reports gives 1 passed, 2 failed (the two new tests).
  • The fix replaces tokio::io::copy with a read/write loop that every cat call goes through, so I compared the two heads directly:
    • 199 scripts on the virtual filesystem: stdout, stderr and status identical.
    • A 60 MB binary file through a direct bind (cat big > c, cat big empty odd big > c, cat < big > c): md5 matches the host copy on both heads.
    • Closed pipe, closed stdout, stdin input and [limits] timeout cases: same on both heads, with one intended difference. cat -n missing big big nope | head -1 now prints strands-shell: cat: pipe closed, where the previous head printed nothing after the missing error. main prints the same line for cat -n big | head -1; bash prints nothing in either case. Filed as #376, since it predates this PR.
  • Not run: pytest tests/python, npm test, WASM, non-Linux.

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.

2 participants