Skip to content

fix(parser): pipe a group or subshell into a loop, group or subshell - #359

Open
ferdingler wants to merge 7 commits into
strands-agents:mainfrom
ferdingler:fix/337-compound-pipe
Open

ferdingler wants to merge 7 commits into
strands-agents:mainfrom
ferdingler:fix/337-compound-pipe

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Piping a group or subshell into a loop, an if, a case, or another group or subshell was a parse error, so something as common as { echo a; echo b; } | while read x; do …; done didn'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.

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

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 --check is clean. clippy reports nothing in src/parser.rs on 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. Through Shell::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 true was if: command not found (127) and is now a parse error. echo a | \if true already does that on base.
  • ⚪ The doc comment on collect_compound_pipe (src/parser.rs:1264-1266) says it wraps everything in a CompoundPipeline. It can now return a Subshell.

Reading order: collect_compound_pipe (1328-1353), then pipe_compound_into_compound (1665-1709), then the here-doc ordering (1728-1741).

Comment thread src/parser.rs Resolved
Comment thread src/parser.rs Resolved
Comment thread src/parser.rs Resolved
@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.

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 --check is clean. clippy reports nothing in src/parser.rs.
  • Differential: 114 passed, 0 failed, 17 known differences (112 passed before, plus the two new cases). Both new .expected files 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 $HOME expands to. Two are not from this PR: under set -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 x prints nothing). Not filed: both forms are rare.
  • The 🔴 repros from the first review, re-run with -c on 89605e4: unchanged.

Not run: the Python, Node and WASM bindings, macOS and Windows.

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

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-targets on Linux: 2743 passed, 0 failed. cargo fmt --check is clean. Differential: 115 passed, 0 failed, 17 known differences.
  • The merge 390f528 is clean: no file differs from both parents, and src/parser.rs is the only file under src/ that differs from main.
  • The four scripts from the capture-leak thread match bash with -c.
  • With #384's src/exec.rs change reverted on 7edb65c, the three new tests fail with [][], a⏎file:b and a⏎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). Through Shell::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, { …; } | cmd keeps the group's variables and cd. On main, x=0; r=$( { x=1; echo a; } | cat; echo "x=$x" ); echo "$r" prints x=1. With this PR the second compound sees it too: x=0; echo "$( { x=1; echo a; } | { cat; echo "x=$x"; } )" prints x=1, bash prints x=0.
  • stderr written by a compound | compound inside $( ) is dropped with -c, like all stderr inside $( ) on main (#370).

Comment thread tests/shell_integration.rs Resolved
@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.

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 with file:b when #384's change is reverted still holds.
  • Linux (Debian 12 aarch64, Rust 1.100.0): cargo test --workspace --all-targets gives 2743 passed, 0 failed, and cargo fmt --check is clean.
  • CI on dacfad7: CI Gate and all three Rust jobs passed. The Windows log shows compound_pipe_into_compound_redirected_from_execute ... ok and 2,054 passed, 0 failed in shell_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).

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.

[BUG] A group or subshell can't be piped into a loop, group or subshell ({ a; b; } | while read … is a parse error)

2 participants