Skip to content

fix: report the first syntax error in multiline input - #197

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/sourced-parse-error
Nov 4, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/sourced-parse-error

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Multiline input (anything passed to run with a newline in it, plus every sourced or sh script) is parsed a statement at a time, and when a statement didn't parse the shell assumed it was unfinished and kept reading lines. So a real syntax error got buried under whatever came later: with echo one, then ), then echo "two, you got told about the open quote with no line number instead of the ) on line 2.

Now the shell tells a real syntax error apart from input that just isn't finished yet, stops at the first real error and reports the line it was on, like bash does, and the exit status stays 1. Unfinished input at the very end still gets the same message as before, a line ending in a backslash still continues, and single-line input is unchanged, so the visible change is that multiline syntax errors now start with the line number.

Evidence of how this is fixed in Box

Strands Box carries a vendored copy of Shell and partly fixed this: it also stops at the first real error and names its line (UPSTREAM.md note). But a line ending in a backslash before a keyword (for i in a; \ then do …) fails to parse there, and 3 of this PR's 6 tests pass on it.

Related Issues

None.

Documentation PR

None.

Type of Change

Bug fix

Testing

I ran about thirty valid multiline shapes on main and with the change, and the only differences were scripts that already failed, which now fail earlier and name a line. A review caught that a backslash before a keyword like do broke, so I fixed that and added a test, and the new tests fail without the fix while the Rust, Python and Node suites pass. Separate bugs I found and left alone: a here-doc inside a multiline if or loop is also parsed as code, f() with { on the next line doesn't parse, and false && at the end of a line still runs the next line.

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

Multiline input, and every sourced or sh script, is parsed one
statement at a time. When a statement did not parse, the executor
read the next line and tried again, as if the statement was
incomplete. A real syntax error was then hidden: for
`echo one; )` followed by a line with an open quote, the shell
reported the unterminated quote instead of the `)`, with no line.

The executor now tells a definite syntax error from input that is only
incomplete. It stops at the first definite error, reports the line
where it found it, and returns status 1 as before. A line that ends in
a backslash still joins the next line. Incomplete input at the end of
the text keeps its old message.
@ferdingler
ferdingler requested a review from a team as a code owner November 2, 2026 18:24
@ferdingler
ferdingler merged commit dc1e58f into strands-agents:main Nov 4, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants