Skip to content

[BUG] A here-doc inside if, a loop, a group, a subshell, case or a function takes the wrong lines as its body #333

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell
  • 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

main @ c28e1fb (crate version 0.0.0)

Language Runtime Version

Rust 1.100.0

Operating System

Amazon Linux 2023 (aarch64)

Installation Method

built from source

Steps to Reproduce

Run this script with Shell::run, or with strands-shell -c:

if true; then cat <<EOF
body
EOF
fi
echo after

Expected Behavior

As in bash 5.2.15, status 0 and empty stderr:

body
after

Actual Behavior

Status 127. cat receives the line after fi as its body, and the real body and delimiter run as commands:

echo after
strands-shell: body: command not found
strands-shell: EOF: command not found

Additional Context

Found while re-reviewing #325. It is not caused by that PR.

I ran the script two ways:

  • Through Shell::run at c28e1fb, as a temporary case in tests/differential/cases/. That gave the output above.
  • Through a debug build of 55d7816 with -c, which gave the same output. I also ran the forms below on that build.
Here-doc inside Result on 55d7816
for i in 1; do cat <<EOF … done same wrong body and command not found lines
while true; do cat <<EOF … break; done same
{ cat <<EOF … } same
( cat <<EOF … ) same
case x in x) cat <<EOF … ;; esac same
f() { cat <<EOF … } then f prints nothing, including the echo after that follows
if true / then / cat <<EOF on separate lines same as the single-line if

Controls that work: a top-level cat <<EOF, and true && cat <<EOF.

I searched tests/*.rs and tests/differential/cases/*.sh for a here-doc on a command inside a compound body and found none. The closest is { cat; } > /out/important <<EOF (tests/shell_integration.rs:5995), where the here-doc is a redirect on the group itself.

Possible Solution

I did not trace the cause. The body is taken from the lines after the end of the whole compound command, so the reader that fills here-doc bodies appears to run after the compound's closing keyword has been read, not after the line that holds <<EOF.

Related Issues

#325

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