Skip to content

fix(driver): keep the group's final physical newline for \<newline> collapse (Refs #3605) - #3673

Merged
caomengxuan666 merged 1 commit into
masterfrom
devin/grouped-trailing-newline-continuation
Nov 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
devin/grouped-trailing-newline-continuation

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

run-parser tail (parser1.sub): eval "array=(foo bar)" ; echo AAA\<newline> printed AAA\ where GNU prints AAA. GNU's reader sees the physical line's newline byte — parse.y:5379 read_token_word's \ + peek \n goes to next_character and the pair disappears; only \<EOF> (no newline in the input) stays a literal backslash.

The grouped reader's run_history_group rebuilt exec text by joining the group's stripped per-line texts with \n, so the last line's real \n never reached the re-parse: \<newline> re-lexed as \<EOF> and the backslash survived as literal. This restores the byte — the group's pending span (raw lines verbatim) already records whether the last line carried its terminator. For every other shape a trailing newline after a complete command is an inert separator, so the append is a no-op.

Refs #3605 (class B, run-parser — clears the suite's only diff).

GNU anchors

  • parse.y:5379 read_token_word — \ + peek_char == '\n' -> goto next_character (pair elided from the token).
  • parse.y:5376 shell_getc — the reader sees the real newline; \<EOF> takes the else arm and keeps the backslash (quoted).
  • evalstring.c parse_and_execute — incremental reader; the same collapse applies on the final physical line.

Verification (macOS arm64, GNU bash 5.3.20 oracle)

Test plan

  • cargo test --test issue3605_grouped_trailing_continuation
  • niubash upstream gate run-parser PASS
  • cargo test --test session_api --test issue414_posix_reserved_word_alias_regressions --test issue1151_heredoc_delimiter_warning_regressions

… collapse (Refs #3605)

run-parser tail (parser1.sub): `eval "array=(foo bar)" ; echo AAA\\\<newline>\'
printed `AAA\` where GNU prints `AAA`. GNU's reader sees the physical
line's newline byte — parse.y:5379 read_token_word's `\` + peek `\\n`
goes to `next_character` and the pair disappears; only `\\<EOF>` (no
newline in the input) stays a literal backslash.

The grouped reader's run_history_group rebuilt exec text by joining the
group's stripped per-line texts, so the final line's real `\\n` never
reached the re-parse: `\\<newline>` re-lexed as `\\<EOF>` and the
backslash survived. Restore it — the group's pending span (raw lines
verbatim) already records whether the last line carried its terminator,
and a trailing newline after a complete command is an inert separator.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3673 第2轮 — 建议:PASS(已 squash 合并 23102adf,不关单)
head: 7a0feed base(merge-base): 96802ca 文件: 2 个 +86/-1
关联 issue: #3605(open,umbrella 目录 issue — 按铁律不关单;核于 2026-11-16 19:5x 本地)

机制核查:

  • GNU parse.y:5376-5385:read_token_word 里 \ 后 peek_char = shell_getc(0),if (MBTEST(peek_char == '\n')) { character = '\n'; goto next_character; } —— 反斜杠-换行的成对消去依赖「读者真的看到了 physical line 的换行字节」;只有 \<EOF>(输入里没有换行)才留字面反斜杠(goto got_character 分支)。
  • 病根在 rust 侧的重拼接:read_next_source_group 把每个 raw line 的 \n 剥进 per-line text,run_history_group 再用 \n 重新 join —— 最后一行的真实换行字节没进 re-parse,\<newline> 被当成 \<EOF>,echo AAA\ 于是打印 AAA\。
  • 改动把「group 末尾是否带真实换行」作为显式参数(pending.ends_with('\n'),两个调用点 run_script_grouped_at_line_offset 与 run_interactive_stdin 都传),在 re-parse 前补回一个 \n。这是把读者见过的字节流还原,而不是对 \ 做特判;对任何完整命令,末尾换行只是惰性分隔符。
  • diff 只碰 src/script_driver.rs + 新测试;无顺手改无关面;无桥接/翻译层;无「删除了什么」小节(仅 1 行签名替换,可豁免)。

腿(逐项给数字与名字):

  • 钉自跑(head):cargo test --test issue3605_grouped_trailing_continuation 在 Windows 上 0 passed(该文件首行 #![cfg(unix)],与仓库 209 个既有 cfg(unix) 测试文件同惯例,Windows 侧整 crate 为空)。为拿到本地判别证据,我只在本地临时删掉该 cfg 行(未提交、跑完 git checkout -- 还原)后跑:3 passed; 0 failed(issue3605_last_line_backslash_newline_collapses / issue3605_fast_path_last_line_backslash_newline_collapses / issue3605_backslash_at_eof_stays_literal)。
  • fail-first(base 96802caf,同一去掉 cfg 的钉文件原样放入):2 passed; 1 failed —— 红名 = {issue3605_last_line_backslash_newline_collapses},断言差异 left: "AAA\\\n" vs right: "AAA\n",与 PR 声称的 grouped-driver 面一致。另两钉在 base 上本就绿(它们是守卫钉:fast path 一致性、\<EOF> 保持字面反斜杠),非本修复的判别钉,按设计保留。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state}(1350 passed/1 failed/4 ignored);base 同集 {同一条};差集 ∅。该条两侧同红,环境/宿主态绑定,不记本 PR 的账。
  • -D warnings(cargo check --all-targets):rc=101,唯一 error 是基线告警 tests/issue1331_1334_signal_and_startup.rs:11 function run_script is never used — 非本 PR 文件;本 PR 两文件 0 告警。
  • 卫生:rustfmt --check 两文件 clean;check-marker-literals.py → OK;check-utf8-boundary-hygiene.sh → clean。
  • CI(head 7a0feedd,run 43702907592):红 = Rust tests、Rust tests (macOS)、GNU regression goldens and canaries,三条的唯一 FAIL 都是 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(consumer_tests.rs:3503 前置 "no rubash binary found")→ 全部 issue2875 基线签名,非本 PR;无 E0063/编译类红。注意 Linux 的 Rust tests 在 issue2875 处早停并 cancel(1223/1249 tests were not run),所以新钉在那次 CI 里没有跑;本地 Windows 去 cfg 实跑已补上该绿腿。
  • GNU 对照(WSL GNU bash 5.3.20(1)-release, x86_64-pc-linux-gnu):12 形矩阵(stdout + rc 双比,脚本 .audit-s6/gen-m3673.sh、.audit-s6/m3673/c*.sh)全部一致:
    • parser1.sub 尾形 eval "array=(foo bar)" ; echo AAA\+newline → 双方 AAA\n、rc0(修前 rubash 为 AAA\)
    • fast path true ; echo AAA\+newline → AAA\n;\<EOF> 无换行 → AAA\\\n;双反斜杠 → AAA\;\<newline> 续行 AA\+BB → AABB;函数体内尾随续行 → Z;here-doc 体内行尾 \ → AAAEOF(双方一致)
    • 无尾随换行、纯换行、以及尾随续行后跟空行 → 均字节一致

F 点:无。

未跑/VOID 的腿:

  • 钉的 Windows 原样 run 为 0 tests(#![cfg(unix)])→ 已用「本地去 cfg」方式补跑并如实披露;该改动未提交、已还原。
    平台局限:GNU 对照为 WSL Linux 5.3.20;结论不依赖平台专属行为。

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.

1 participant