Skip to content

fix(shell): join continued source lines without empty words - #402

Merged
pgrayy merged 9 commits into
strands-agents:mainfrom
pgrayy:fix/pgrayy/shell-line-continuations
Nov 12, 2026
Merged

pgrayy merged 9 commits into
strands-agents:mainfrom
pgrayy:fix/pgrayy/shell-line-continuations

Conversation

@pgrayy

@pgrayy pgrayy commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Description

Backslash-newline continuations could make a command run before its next physical line was read. Removing a continuation could also create an unintended empty argument or break grouped syntax.

For example, this is one printf command written across two physical lines:

printf '<%s>\n' first \
  second

Before the fix: Strands Shell executed the first line too early, with first and an accidental empty argument. It printed:

<first>
<>

It then treated second as a separate command, reported strands-shell: second: command not found, and returned exit status 127.

After the fix: Strands Shell collects the continued line and runs the command once with the two intended values, first and second. This is equivalent to printf '<%s>\n' first second:

<first>
<second>

It returns exit status 0 with no error. The example demonstrates both corrections: waiting for the continued line and avoiding an unintended empty argument.

The source executor now tells the shared parser when more physical lines remain. The lexer waits for a continued line and omits words with no parts. The lexer retains explicitly quoted empty words as tokens and preserves the existing end-of-input escape handling. Alias expansion bounds its cursor adjustment when replacements remove words.

Related Issues

Companion upstream fix: strands-agents/shell#243.

Reproduced from a user report; no linked issue.

Type of Change

Bug fix

Testing

  • Three focused regression tests cover source joining/control flow, empty lexer words/grouped commands, quoting/heredoc/alias boundaries, and a continuation at end of input. Each affected guard was removed separately; its regression failed, then the guard was restored.
  • All 24 exploratory scripts match Bash's status and stdout after the fix.
  • Optimized smoke: source nvm.sh --no-use && nvm --version succeeds and prints 0.41.0.
  • Shell all-features suite after merging current main: 2,005 passed, zero failed, three existing ignored child-helper tests exercised by their parents. No interpreter-dependent skips.
  • Shell all-features build, no-default-features build, formatting, and all-targets/all-features clippy passed. Vendored warning diagnostics are unchanged.
  • scripts/test-all.sh box-examples built Box and containment with all features. It reports an explicit skip because no scripted examples remain.
  • Deterministic containment: GREEN, 129 passed, 12 declared Linux-only skips on macOS, zero failures or errors, full expected coverage (141 cases).
  • Independent code review found an alias cursor underflow exposed by empty replacements; the fix and a regression case are included. No remaining findings.

Some quoted empty arguments are still lost during execution, including literal empty double quotes and empty expansions of unset parameters. This pre-existing defect also affects ordinary nvm initialization and was reproduced without continuations. Repeated parsing of large statements, and continuations inside operator or variable-name tokens, remain outside this fix.

Full workspace Rust tests and the live tutorial were not run locally. Local verification was on macOS. The change is confined to the vendored Shell parser/executor and touches no platform-specific code.

  • I ran the relevant suites (package gates listed above)
  • If I touched Rust, I ran formatting and clippy (package gates listed above)

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
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

## Problem

A source statement ending in an unquoted backslash ran before the next
physical line was read. Removing a backslash-newline could also emit an
empty word, which added arguments and broke the grouped condition in
nvm 0.41.0.

## Solution

Pass physical-line availability from the source executor to the shared
parser. The lexer marks an unfinished escape as incomplete only while more
input remains. Omit words with no parts after continuation removal, and
bound the alias cursor adjustment when replacements remove words.

End-of-input escape handling and explicitly quoted empty arguments keep
their behavior. No public interface, dependency, policy, or limit changes.
Record the upstream bug and remaining limitations in UPSTREAM.md.

## Tests

line_continuations_join_source_lines checks command and argument joining,
pipeline input, and short-circuit evaluation through direct execution,
dot-source, and sh. line_continuations_do_not_create_words checks exact
arguments, the reduced nvm condition, and quoted empty arguments.
line_continuations_preserve_quoting_and_input_boundaries checks quotes,
escaped backslashes, comments, heredoc sequencing, EOF, and empty aliases.
All three fail before the fix. Separate mutations of source accumulation,
empty-word removal, and alias bounds each fail their regression.

All 2,002 shell tests pass with all features, including doctests. The three
ignored child-helper tests are invoked by their parent tests. All-features
build, no-default-features build, formatting, and all-targets/all-features
clippy pass. The 14 vendored clippy diagnostics match the baseline.

All 24 exploratory scripts match Bash status and stdout. An optimized
build sources nvm 0.41.0 with --no-use and prints its version.

scripts/test-all.sh box-examples builds Box and containment with all
features, then reports that no scripted examples remain. Deterministic
containment is GREEN: 129 pass, 12 declared Linux-only skips on macOS,
zero failures or errors, and full expected coverage across 141 cases.
Independent feature review found no remaining issues.

Full workspace tests, live tutorial, and other platforms were not run.
Default nvm initialization still fails due to the pre-existing loss of
quoted empty parameter arguments, reproduced without continuations in
both builds. Large statements still incur repeated parsing, and separate
lexer collectors still mishandle continuations within operators or
variable names.
@github-actions github-actions Bot added Area - Shell/Monty bug Something isn't working labels Nov 11, 2026
@pgrayy
pgrayy marked this pull request as ready for review November 11, 2026 17:33
Comment thread crates/shell/tests/shell_integration.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Comment

The core fix is correct. I probed about 40 continuation scripts against the base and Bash, and every row in the main path improved or stayed the same. The one open finding, posted inline, is about end-of-input \ rows that assert results Bash does not produce.

Review Categories
  • Correctness (held): An unquoted trailing \ followed by another physical line is always a continuation. Comments, single and double quotes, $(…), backticks, and even backslash runs all reach the right branch.
  • Correctness (held): A word with no parts can only come from the \ arm. The first character of a word is always consumed, so the new continue cannot loop forever.
  • Correctness (held): more_input = i < lines.len() is safe with heredocs, because tokenization fails before resolve_heredocs reads any line.
  • Correctness (held): saturating_sub removes a real panic that predates this PR. The base panics on alias a=''; alias b=''; a b echo after.
  • Pre-existing, unchanged: These behave the same on the base and the PR, and both differ from Bash:
    • a heredoc inside a multi-line compound (if/for/function body);
    • continuation inside an unquoted heredoc body.
  • Docs: The new limitation in UPSTREAM.md ("quoted empty parameter expansions can lose arguments") names the wrong case:
    • A literal "" is dropped even on one line: printf "<%s>" a "" b gives <a><b>.
    • An empty "$x" is kept: <a><><b>.
    • So the parser-level '' \⏎ "" row passes, but that argument still disappears when the command runs.
  • Rules (held): The divergence is recorded in UPSTREAM.md, modification headers are present, and no frozen Box surface is touched.

The regression tests are well chosen, and the alias underflow fix was a good catch.

zastrowm
zastrowm previously approved these changes Nov 11, 2026
@github-actions

Copy link
Copy Markdown

Assessment: Approve

Re-reviewed at 974d4c1. The only change since e64c752 is in tests and docs, and it addresses the earlier finding. parser.rs and exec.rs are unchanged, so the earlier correctness results still apply.

  • Held: The continuation tests pass and cargo fmt --check is clean. The alias underflow guard is proven: with the old subtraction put back, the new row panics.
  • Held: The UPSTREAM.md limitation now describes the real defect: "some quoted empty arguments are lost during execution". A literal "" argument is dropped while an empty "$x" is kept.
  • Held: The parser and executor are unchanged from the previous review, so its results still stand: continuation detection, skipping words emptied by continuation removal, and heredoc ordering.

@github-actions

Copy link
Copy Markdown

Assessment: Approve

Re-reviewed at eed0f0b. This is a merge of main (up to 82c2889). Compared with main, the PR still changes only parser.rs, the parse_source call in exec.rs, the continuation tests, and its UPSTREAM.md section. All four match what I approved at 974d4c1.

@github-actions

Copy link
Copy Markdown

Assessment: Approve

Re-reviewed at d3628cc. This is a merge of main (up to a41332d). Compared with main, the PR still changes only parser.rs, the parse_source call in exec.rs, the continuation tests, and its UPSTREAM.md section. All four match what I approved at 974d4c1.

@pgrayy
pgrayy enabled auto-merge (squash) November 12, 2026 14:25
@pgrayy
pgrayy merged commit 890665c into strands-agents:main Nov 12, 2026
15 checks passed
@pgrayy
pgrayy deleted the fix/pgrayy/shell-line-continuations branch November 12, 2026 14:51
ferdingler added a commit that referenced this pull request Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area - Shell/Monty bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants