Skip to content

fix(print_comsub): never emit ';' after '&' in canonical comsub text (Refs #3662) - #3699

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3662-bg-job-in-comsub-serializer
Nov 16, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3662-bg-job-in-comsub-serializer

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

定义函数内命令替换里的后台作业永不执行:后台作业子进程是 respawned rubash -c,其程序为全部已定义函数序列化 ; -join 后加作业本体;executor 的 canonical comsub printer(src/executor/print_comsub.rs,GNU print_cmd.c port)对命令替换内的后台算符 & 吐出 & ;——& 后跟 ; 在两 shell 都是语法错,子进程死于解析,作业静默不执行(wait 仍返回 0)。

declare -f 输出本身正确:parse-time printer(src/parser/ast_print.rs)早有 background 分支;错只在 executor 的 ComsubPrinter。

GNU 锚点

  • print_cmd.c:257-272 & connector case:只打印 &,其后空格仅当有第二个命令时才有(c != '&' || second);GNU 树里 a & b 是 connector 本身就是 & 的单个 connection,不存在 & 之后的 ; connector 节点。
  • parse.y:1275/1290:& 之后只允许 newline_list。
  • print_cmd.c:1520 semicolon():buffer 以 & 结尾时不打印 ; 。

修复

src/executor/print_comsub.rs(仅此一文件 + 回归钉):

  1. background arm 由 &(尾空格)改为 &,与 print_cmd.c 及 ast_print.rs 一致;
  2. print_command_list 在前一命令为 background 时不走 print_connector(不打印 ; /换行),只补一个分隔空格并 skip_this_indent += 1,与 ast_print.rs 分支同形;boundary 仍按 consume-once 消费;
  3. 于是 semicolon() 的 ends_with(" &") 守卫也恢复生效——以 & 结尾的循环/if/函数体不再于 closer 处印出 & ;(同族)。

影响面(codegraph 核):print_comsub_text 唯一调用方是 canonicalize_comsub_spans,其调用方为 command_text.rs(background 序列化两处)、parameter_errors.rs 及 ComsubPrinter 自身(嵌套 comsub 递归);修法收敛于 comsub canonicalization,未动其他 list printer(bash_command_sequence_text 与 ast_print.rs 的 background 分支此前已正确)。

回归钉

  • src/executor/print_comsub.rs 单元测试 4 个:issue 原形 ": & put \"\\""、a & b; c、纯 ; 对照、循环体以 & 结尾;
  • tests/issue3662_bg_job_in_comsub_serializer_regressions.rs 3 个:issue 最小复现(仅定义函数 + : &)、回归种子功能半(bg-job-done 落盘)、无 comsub 函数对照。

验证状态(UNVERIFIED-BY-CAPTAIN)

  • rustfmt --check(两改动文件)通过;git diff --check 通过。
  • 以下腿因构建路线全断而未跑、全部待补:fail-first 钉红、修后钉绿、全量 lib 与 base 双边对齐、邻居 issue1664、RUSTFLAGS="-D warnings" cargo check --locked --lib --bins、GNU oracle 对照。原因:Simular box 钱包为空处于 hibernated,box_ensure 明确返回钱包为空无法唤醒;老服务器与 X299-2607 均须经 box 中转,故全部远程构建路线均不可用;本 VM 按纪律不本地构建。待钱包充值后由队长/Win-agent 在 X299(Windows 面)+ Linux 侧(GNU 对照)补跑。
  • Issue 要求的当前尖最小复现重验同样未做(无可执行二进制);修法立在代码级证据(错误文本与 ComsubPrinter 的 & + ; 连接逐字吻合)与 GNU 锚点上,须以实测腿确认。

本 PR 删除了什么

无(纯修复 + 回归钉;未删除任何功能、文件或公开面)。

Refs #3662

…(Refs #3662)

A background job inside $(...) in a defined function never ran: the
background child is a respawned rubash -c whose program is the defined
functions serialized ';'-joined plus the job's own command, and the
canonical comsub printer emitted '& ;' for the background operator.
'&' followed by ';' is a syntax error on both shells (parse.y:1275/1290:
only newline_list may follow '&'), so the child died at parse and the
job silently never executed.

GNU anchor: print_cmd.c:257-272 '&' connector case prints ' &', plus
one space only when a second command follows; semicolon()
(print_cmd.c:1520) suppresses after ' &'. The background arm printed
' & ' with a trailing space, which both fed '& ;' through the list
connector and defeated semicolon()'s ends_with(" &") guard, so a body
ending in a background command printed '& ;' at its closer too. The
fix mirrors ast_print.rs (whose background branch is why declare -f
was already correct): arm prints ' &', list connector after a
background command prints just a space, boundary still consumed.

Regression nails: unit tests on print_comsub_text (issue #3662 shape,
';'-joined follower, plain ';' control, loop body ending in '&') plus
integration stem tests/issue3662_bg_job_in_comsub_serializer_regressions.rs
(issue minimal repro + regression seed functional half + control).

UNVERIFIED-BY-CAPTAIN

本 PR 删除了什么:无(纯修复 + 回归钉;未删除任何功能、文件或公开面)。
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3699 第1轮 — 建议:FAIL

head: b31ed3b base(merge-base): e81a1af 文件: 2 个 +207/-4
关联 issue: #3662(状态:open,我核过的时间:2026-11-16 本地)

机制核查:

  • 语义修法正确:GNU print_cmd.c:257-272 的 '&' connector 分支只打 &(c != '&' || second 才补一个空格),semicolon()(print_cmd.c:1520-1525)在缓冲以 & 结尾时不再补 ;;语法上 parse.y:1274-1289 的 list1 '&' newline_list 只允许 newline_list 跟随 &,& ; 必是语法错。PR 把 background arm 改成 &(去掉尾空格)、list connector 在上一命令为 background 时只补分隔空格,机制与 GNU 一致,非个案补丁。
  • 功能实测:issue 的最小复现(定义含 $( : & put "$!" ) 的函数 + : & wait)在 base 上 respawn 子进程报 syntax error near unexpected token ';'(base 复跑原文见下),head 上无语法错且后台作业真的落盘(钉 2/3 绿)。
  • 抓到一个新问题(见 F1):PR 新增的 单元测试自己红了(head 与 CI 均红),且它钉的期望值本身是对的。print_comsub 的 background arm 在 print_command 已经 indent 一次后又无 skip_this_indent += 1 地递归 print_command(&background.command),导致 : & 被打成 8 空格;GNU print_cmd.c 的 while 体只 +4(declare -f 的函数层 4+4=8,去掉函数层的 4 即 4)。这个双缩进是 base 就有的既有偏差(base 原文 while false; do\n : & ;\ndone,同一 8 空格),不是本 PR 引入,但本 PR 的新钉按 GNU 的 4 写,因此红。
  • 无关改动:无(src/executor/print_comsub.rs + 新钉文件)。

腿(逐项):

  • 钉自跑(head b31ed3b):
    • issue3662_bg_job_in_comsub_serializer_regressions 3 passed / 0 failed;
    • lib 内 4 个单元测试:issue_3662_comsub_background_job_keeps_ampersand_connector / ..._background_then_semicolon_follower / ..._plain_semicolon_list_unchanged pass,issue_3662_comsub_loop_body_ending_in_background FAIL(src/executor/print_comsub.rs:810,left: "…\n : &\n…" vs right: "…\n : &\n…")。
  • fail-first(base e81a1af,钉文件原样放入):issue3662_bg_job_in_comsub_serializer_regressions 1 passed / 2 failed,红名 = {issue_3662_bg_job_in_comsub_in_function_parses, issue_3662_bg_job_runs_with_comsub_function_defined};base 失败文本即 syntax error near unexpected token ';'(: & ; put "$!"),与 PR 声称的根因一致。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state, executor::print_comsub::tests::issue_3662_comsub_loop_body_ending_in_background}(1354 passed / 2 failed)/ base e81a1af {issue2084…}(1351 passed / 1 failed)/ 差集 {executor::print_comsub::tests::issue_3662_comsub_loop_body_ending_in_background} = 本 PR 的账(issue2084 为并行 env flake,两侧同名)。
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --lib --bins → 0 警告。
  • 卫生:rustfmt 2 文件 clean;marker-lint OK (117 registry consts, 268 allowlisted);utf8-boundary clean。
  • CI(head b31ed3b, run 43732512072 / 43732512173):红任务 4 个,逐个归因——
    • Rust tests、Rust tests (macOS):唯一红格 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell = 已知基线;
    • GNU regression goldens and canaries:基线 + 本 PR:test result: FAILED. 1354 passed; 2 failed,红名 = {issue2875…, executor::print_comsub::tests::issue_3662_comsub_loop_body_ending_in_background}(日志原文命中 src\executor\print_comsub.rs:810);
    • niubash builds against this tree:MISSING session-method rubash::api::Session::cwd(master 已由 58eeb88c/docs(ledger): record Session::cwd #3704 记录,stale-base,非本 PR)。
  • GNU 对照(WSL /usr/local/bin/bash 5.3.20(1)-release,脚本文件形态,target/audit-s5/p3699/g1.sh、g2.sh):
    • 功能 seed:GNU 定义含 $( : & put "$!" ) 的函数 + : & wait → 无语法错、bg.txt 含 main-line 与 bg-job-done、stderr 空(head 钉同形通过);
    • declare -f 序列化:GNU 打 _Msh_FIFO=…${BASHPID:-$(: & put "$!")};(& 后无 ;);rubash head 的 comsub canonical text 同形(钉 1 单测);base 为 : & ; put "$!"(语法错);
    • 缩进(决定性):GNU 的 declare -f 打印的就是 parse 期 parse_comsub 重排后的 canonical 文本。对 f() { x=$(while false; do + 源文件里 顶格 的 : & + done; echo ok); },GNU 5.3.20 输出 x=$(while false; do\n : &\ndone\n echo ok)——: & 前是 4 空格(源文件是顶格,证明这是重排结果),即 canonical comsub 的循环体缩进是 4;rubash print_comsub_text 实测 8(target/audit-s5/p3699/g3.sh,cat -A 原文)。本 PR 的钉期望(4)与 GNU 一致,产品仍差 4 空格(base/head 都差,见 F1 的 base probe)。

F 点:

  • F1: PR 自带单元测试在 head/CI 红:src/executor/print_comsub.rs::tests::issue_3662_comsub_loop_body_ending_in_background 期望 "while false; do\n : &\ndone; echo ok"(GNU print_cmd.c 的正确缩进),实际 "…\n : &\n…"。根因:print_command 顶部已 indent(),background arm 又无 skip_this_indent += 1 直接 print_command(&background.command),双缩进;base 同病(grep/probe 实测 base 也是 8 空格,只是后半是 & ;)。GNU 应然:每命令只缩进一次(print_cmd.c:1495/1507 newline()/indent() 只在命令起点调用)。建议在 background arm 递归前加 self.skip_this_indent += 1;(与 time_command arm 同形),使 canonical text 与 GNU print_cmd.c 的 4 空格一致;否则该钉无法绿,PR 不能按「测试全绿」合并。
  • F2(披露,不阻塞本单语义):同族双缩进对其它 background 形态(if/case/group 体)同样存在(probe:if true; then\n: &\nfi → 8 空格),若按 F1 修 background arm,应顺带覆盖这些容器(同一次修复)。

续作记录:无(未改代码;FAIL 不合不关;#3662 保持 open)。
未跑/VOID 的腿:无。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3699 第2轮 — 建议:PASS(F1 已修;F2 同根因顺带覆盖)
head: d1c0e81 base(merge-base): e81a1af 文件: 2 个
关联 issue: #3662(状态:open,我核过的时间:2026-11-16 本地)

机制核查:

  • F1 修复:print_command 的 background arm 在递归 print_command(&background.command) 前补 self.skip_this_indent += 1(src/executor/print_comsub.rs:269-276),与 time_command/inverted arm 同形;GNU print_cmd.c 每命令只 indent 一次(print_cmd.c:1495/1507 newline()/indent())。修复点在 background arm 本身,if/case/group 等所有容器形态的双缩进(F2)同根因一并覆盖——background arm 是唯一递归入口。

腿(head d1c0e81):

  • 钉自跑:issue3662_bg_job_in_comsub_serializer_regressions 3 passed / 0 failed;lib 内 4 个 issue_3662 单测 4 passed / 0 failed(原红的 issue_3662_comsub_loop_body_ending_in_background 现绿,print_comsub.rs:810 断言的 4 空格与 GNU declare -f canonical 输出一致)。
  • fail-first:第1轮已在 base e81a1af 实测 1 passed / 2 failed(红名与 PR 声称一致),本轮未重跑(钉文件未变)。
  • lib 双边:本轮仅重跑 print_comsub 相关 4 单测全绿;全量 lib 第1轮差集 {issue_3662_comsub_loop_body_ending_in_background} 即本次修复对象,现已转绿;issue2084 为并行 env flake 两侧同名。
  • -D warnings:第1轮 cargo check --locked --lib --bins 0 警告;本轮仅加一行 skip 计数,无新警告面。
  • 卫生:rustfmt src/executor/print_comsub.rs clean;marker-lint OK (117 registry consts, 268 allowlisted);utf8-boundary clean。
  • CI:待本 head 跑完复核(第1轮 4 红中 3 基线/infra + 1 本 PR 账=print_comsub 单测,现应消)。
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1)-release,第1轮 g1/g2/g3 脚本已验证 : & canonical 缩进 4 空格;本轮修复后 rubash 输出同形(单测断言即该形状)。

F 点:无(F1 已修 d1c0e81;F2 由同一修复覆盖)。
续作记录:d1c0e81fb fix(executor): skip indent for background command child in comsub serializer;新 head: d1c0e81(已 push,远端 head 复核一致)。
未跑/VOID 的腿:全量 lib 双边未重跑(改动仅一行 indent 计数,print_comsub 4 单测 + 集成钉 3/3 已覆盖该面;如需队长可补跑)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 15:29
@caomengxuan666
caomengxuan666 merged commit b5b0003 into master Nov 16, 2026
10 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 16, 2026
…eters (Refs #3783)

## 根因

`test -v` / `[[ -v ]]` 的全数字操作数恒返 false:`builtins::test::variable_is_set` 只查变量表(+ 动态参数),而位置参数从不进变量表,所以 `test -v 0`、`test -v 1` 等一律判未设置。

## GNU 锚点

- `test.c:650-685`(unary `-v`):先 `valid_array_reference`,再 `valid_number(arg, &r)`,答 `r >= 0 && r <= number_of_args()`。
- `general.c:248-281` `valid_number`:strtoimax base 10,可选符号、前后空白、溢出判非法。
- `builtins/common.c:429-446` `number_of_args()`:即 posparam_count($1..$N 的个数,不含 $0)。

结论:`0` 恒 set;`N` 存在即 set;前导零按数值解析(`00` 即 $0、`01` 即 $1);`[[ -v N ]]` 与函数帧内同理。

## 修法

- `src/builtins/test/variable.rs`:`variable_is_set` 增加 `positional_count` 参数;nameref 判定后用既有的 `super::valid_number` 做位置参数探针:`n >= 0 && n <= positional_count`(空串位置参数仍计入)。
- 线程化 `positional_count`(均为 live window 长度 `ShellState::positional_len`,帧感知):`test.rs`、`command_dispatch_late.rs`、`conditional.rs`、`simple_if.rs`、`test_tests.rs`。
- 回归钉 `tests/issue3783_test_v_positional_regressions.rs`:6 组,覆盖 seed 12 项 + 边界矩阵(`+1`、前后空白、`-1`、溢出、`0x1`/`1e1`/`1abc`、空串位置参数、`[ -v 0 ]`)。

## 本 PR 删除了什么

无删除。仅新增:`variable_is_set` 等函数的 `positional_count` 参数与一处位置参数探针分支、回归钉文件 1 个。无行为删除,无公开面变动(未碰 `docs/api-ledger.md`、`src/lexer/continuation.rs`)。

## 验证(队长已核)

- X299 腿:fail-first 钉 1/6(仅 controls 组过,PIN_RC=101);修后钉 6/6;邻钉 #2710 2/2、#2073 3/3、#1707 4/4;`builtins::test` 单测 38/1;全量 lib 双边 1356/1 = 1356/1(唯一败 `issue2084_session_end_restores_host_process_state` 双边同格);`RUSTFLAGS="-D warnings" cargo check --locked --lib --bins` rc=0;seed 脚本对 built rubash.exe 12/12;GNU oracle seed 12/12;rustfmt 直核 7 文件 rc=0。
- CI run 43897420929(头 e23cc71,update-branch 到 master b5b0003 即 #3699 合入点):goldens 仅 `issue_753::case_9` + `upstream_suite_slice_snapshots`(与 master 逐格一致);census 86 败格 = 85 基线 + 1 主干继承格 `at_sign_at_a_quoted_with_colon_ifs_does_not_split`(master 跑 43895963546 同格 FAIL,旧头同格 PASS);CLI tests 单格 coproc 时序抖动 `c_command_keeps_named_coproc_output_fds_distinct`(left "first:\n" vs right "first:second\n"),重跑后绿;其余全 pass(darwin、linux/android、rust tests、macOS、shell、windows 编译、niubash 构建、ledger)。
- 队长复核:调用面 rg 已核(variable_is_set 外部 2 处、test::execute 6 处,无遗漏);`positional_len` 语义由钉边界格实测覆盖;漂移 hunk(#3612 的 command_dispatch_late.rs `((` 面)与本 PR hunk 相距约 260 行,update-branch 干净合入。

Refs #3783
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