Skip to content

[BUG] In the CLI, output of a group or subshell piped into a function skips the enclosing $( ), redirect or pipe (x=$( { echo a; } | f ) leaves x empty) #383

Closed

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (built from main at dbb6189)
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Binding

Rust

Strands Shell Version

0.0.0 (built from source, main dbb6189)

Language Runtime Version

Rust 1.100.0

Operating System

Debian 12 (aarch64)

Installation Method

built from source

Steps to Reproduce

$ strands-shell -c 'g() { cat; }; x=$( { echo a; } | g ); echo "[$x]"'
$ strands-shell -c 'g() { cat; }; { { echo a; } | g; } > f; echo "file:$(cat f)"'
$ strands-shell -c 'g() { cat; }; for i in 1 2; do { echo $i; } | g; done | tr 12 xy'

Expected Behavior

As in bash 5.2.15: [a], then file:a, then x and y.

Actual Behavior

The function's output goes straight to the terminal and the enclosing construct gets nothing. Exit status is 0 and stderr is empty in all three:

Script stdout
x=$( { echo a; } | g ); echo "[$x]" a then []
{ { echo a; } | g; } > f; echo "file:$(cat f)" a then file:
for i in 1 2; do { echo $i; } | g; done | tr 12 xy 1 then 2

The first script gives the same result when it is piped to strands-shell on stdin. These are correct:

$ strands-shell -c 'g() { cat; }; x=$( echo a | g ); echo "[$x]"'            # [a]
$ strands-shell -c 'g() { cat; }; x=$( { echo a; } | g | cat ); echo "[$x]"'  # [a]

So it needs a compound command on the left and a single function call as the whole tail.

Additional Context

Found while reviewing #359. That PR turns the right side of every compound | compound into a call to a generated function, so with it v=$( { echo a; } | while read x; do echo "$x"; done ) takes this path too (repros in #359 (comment)).

In src/exec.rs at dbb6189, the capturing executor runs the tail of a CompoundPipeline at line 2193:

let (mut exit, out) = execute_pipeline_capture(os.clone(), proc, tail).await;

For a single function call, the body runs first (lines 4068-4076) and its output is collected only if capture (line 4107). proc.capture is false here, so the body has already written to the real stdout. capture_pipeline_checked (lines 1918-1933) sets proc.capture and moves stdout aside before it runs a pipeline; this call site doesn't.

I could not reproduce it through Shell::run: the differential harness, which uses Shell::run, passes with the #359 forms of these scripts. I did not test the Python, Node.js or WASM bindings.

Possible Solution

Capture around the tail at line 2193 the way capture_pipeline_checked does. I ran this on a copy of #359 at 56ca14e: the scripts above and the #359 forms then match bash, and cargo test --workspace --all-targets gives 2733 passed, 0 failed.

-                    let (mut exit, out) = execute_pipeline_capture(os.clone(), proc, tail).await;
+                    let saved_capture = std::mem::replace(&mut proc.capture, true);
+                    let saved_output = std::mem::take(&mut proc.captured_output);
+                    let mut saved_stdout = Process::empty();
+                    proc.transfer_fd(STDOUT, &mut saved_stdout);
+                    let (mut exit, mut out) = execute_pipeline_capture(os.clone(), proc, tail).await;
+                    proc.capture = saved_capture;
+                    proc.close(STDOUT);
+                    saved_stdout.transfer_fd(STDOUT, proc);
+                    out.push_str(&std::mem::replace(&mut proc.captured_output, saved_output));

A test for it has to run the binary (as tests/shell_integration.rs does with CARGO_BIN_EXE_strands-shell), since differential cases go through Shell::run.

Related Issues

#359, #337. #379 is a different path (a redirect written directly on a function call, with output writers installed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions