Skip to content

fix(lexer): word-level $(( cmd ) || cmd) ran the )) arithmetic scan at six sites, swallowing the enclosing word (rubash#2096) - #2099

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/nvm-ls-syntax
Oct 29, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/nvm-ls-syntax

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #2096.

Root cause

Word-level $(( cmd ) || cmd) — a command substitution of a subshell-or-list — was classified as arithmetic at six lexer scan sites, and skip_arith_paren's ))-pair hunt (no )) exists in this construct) consumed past the enclosing quote to end of input: one-line top-level assignments were silently swallowed whole; inside a function body it surfaced as syntax error: unexpected end of file from '{' / `while'.

Real-world face: nvm v0.40.3 (nvm.sh:1336 ALIAS_TEMP="$( ( ... ) || nvm_echo)") reprinted spaceless by the async respawn prelude — every nvm ls spawned 6 background children whose -c source failed to parse → exactly 6 lines of niu: -c: line 103: syntax error near unexpected token `)' per invocation (GNU: 0) and the default-alias lines (iojs -> N/A (default) …) never printed.

GNU specification (vendored bash.git @9c465866, Bash-5.3 patch 20)

  • parse.y:4451 parse_comsub(), 4464-4473: word-level $( peeking ( hands the word to parse_matched_pair(qc,'(',')',P_ARITH) — a plain balanced nesting match (parse.y:3898 count = 1; 3998-4005 count++/count--). Never a )) hunt at word level.
  • The balanced-group-then-require-) arithmetic verdict exists only at command position: parse.y:4900 parse_dparen() → parse.y:4970 parse_arith_cmd() (4970-4996).
  • parse.y:4183-4188 (inside parse_matched_pair): a dquoted $( recurses into parse_comsub; the top-level ) is quoted text and the closing " still delimits.

Fix

Apply the existing dollar_double_paren_is_arithmetic gate — the same predicate the token-start site has used since #512 — at the six sibling sites that classified $(( unconditionally:

  • src/lexer/skip.rs: skip_cmd_subst, skip_double, skip_braced (both arms), run_skip_brace
  • src/lexer/word.rs: skip_word_inner

This converges the whole class onto one predicate (no new symptom checks); arithmetic-true forms keep the unchanged skip_arith_paren path.

Evidence

  • Minimized A/B (script file, GNU 5.3.20 vs fixed binary): 17-case battery stdout byte-identical, including the o-series reproducers and the exact serialized nvm construct.
  • tests/issue2096_dollar_dparen_subshell_or_list_regressions.rs: 8 pins (dquoted, function-body, mid-word, ${}-default, nested-comsub, the serialized nvm shape, arith-true guard, EOF diagnostics).
  • End-to-end: non-interactive nvm ls syntax errors 6 → 0, exit 0, default-alias lines restored; psmux real-tty face with niu (product) rebuilt against this engine — 0 syntax-error lines, alias lines restored, prompt returns.
  • 83-suite true-baseline ledger: fixed binary vs pristine origin/master (5edad05) — all 82 static suites byte-identical (1015 = 1015 diff lines); the single moving line, coproc 4 -> 8, is a WSL GNU-side timing flake: the 8-line gnu.out variant reproduces on the pristine binary too (alternating reruns: 4,4,4 on fixed/pristine/fixed, 8 on pristine), while the rubash-side rb.out is byte-identical across every run.
  • Branch is rebased on current master tip b41150e (master moved hourly during the task; initial baseline 5edad05).

…t six sites, swallowing the enclosing word (rubash#2096)

GNU classifies a word-level $(( as arithmetic only when the second '('s
balanced group closes right before ')': parse.y:4464-4473 parse_comsub
hands the word to parse_matched_pair(P_ARITH), a plain nesting match
(parse.y:3898 count=1), and the balanced-group-then-require-')' verdict
lives only at command position (parse.y:4900 parse_dparen -> parse.y:4970
parse_arith_cmd). Inside double quotes the nested $( recursion
(parse.y:4183-4188) keeps the top-level ')' as quoted text.

The token-start site has gated on dollar_double_paren_is_arithmetic since
#512; skip_cmd_subst, skip_double, skip_braced (both arms), run_skip_brace
and skip_word_inner classified $(( unconditionally and skip_arith_paren's
))-pair hunt then consumed past the closing quote to EOF. nvm v0.40.3
reprints nvm.sh:1336 ALIAS_TEMP="$( ( ... ) || nvm_echo)" spaceless into
every respawned background child's -c source, so each nvm ls emitted six
'syntax error near unexpected token )'' lines and lost the default-alias
output; GNU emits none.

All six sites now share the same predicate; arithmetic-true forms keep the
unchanged skip_arith_paren path.

Formatting: applied rustfmt directly to the three touched files (cargo fmt
aborts on this Windows tree with OS error 206 - filename too long - the
documented hook precedent); commit made with --no-verify for that reason
only.

Verification: tests/issue2096_dollar_dparen_subshell_or_list_regressions.rs
8/8; 17-case GNU-diff battery stdout byte-identical; 83-suite
true-baseline ledger vs pristine 5edad05 identical except the coproc
GNU-side timing flake (8-line variant reproduced on the pristine binary);
nvm ls end-to-end 6 -> 0 syntax-error lines, alias lines restored, on the
psmux real-tty niu face rebuilt against this engine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant