Repository navigation
fix(parser): pipe a group or subshell into a loop, group or subshell - #359
ferdingler wants to merge 7 commits into
Conversation
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested. The six scripts from #337 now run and match bash at top level, but three of the newly parsing forms give wrong results with exit 0 and nothing on stderr. Each has a small fix that I ran; with all three applied the suite still passes (2733) and every repro matches bash.
| Finding | Where it shows | |
|---|---|---|
| 🔴 | Output of compound | compound escapes an enclosing $( ), redirect or pipe |
CLI and Shell::execute only |
| 🟡 | set -e; ! { …; } | { …; } ends the script |
everywhere |
| 🟡 | Here-docs on two compound stages after a plain command are read in the wrong order | everywhere |
The 🔴 needs a 9-line change in src/exec.rs, so the PR would no longer be parser-only. main has the same gap for { …; } | user_fn, so that fix could also land as its own PR first. The two 🟡 are parser-only.
What I verified, and non-blocking notes
Verified at 56ca14e against base dbb6189 (Debian 12 aarch64, Rust 1.100.0, bash 5.2.15):
cargo test --workspace --all-targets: 2733 passed, 0 failed.cargo fmt --checkis clean. clippy reports nothing insrc/parser.rson head or base.- The six scripts from #337 are parse errors on base and match bash on head.
- The existing differential cases give the same result on base and head, except the four new cases that fail on base.
- An adversarial pass compared 12,882 scripts with bash and with base, through the CLI and through
Shell::run: 1,143 hand-written, 11,610 generated mixes of 2 to 6 stages, and the 129 repo cases. ThroughShell::run, 12,280 match bash, 329 differ the same way on base, 232 hit a base limitation that the new forms can now reach, and 41 are the findings here. - 85 malformed inputs and 21,000 fuzzed token sequences gave no panic or hang.
- With the three suggested fixes applied to a copy of head: 2733 passed, and the repros in the inline comments match bash.
Not run: the Python, Node and WASM bindings, macOS and Windows.
Non-blocking (5)
- Pre-existing, filed #381:
$?reads 0 in a command piped into a compound (false; echo "$?" | { cat; }prints 0 on main). With this PR the first group has it too:false; { echo "$?"; } | { cat; }prints 0, bash prints 1. - A temporary differential case with four compound stages,
{ echo a; } | { cat; } | { cat; } | { cat; }, aborts the test binary with a stack overflow (debug build, default thread stack). The same script runs fine with-c. Worth knowing before adding longer chains as cases. - #360 also changes here-doc reading in
src/parser.rs. Whichever lands second should re-run the here-doc repro below. - ⚪
{ echo a; } | \if truewasif: command not found(127) and is now a parse error.echo a | \if truealready does that on base. - ⚪ The doc comment on
collect_compound_pipe(src/parser.rs:1264-1266) says it wraps everything in aCompoundPipeline. It can now return aSubshell.
Reading order: collect_compound_pipe (1328-1353), then pipe_compound_into_compound (1665-1709), then the here-doc ordering (1728-1741).
… after a plain command (strands-agents#337)
…here-docs in order after a plain command (strands-agents#337)
|
@strandly-the-agent Please review this PR |
There was a problem hiding this comment.
Follow-up at 89605e4: both 🟡 are fixed. The 🔴 is still open, so my changes-requested stands for now.
| Finding | Status | |
|---|---|---|
| 🟡 | set -e; ! { …; } | { …; } ends the script |
fixed and verified |
| 🟡 | Here-doc order after a plain command | fixed and verified |
| 🔴 | CLI: output of compound | compound skips $( ), a redirect or a pipe |
still reproduces at 89605e4 |
I can't resolve review threads on this repo (no permission), so the two fixed threads are yours to close.
On the 🔴 you said it fits better as its own PR that lands first. I filed #383 for that, with the repro on main ({ …; } | user_fn) and the diff I tested. Once that fix is on main and this branch is rebased, I have nothing else blocking here. Whether to fold it in or split it is yours to decide.
What I verified
At 89605e4 against base dbb6189 (Debian 12 aarch64, Rust 1.100.0, bash 5.2.15):
cargo test --workspace --all-targets: 2733 passed, 0 failed.cargo fmt --checkis clean. clippy reports nothing insrc/parser.rs.- Differential: 114 passed, 0 failed, 17 known differences (112 passed before, plus the two new cases). Both new
.expectedfiles match what bash prints, and both scripts give different output on 56ca14e. - 75 scripts aimed at the two fixes, compared with bash and with 56ca14e: 72 match bash. One differs only in the value
$HOMEexpands to. Two are not from this PR: underset -e, a negated pipeline that is the last command of a group or runs in the background ends the script. main does the same with a plain command (set -e; { ! true; }; echo xprints nothing). Not filed: both forms are rare. - The 🔴 repros from the first review, re-run with
-con 89605e4: unchanged.
Not run: the Python, Node and WASM bindings, macOS and Windows.
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Follow-up at 7edb65c: the 🔴 capture leak is fixed. One new blocker: Rust (windows-latest) fails on a test added in 7edb65c.
| Finding | Status | |
|---|---|---|
| 🔴 | CLI: output of compound | compound skips $( ), a redirect or a pipe |
fixed by #384 (merged in 390f528), verified |
| 🔴 new | compound_pipe_into_compound_redirected_from_cli overflows the stack of the Windows debug CLI |
inline, with a replacement I ran on Linux |
The overflow is in the debug build only: a release build needs about 250 KB of stack for that script. Once Windows is green I have nothing else blocking.
What I verified, and non-blocking notes
At 7edb65c against main 4069bf9 (Debian 12 aarch64, Rust 1.100.0, bash 5.2.15):
cargo test --workspace --all-targetson Linux: 2743 passed, 0 failed.cargo fmt --checkis clean. Differential: 115 passed, 0 failed, 17 known differences.- The merge 390f528 is clean: no file differs from both parents, and
src/parser.rsis the only file undersrc/that differs from main. - The four scripts from the capture-leak thread match bash with
-c. - With #384's
src/exec.rschange reverted on 7edb65c, the three new tests fail with[][],a⏎file:banda⏎1⏎2, as you reported. With it they pass. - I re-ran the 12,810 scripts from the first review against bash, 89605e4 and 7edb65c. With
-c: 1,103 that differed now match bash, none that matched now differs, 664 differ exactly as before, and 3 changed but still differ (the state leak below). ThroughShell::run: no script changed. - 100 more scripts aimed at the capture path (nested
$( ), redirects, exit status, pipefail,set -e,eval,source, functions, here-docs, binary and large output): every difference from bash is one main also has with a plain command.
Not run: anything on Windows or macOS, and the Python, Node and WASM bindings.
Non-blocking (2)
- Pre-existing, filed #399: where output is captured,
{ …; } | cmdkeeps the group's variables andcd. On main,x=0; r=$( { x=1; echo a; } | cat; echo "x=$x" ); echo "$r"printsx=1. With this PR the second compound sees it too:x=0; echo "$( { x=1; echo a; } | { cat; echo "x=$x"; } )"printsx=1, bash printsx=0. - stderr written by a
compound | compoundinside$( )is dropped with-c, like all stderr inside$( )on main (#370).
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approved at dacfad7. The Windows failure is fixed, and all four findings from my earlier reviews are now closed.
| Finding | Status | |
|---|---|---|
| 🔴 | The redirected test overflows the stack of the Windows debug CLI | fixed in dacfad7, Rust (windows-latest) passes |
| 🔴 | CLI: output of compound | compound skips $( ), a redirect or a pipe |
fixed by #384 |
| 🟡 | set -e; ! { …; } | { …; } ends the script |
fixed in 89605e4 |
| 🟡 | Here-doc order after a plain command | fixed in 89605e4 |
I can't resolve review threads on this repo, so the Windows thread is yours to close.
What I verified
At dacfad7 against main 4069bf9:
- dacfad7 changes only
tests/shell_integration.rs, and that file is byte for byte the version I tested for the suggestion (same blob, e8d33a5). So the check that the test fails withfile:bwhen #384's change is reverted still holds. - Linux (Debian 12 aarch64, Rust 1.100.0):
cargo test --workspace --all-targetsgives 2743 passed, 0 failed, andcargo fmt --checkis clean. - CI on dacfad7:
CI Gateand all three Rust jobs passed. The Windows log showscompound_pipe_into_compound_redirected_from_execute ... okand 2,054 passed, 0 failed inshell_integration. - The source under
src/is unchanged since 7edb65c, so the 12,810-script comparison from my last review still applies.
Still open from earlier reviews, none blocking: #381 ($? reads 0 in a stage), #399 (a piped group keeps its variables and cd where output is captured) and #370 (stderr inside $( ) is dropped with -c).
Description
Piping a group or subshell into a loop, an
if, acase, or another group or subshell was a parse error, so something as common as{ echo a; echo b; } | while read x; do …; donedidn't run at all. A plain command piped into a loop already worked. Only the case where the left side was itself a group or subshell was missing, and so were longer chains like{ …; } | ( … ) | while ….Now the shell parses the right side as a compound command when it is one, and runs it as its own pipeline stage the same way it already does for a plain command on the left. The change is confined to pipeline parsing.
Related Issues
Fixes #337
Documentation PR
N/A
Type of Change
Bug fix
Testing
I added differential cases for every combination of plain command, group or subshell on the left with
while,for,if,case, a group or a subshell on the right, plus three-stage chains, here-documents on either side, negation, exit status, pipefail and positional arguments. All of them match bash. On main the group and subshell cases and the three-stage case failed. The plain-command case already passed and is there as a regression guard. The full test suite passes.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.