Skip to content

parser: heredoc + pipeline inside a quoted "$( ... )" fails to parse #703

Closed

Description

@caomengxuan666

Summary

A heredoc that feeds a pipeline, inside a double-quoted command substitution, fails to parse.

Environment: Niubash 1.4.0 · rubash git e785c3c8f610 — Windows 10 x64.
Method: differential vs GNU bash 5.2.21(1) / 5.3.0. The probe file is LF-only, so this is not a CRLF artifact.

Repro

$ echo "X=$(cat <<EOF | cat
body
EOF
)"
niu : command substitution: line 4: unexpected EOF while looking for matching `)'
      (exit 1, no output)
bash: X=body

What does work

  • Unquoted assignment: x=$(cat <<EOF | cat …) works.
  • Quoted, but without the pipeline in the substitution: works.
  • So the trigger is the combination: quoted "$( … )" + heredoc + pipeline inside.

The parser appears to lose the heredoc body's line accounting when the substitution is inside double quotes and contains a pipeline.

Activity

  1. added
    bug
    P2-medium
    on Oct 17, 2026
  2. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Version note (correction to the Method line): the oracle was reached with bare wsl.exe -e bash, which resolves through PATH to /usr/local/bin/bash = GNU bash 5.3.0(1) — not 5.2.21. The 5.2.21 build lives at /bin/bash, and every item in this report was re-checked on it as well; results were identical on both.

    So the two versions are listed in the wrong order above: 5.3.0 was the default, 5.2.21 the cross-check.

    No finding here changes; noting it only so the numbers reproduce. (Incidentally 5.3.0 is also the oracle cited in rubash#641, "the repository oracle is WSL 5.3.0".)

  3. added
    area:parser
    family:S4
    family:S5
    on Oct 17, 2026
  4. caomengxuan666 commented on Oct 18, 2026

    @caomengxuan666
    MemberAuthor

    Fixed in 041f0a6.

    Root cause was not line accounting but the delimiter WORD. The word-expansion comsub collector (collect_command_substitution_source_ex, src/executor/embedded_mutations.rs) scanned the heredoc delimiter to end of line, so cat <<EOF | cat declared the delimiter EOF | cat. The EOF terminator line then never matched, the collector swallowed the rest of the script looking for it, and the unclosed ) surfaced as the unexpected EOF fatal. The three shapes the report lists as working all hand the collector a header line with nothing after the delimiter word, which is why only quoted + heredoc + pipeline went red.

    The delimiter word now ends at the first unquoted blank or shell metacharacter (;|&()<>), the rule read_token_word already applies (parse.y:5688 shellbreak) and the rule the lexer's own heredoc header scan already implements (lexer/heredoc_scan.rs skip_heredoc_in_chars_core). << immediately followed by - is one operator word carrying the tab-strip dash, and blanks between << and the word are skipped: cat << EOF names EOF, cat << -EOF names -EOF without stripping, same as GNU.

    Fixing the scan exposed a second defect on the EOF) pushback shape (x=$(cat <<EOF | cat + body + EOF)): the substitution output carried a stray EOF line. The lexer marks a heredoc whose ) closes on the delimiter line by splicing an IFS_GLUE byte before that ) (make_cmd.c:602-611 shape); the typed fast path consumes that signal, but the marker also reached the real parser, where the terminator line read EOF\x1c and stopped matching its delimiter. The marker is now stripped after the typed path and before every real-parser path.

    Pinned in mod issue_703 (tests/regression.rs) per #746: 703.1 the exact repro [ratchet], 703.2 the three already-working shapes [control], 703.3 the EOF) pushback shape [ratchet], 703.4 the delimiter spellings << EOF / <<-EOF / <<"E O" [ratchet], 703.5 a ; after the delimiter word [ratchet]. Reverting the collector fix turns 4 of the 5 red; the survivor is the 703.2 control.

    One gap deliberately left: bash's own behaviour for << -WORD inside a command substitution is self-contradictory (single blank strips, double blank treats the dash as text, and the reported EOF line depends on the terminator line), so there is no oracle to match. rubash stays consistent with its own lexer rules and with bash's top-level behaviour.

    The same-rule-twice drift behind this bug is recorded in docs/pitfall-taxonomy-and-governance.md (S4 row plus structural conclusion 5): the lexer scan and the comsub collector each carried their own copy of the delimiter word-boundary rule, and the copies had drifted.

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

    P2-mediumarea:parserbugfamily:S4family:S5

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions