Skip to content

fix: join && and || line ends in sourced files - #248

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/source-line-join
Nov 12, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/source-line-join

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Description

In a sourced file (. file, source file, or sh file), a line ending in && or || was treated as a finished command. So false && with echo hi on the next line still printed hi, and the same with true ||. Bash and zsh join the next line, so the guarded command doesn't run. A script that relies on check && to gate the next line would run that line anyway.

Parse errors in sourced files also didn't say which file they came from, and an unclosed statement at the end of a file gave no line at all. Now the shell joins the next line after a trailing && or ||, and a parse error names the file and the line, the way bash does. A stray ( that can't open anything now stops the file right there, instead of reading on to the end and reporting some unrelated error from further down. Earlier PRs fixed other parts of sourced-file parse errors; this covers what they left.

I added tests for ., source, and sh that check the guarded command doesn't run, the exit status, the file and line in the error, and that a parse error stops the file while the calling script keeps going. I also added a differential case recorded from real bash. Every new test fails on main and passes with the change. I compared the output with bash 5.2 by hand: same output, same file and line, same lines skipped. One difference stays: bash exits 2 on a syntax error, and Shell keeps exiting 1 as before. The full Rust suite, fmt, clippy, and docs pass, with no new warnings. I didn't run the Python or Node suites because the bindings aren't touched.

Related Issues

#212 (sh -c) also touches the code that runs a script file, so whichever lands second may need a small rebase.

Documentation PR

None.

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

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

…arse errors

A sourced file (`. file`, `source file`, `sh file`) ran each line that
ended in `&&` or `||` as a complete statement. So `false &&` followed by
`echo x` on the next line ran `echo x`. Bash joins the next line, so the
guarded command does not run. The loop now reads more lines while the
parsed statement ends in `&&` or `||`.

A parse error in a sourced file did not name the file, and an
unterminated statement at end of file gave no line. The error now shows
`<file>: line <n>:`, as bash does. A `(` that cannot open anything is now
a definite error, so the file stops at it and does not consume the rest
of the file.
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 00:47
@priyasingh2111

Copy link
Copy Markdown
Contributor

Non-blocking: looks good. Two older gaps in the same area, if you want them here or in a follow-up:

  • A heredoc line ending in && or || still runs the next line. false <<E &&, then b, E, echo SKIP prints SKIP; bash skips it.
  • A file that ends in && or || runs with status 0, where bash reports syntax error: unexpected end of file.

Both behave the same on main, so neither is a regression.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for checking these! I reproduced both, and you're right that main does the same thing. I'm going to keep this PR to the two fixes it has, so I'm leaving these for a follow-up.

@ferdingler
ferdingler merged commit 5740518 into strands-agents:main Nov 12, 2026
30 checks passed
@ferdingler
ferdingler deleted the fix/source-line-join branch November 12, 2026 04:37
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