Repository navigation
fix: keep going past a failing operand in cat and mkdir - #354
ferdingler wants to merge 4 commits into
Conversation
|
@strandly-the-agent Please review this PR |
There was a problem hiding this comment.
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.
- 🟡
cathides 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
rmalready has onmainnow also reachescatandmkdir: 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 --checkclean.cargo clippy --workspace --all-targetsexits 0 with no warnings incat.rsormkdir.rs.- The new tests fail without the fix: at
acd2f40both differential cases fail onstdoutand both integration tests fail on a stdout mismatch. - Both
.expectedfiles match what bash 5.2.15 + coreutils 9.1 print for the.shcases, byte for byte. make_diris the old-pwalk 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,-ppartial failures,cat -nnumbering across a failure), plus exit status underset -e,&&/||,$(…), pipelines and functions. In-shell2>&1keeps 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 onmainand never returns on this head, even with[limits] timeout.grep line lines | rm <65 missing>does the same onmain. - Test gap:
mkdir -p "" ctakes the early return atmkdir.rs:48, so no test has a failure inside the-pwalk followed by another operand.echo f > file; mkdir -p file/x ybehaves 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 onmain, andrmdoes 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:137asks for it. - Pre-existing on
main, found while testing:mkdir -p fon a regular file exits 0 (filed #372, in the code this PR moves);touchandlsstop at the second failing operand (added to #335);cmd >> out 2>> outloses stdout (filed #373);timeoutdoesn't stopcat /dev/zero > /dev/null(filed #374); stderr inside$( )is dropped by the CLI (already #370).
| let mut r = io::take_reader(fd)?; | ||
| cat_stream(&mut r, &mut w, number, &mut lineno).await?; |
There was a problem hiding this comment.
🟡 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
On the @strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
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
catmoves on; a write error is reported once and ends the loop. Both new tests fail at2a58b3fand pass here. - The
Fixes #335item from my first review was already out of date when I posted it, since the body had been changed toPart of #335by 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 --checkclean.cargo clippy --workspace --all-targetsexits 0 with no warnings incat.rs.- At
2a58b3f,cargo test --test shell_integration -- cat_reportsgives 1 passed, 2 failed (the two new tests). - The fix replaces
tokio::io::copywith a read/write loop that everycatcall 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] timeoutcases: same on both heads, with one intended difference.cat -n missing big big nope | head -1now printsstrands-shell: cat: pipe closed, where the previous head printed nothing after themissingerror.mainprints the same line forcat -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.
Description
catandmkdirgave up at the first operand that failed.cat missing fprinted the error and nothing fromf, andmkdir nodir/a bnever createdb. GNU reports the bad operand, handles the rest, and exits 1. The issue also liststee,rmdir,chmod,cp,sed -iandwc, which do the same thing.Now
catandmkdirreport 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
catandmkdir; other commands are follow-ups)Documentation PR
N/A
Type of Change
Bug fix
Testing
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.