Skip to content

fix(parser): echo the source line after ! in reserved position (rubash#3647) - #3765

Merged
caomengxuan666 merged 1 commit into
masterfrom
ai-fix/rubash-3647
Nov 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
ai-fix/rubash-3647

Conversation

@arookieofc-ai

Copy link
Copy Markdown
Contributor

Fixes #3647.

Summary

echo hi | ! true printed only the diagnostic; GNU also echoes the offending source line. This PR restores the echo for the !-in-reserved-position shape (rc stays 2):

$ rubash -c 'echo hi | ! true'
rubash: -c: line 1: syntax error near unexpected token `!'
rubash: -c: line 1: `echo hi | ! true'

Root cause

The !-after-pipe arm in src/parser/token_actions.rs (parse.y:1408-1413, rubash#360) hand-rolled a __RUBASH_PARSE_ERROR_NEAR__ node with the token + line but no __RUBASH_PARSE_SOURCE__ assignment, so the executor's NEAR arm printed the diagnostic with no line to echo. A scan of src/parser/*.rs showed it was the only NEAR producer without the source assignment. The fix attaches it via offending_line_text, mirroring the Pipe arm in the same function (+14/-0 in that file). No other error shape changes: EOF reports still echo nothing, and legal ! positions are untouched.

GNU oracle (bash 5.2.21, Simular box)

Echo happens for every "near unexpected token" report and echoes the physical line verbatim (leading/trailing spaces and trailing comments survive); the echoed line is the one at the reported line number (multi-line input echoes line 2 only, after line 1 ran). "syntax error: unexpected end of file" and "unexpected EOF while looking for matching" reports echo nothing. Post-fix, an 18-case GNU-vs-rubash -c matrix matches byte-for-byte after program-name normalization (seed, second-stage / bare / mid-pipeline / |& bangs, | |, && |, time { }, >>|, multi-line, spacing, comment, echo hi; !, ! ! true, unclosed backtick).

Verification (box, baseline afd0778)

  • New suite tests/issue3647_bang_after_pipe_source_echo_regressions.rs: 10/10 on the fixed tree; fail-first 8 failed / 2 passed on the unpatched tree (the 2 passes are the EOF no-echo guard and the legal-bang guard).
  • cargo test --lib: 1249 passed, 0 failed (single-threaded).
  • Neighbors: issue1840 8/8, issue305 4/4 + 14/14, issue551 9/9, parser_redirection_tests 68/68, issue360 6/9, issue2739 3/4, issue390 1/9, parser_tests 312/316 — the non-green ones are pre-existing failures unrelated to this change (see below).
  • cargo fmt --check and git diff --check clean.

Pre-existing failures (not introduced here)

  • issue360 (3), issue2739 (1), issue390 (8): actual vs expected differ only in the program-name prefix (/dev/shm/.../rubash: or .../ruline vs the stale bash: / bare line 1: the tests expect); message bodies — including echo lines — are byte-identical. This stale-bash:-prefix family predates [parser] syntax error after ! in reserved position does not echo the source line #3647.
  • parser_tests (4): coproc x3 + test_function_body_can_be_time_command unwrap None from the in-process parse() API. None of their inputs contains a ! token, so the touched branch (gated on token.value == "!") is unreachable for them; the parse results are identical with/without this patch.
  • This PR updates the one stale expectation it owns: bang_after_pipe_rejected in issue360 now expects the echo line (it still fails locally on the pre-existing prefix only, like its two siblings).

Out of scope (verified, not fixed)

  • if true; then: rubash reports syntax error: unexpected end of file from 'if' command on line 1 vs GNU's bare syntax error: unexpected end of file — confirmed present on the unpatched baseline, separate EOF-wording divergence.
  • Scope kept to the !-after-pipe arm; every other NEAR producer already echoed.

CI caveat

As with the recent P3 PRs, the new regression suite's only execution evidence is the box run above: the Rust-tests job is fail-fast and the "Run all integration test stems" step is skipped once the lib step goes red on the known master flaky (issue2875), so CI never executes the new file. That structural gap is unchanged by this PR.

…ash#3647)

The bang-after-pipe arm in token_actions.rs hand-rolled a __RUBASH_PARSE_ERROR_NEAR__ node with the token and line but no __RUBASH_PARSE_SOURCE__ assignment, so the executor NEAR arm printed the diagnostic with no line to echo, while GNU echoes the offending source line. It was the only NEAR producer in src/parser without the source assignment; the fix attaches it via offending_line_text, mirroring the Pipe arm in the same function. No other error shape changes: EOF reports still echo nothing, and legal bang positions are untouched.

Regression tests: tests/issue3647_bang_after_pipe_source_echo_regressions.rs (10 tests: seed, second-stage, bare, mid-pipeline and pipe-err bangs, multi-line offending-line-only, spacing/comment verbatim, script file, EOF no-echo guard, legal-bang guard). Also updates the stale bang_after_pipe_rejected expectation in tests/issue360_parser_overlax_regressions.rs to the fixed output.
@caomengxuan666 caomengxuan666 added bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger P3-low 低:优化、文档、远期规划,有机会再做 area:parser lexer/parser: heredoc collection, comsub body re-parse, quoting state labels Nov 16, 2026
@caomengxuan666 caomengxuan666 self-assigned this Nov 16, 2026
@caomengxuan666

Copy link
Copy Markdown
Member

Thanks for the PR — and for the thorough write-up with the GNU oracle matrix and the fail-first numbers.

Per the owner's rule, external PRs are reviewed by the captain first, so this one is queued for captain review and CI is being watched. No merge and no requested changes from this first pass.

First-pass read of the diff (3 files, +239/−1):

  • The fix itself is small and scoped: the !-after-pipe arm in src/parser/token_actions.rs now attaches __RUBASH_PARSE_SOURCE__ via the same offending_line_text helper the Pipe arm already uses, so the executor's NEAR arm has a line to echo. Nothing else in the parser changes, and the change is gated on the ! token, matching the claim that legal ! positions are untouched.
  • The one-line expectation update in tests/issue360_parser_overlax_regressions.rs (bang_after_pipe_rejected now expects the echo line) follows directly from the fix.
  • The new suite tests/issue3647_bang_after_pipe_source_echo_regressions.rs has 10 tests covering the seed case, second-stage/bare/mid-pipeline/|& bangs, multi-line (offending line only), spacing/comment preservation, script-file echo, plus the two guards that matter most: EOF errors still echo nothing, and legal ! positions stay legal. That matches what the PR body describes.

CI at the time of this check (runs 43816319393 / 43816319597): API ledger tripwire SUCCESS, Shell functionality tests SUCCESS; Rust tests, Compatibility CLI tests, goldens, cross-target checks and the Windows compile check still in progress. As the PR body itself notes, the new stem is not executed by CI's nextest table, so the 10/10 and fail-first 8/2 figures rest on the author's box run for now — the captain's re-run is the deciding evidence, not CI.

Two points flagged for the captain's review (not blockers from this pass):

  1. The disclosed pre-existing failures (issue360 ×3, issue2739 ×1, issue390 ×8 — program-name prefix only, bodies byte-identical; parser_tests ×4 coproc/None unwrap) are attributed by the author to the baseline, not to this change. Worth confirming on the captain's re-run before merge.
  2. The out-of-scope note — if true; then EOF wording differs from GNU — is a separate divergence; if it is not already tracked, it may deserve its own issue rather than being folded into this PR.

Collision check: no other open PR touches src/parser/token_actions.rs or tests/issue360_parser_overlax_regressions.rs, and #3647 has no other in-flight PR, so this one stands alone on its files.

Thanks again — the captain will take it from here.

@caomengxuan666

Copy link
Copy Markdown
Member

【WIN-AGENT-AUDIT】PR #3765 第1轮 — 建议:PASS
head: 37c5e80 base(merge-base): afd0778 文件: 3 个 +239/-1(src/parser/token_actions.rs +14、tests/issue3647_bang_after_pipe_source_echo_regressions.rs +224、tests/issue360_parser_overlax_regressions.rs 期望 +1 行)
关联 issue: #3647(状态:open,我核过的时间:2026-11-17 本地)

机制核查:

  • 与 GNU 一致:report_syntax_error 的 near-token 分支在非交互模式下调 print_offending_line()(parse.y:6864-6865,函数定义 parse.y:6814,回显 shell_input_line 原文)。PR 给 !-after-pipe arm 补 __RUBASH_PARSE_SOURCE__(token_actions.rs:1140-1153),与同函数 Pipe arm(token_actions.rs:~650)逐字同构;offending_line_text(parse_loop.rs:3518)按 tokens[index].position-line_offset 取物理行,无 source 时回退 token raw 重建。
  • 「唯一缺 source 的 NEAR 产生点」抽查属实:同文件 missing_redirect_target_node(:44-49)与 Pipe arm 均已有同款赋值;本 arm 确为漏网。
  • diff 无可疑改动;无桥接/翻译层。issue360 期望更新(+echo 行)与本修复一致。

腿:

  • 钉自跑(head 37c5e80,Windows):issue3647_bang_after_pipe_source_echo_regressions 10 passed / 0 failed;issue360_parser_overlax_regressions 6 passed / 3 failed——3 红(bang_after_pipe_rejected、escaped_conditional_operator_rejected、paren_array_element_rejected)actual vs expected 仅差 argv0 前缀(.../rubash.exe: vs bash:),消息体含新 echo 行逐字节一致,属 PR 已披露的存量 bash:-前缀族(rubash#2935 约定),非本 PR 账。
  • fail-first(base afd0778 + 同一钉文件原样):2 passed / 8 failed,红名 = {seed_bang_after_pipe_echoes_source_line, bang_after_pipe_second_stage_echoes, bare_bang_after_pipe_echoes, bang_after_pipe_mid_pipeline_echoes, bang_after_pipe_pipeerr_echoes, multiline_echoes_offending_line_only, echo_preserves_spacing_and_comment, script_file_echoes_source_line},与 PR 声称一致;守卫 {eof_error_has_no_echo, legal_bang_positions_stay_legal} 在 base 正确放行。base 上 8 红表现为 diagnostic 有、echo 无、rc=1(NEAR arm 缺 source 时走了回退路径),修复后 rc=2+echo,与 GNU 一致。
  • lib 双边:head 1351 passed / 1 failed / 4 ignored(失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state})/ base 1351 / 1 / 4(同名单点)/ 差集 ∅(本机环境基线)。
  • -D warnings:cargo check --locked --lib --bins 0 警告;cargo check --locked --tests 在 tests/regression.rs:5850/6009 fn run dead_code 处红 2 条——该文件不在本 PR diff(merge-base 同内容,#[cfg(unix)] 模块在 Windows 下死代码),属基线,非本 PR 账。
  • 卫生:rustfmt --edition 2021 --check 三涉文件 0 diff;git diff --check 净;marker-lint OK(117 consts/268 sites);utf8-boundary clean。
  • CI(head 37c5e80 run 43816319393):红 = GNU regression goldens / Rust tests / Rust tests (macOS)。逐名归因:Rust tests 与 macOS 均死于 lib 步 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(consumer_tests.rs:3503 前置条件签名,已知基线 issue2875,job 中止于集成钉之前);goldens job 的 lib 步同一签名(1351/1),其后的 upstream_suite_slice_snapshots 步 ok。无本 PR 新红。
  • GNU 对照:WSL bash 5.3.20(1)-release(oracle 级,非 winuxsh/Git Bash)。探针 target/u2-audit/p3765-gnu.sh(GNU)vs p3765-rubash.sh(rubash head):12/12 形状诊断行+echo 行+rc 逐字节一致(仅 argv0 前缀差):echo hi | ! true→diag+echo hi | ! true'(2)、| !、mid-pipeline、|&、second-stage、multiline 只回显 line2 且 a 已输出、首尾空格与尾注释原样保留、echo hi |EOF 无 echo(2)、合法!` 位 rc 1/1/0。

F 点:无。

续作记录:无(本轮未动代码)。

未跑/VOID 的腿:无。

@caomengxuan666
caomengxuan666 merged commit 44bd730 into master Nov 17, 2026
12 of 15 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member

【WIN-AGENT-AUDIT】PR #3765 第2轮 — 建议:PASS(已合并 squash 44bd7307481f93f2005b18cb1fa5f52b54801939)
head: 37c5e80ac944508be9fb4780f4fb7a76f89f98cb(自 U2 未变)base(merge-base): afd077811c8520c0639cfd9ce9d036b8e8862bbb
关联 issue: #3647(closed,2026-11-17T01:39Z completed)

V 波收网复核:

  • 钉自跑:issue3647_bang_after_pipe_source_echo_regressions 10/10 绿(本机实测,head 37c5e80 构建)。
  • GNU 对照(本轮独立复跑):WSL /usr/local/bin/bash 5.3.20(1)-release。
    • echo hi | ! true(脚本文件)→ 双方同:syntax error near unexpected token '!' + 回显源行 `echo hi | ! true' + rc=2。修复成立。
    • 合法 ! 位(! true→rc1=1、! false→rc2=0)双侧一致。
  • 「EOF 措辞」F 点核销:正文披露的 if true; then EOF 分歧系 oracle 版本差——GNU 5.2.21 输出裸 syntax error: unexpected end of file,5.3.20 输出 syntax error: unexpected end of file from 'if' command on line 1(third_party/bash/parse.y:6899 带 compoundcmd 子句 vs :6905 裸形)。rubash head 与 5.3.20 逐字一致。对 5.3 契约无分歧,无需 follow-up issue。
  • CI:红 {Rust tests, Rust tests (macOS), GNU regression goldens and canaries} = issue2875 基线 + 基线 goldens 归因。
  • lib 双边 / fail-first / 警告门 / 卫生:沿用 U2(同 head 同 merge-base,内容未变)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:parser lexer/parser: heredoc collection, comsub body re-parse, quoting state bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger P3-low 低:优化、文档、远期规划,有机会再做

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[parser] syntax error after ! in reserved position does not echo the source line

2 participants