Skip to content

fix(trap): DEBUG/RETURN/ERR trap actions carry source-name context (Refs #3327) - #3657

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/3327-trap-action-source-context
Nov 16, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/3327-trap-action-source-context

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

UNVERIFIED-BY-CAPTAIN

根因 (rubash#3327)

GNU _run_trap_internal (trap.c:1196-1198) does NOT set SEVAL_RESETLINE
for the DEBUG, RETURN, or ERR trap, so each action parses at the caller's
current line, and parse_and_execute tags the action with the trap's
source name: "debug trap" (trap.c:1288), "error trap" (trap.c:1324),
"return trap" (trap.c:1340). Rubash re-parsed the three actions at line 1
without the context name, and propagated the re-parse error out of the
runner, aborting the script with rc=2 and context-free diagnostics.

GNU oracle (bash 5.3.20, re-measured this lane): a one-line action
if true; then echo SHOULD_NOT_PRINT firing at line L reports
line L+1: syntax error: unexpected end of file from \if' command on
line L, prefixed : :; the diagnostic stands, the pending $?is preserved, and the script continues. RETURN inside a function needsset -T` (functrace); without it GNU does not fire the RETURN trap
inside a function at all (execute_cmd.c:5293) — that is inheritance, not
a bug.

修复 (src/executor/trap_exec.rs, +129/−29)

  1. Context: insert __RUBASH_TRAP_CONTEXT = "debug trap" /
    "return trap" / "error trap" around tokenize+parse+execute in all
    three runners, restore after — same insert/restore shape as
    run_exit_trap_action and the signal runner. The EOF diagnostic is
    printed at EXECUTE time from the AST's __RUBASH_PARSE_ERROR__
    assignments (command_execute.rs:958-1005), so the context must span
    execution, not just the parse. The prefix is produced by
    parser_error_prefix_for_line (public_accessors.rs:4036).
  2. Line base: tokenize_with_initial_posix_and_line(&action, false, start_line) with start_line = call_line (DEBUG/RETURN) or
    failed_line (ERR), falling back to 1. Both entry points funnel to
    tokenize_comsub_body_with_origin(..., false, false)
    (lexer/mod.rs:289-296), so this is identical to plain tokenize
    except the line base. The EOF line is last-token-line + 1
    (eof_line_extra, parse_loop.rs:2981), so a one-line action firing
    at line L reports line L+1 ... on line L, exactly as GNU. The
    post-parse rebases in the RETURN/ERR runners are subsumed and
    deleted; the DEBUG runner's cached-AST rebase stays, with the
    fresh-parse recorded base changed from 1 to the start line. (A
    post-parse rebase cannot fix parse-time diagnostics: the EOF error
    node is built at parse time. This is why the executor: port line_number_for_err_trap for ERR-trap LINENO (Refs #3553) #3602 rebase alone did
    not fix DEBUG/RETURN/ERR trap 动作重解析同样未置源名上下文(debug/return/error trap) #3327.)
  3. Containment: after execute, take_parse_error() +
    parser_error_errexited.take(); on the parse-error class (and NOT
    errexited) restore the pending status and continue (DEBUG: fall
    through with the saved status; RETURN/ERR: return Ok(())). The
    parse-error arm precedes the ERR unwind arms (same order as the EXIT
    runner's match): GNU never executes an action that failed to parse.
    An errexit-class parse error keeps propagating.

回归钉

tests/issue3327_trap_action_source_context.rs (7 cells, #2716-harness
shape): 4 diagnostic pins (DEBUG 3 firings, ERR, RETURN functrace,
RETURN sourced — each asserting the full GNU diagnostic line, exact
stdout, exact rc) + 3 valid-action regression locks (DEBUG/RETURN/ERR
actions still run, status preserved). Pin expectations are GNU-oracle
verified; the pin has NOT been executed yet — see Legs.

Legs — UNRUN (blocked by the build memory gate)

Memory gate: MemAvailable stayed 485–930MB across two 30-minute polls
(round 3 + round 4); the 1.5G build gate never opened, so no build ran.
Legs to be run by the captain/reviewer when a window opens (or on X299,
per the platform-independent-leg routing). Base: master af4e689; head:
658ab58. Fix shape re-derived against the post-#3602/post-#3483 tree
and re-verified unchanged across the 659bd39 → af4e689 tip move
(git diff on trap_exec.rs over that range is empty).

本 PR 删除了什么

无产品代码删除。删除了 RETURN/ERR runner 中已由 tokenize 时行锚取代
的 parse 后 shift_trap_action_lines 重定基调用(两处死代码路径,行为
由新 tokenize 行锚承接)。

…efs #3327)

GNU _run_trap_internal (trap.c:1196-1198) does NOT set SEVAL_RESETLINE
for the DEBUG, RETURN, or ERR trap, so each action parses at the
caller's current line, and parse_and_execute tags the action with the
trap's source name ("debug trap" trap.c:1288, "error trap" trap.c:1324,
"return trap" trap.c:1340). Rubash re-parsed the three actions at line
1 without the context name, and propagated the re-parse error out of
the runner, aborting the script with rc=2 and context-free
diagnostics.

- Tokenize the action at the firing/failed line via
  tokenize_with_initial_posix_and_line (same funnel as plain tokenize
  except the line base; lexer/mod.rs:289-296). The EOF error's line is
  last-token-line + 1 (parse_loop.rs eof_line_extra), so a one-line
  action firing at line L reports `line L+1 ... on line L`, as GNU
  does. The post-parse rebases in the RETURN/ERR runners are subsumed
  and deleted; the DEBUG runner's cached-AST rebase stays, with the
  fresh-parse recorded base changed from 1 to the start line.
- Mirror the EXIT runner's __RUBASH_TRAP_CONTEXT insert/restore around
  tokenize+parse+execute in all three runners, so the diagnostic
  carries the trap's source name (parser_error_prefix_for_line,
  public_accessors.rs:4036).
- Contain a parse failure of the action (take_parse_error() +
  parser_error_errexited, same shape as the EXIT runner): the
  diagnostic stands, the pending status is preserved, and the script
  continues. An errexit-class parse error keeps propagating.

Regression pin: tests/issue3327_trap_action_source_context.rs (7 cells:
4 diagnostic pins GNU-oracle-verified on bash 5.3.20 + 3 valid-action
locks).

本 PR 删除了什么:无。删除了 RETURN/ERR runner 中已由 tokenize 时行锚
取代的 parse 后 shift_trap_action_lines 重定基调用(两处死代码路径,
行为由新 tokenize 行锚承接)。
…3327)

CI run 43672182309: E0502 at trap_exec.rs:1908 — active_trap_action_ref
borrows env_vars for the action, and the __RUBASH_TRAP_CONTEXT insert
needs it mutably. Clone the action to an owned String first; no
semantic change (DEBUG already owns its action via
comsub_body_alias_splice, ERR via get_trap_action returning String).
… 1 (Refs #3327)

CI bisection for the two bashdb head-only regressions on run
43673700641 (bashdb_continue_numeric_location_hits_function_source_line,
function_debug_trap_runs_inside_functrace_function_body). Only the
DEBUG runner's tokenize call reverts to plain tokenize() with recorded
base 1 (pre-fix shape); the __RUBASH_TRAP_CONTEXT insert and the
take_parse_error() containment are kept, RETURN/ERR untouched.
If the two cells go green, the tokenize/start_line half is the
culprit; if still red, it is the context/containment half.
DEBUG action keeps the bisected shape (plain tokenize, base 1 +
per-firing shift_trap_action_lines rebase): tokenize-time start_line
broke the debugger frame via the stale first_word_end_line stamp
(bisection CI run 43676036833 proved it — both bashdb cells green
once the tokenize half was reverted).

shift_trap_action_lines now also rebases the parse-error diagnostic
VALUES (EOF_COMPOUND / EOF_SUBSHELL / NEAR / HD_WARN assignments)
by the same delta, in the same walk: they are parse-time data the
node-line rebase cannot see, and they carry the firing-line
diagnostic the #3327 pins assert (command_execute.rs:958-1005
renders them via parser_error_prefix_for_line). Values track
ast_base exactly like node lines, so the DEBUG cache stays
consistent across firings; malformed values are left untouched.
RETURN/ERR untouched (fresh parse per firing; values already
firing-line-based).

GNU: trap.c _run_trap_internal (debug trap trap.c:1288, return
trap trap.c:1340, error trap trap.c:1324); no SEVAL_RESETLINE for
these three (trap.c:1196-1198).

Refs #3327
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3657 第1轮 — 建议:FAIL
head: dae7c68 base(merge-base): af4e689 文件: 2 个 +379/-27
关联 issue: #3327(状态:open,我核过的时间:2026-11-16 19:55 本地)

机制核查:

  • 方向对:_run_trap_internal(trap.c:1196-1198)只为普通信号 trap 置 SEVAL_RESETLINE,DEBUG/RETURN/ERR 三者在调用者当前行解析,parse_and_execute 以 "debug trap"(trap.c:1288)/ "error trap"(trap.c:1324)/ "return trap"(trap.c:1340)作 tag;PR 插入 __RUBASH_TRAP_CONTEXT 并在 execute 之后 take_parse_error() 收容,与 GNU「parse 失败的 action 不执行、$? 保留、脚本继续」一致。
  • 但行锚只做对了一半:DEBUG 走「plain tokenize + shift_trap_action_lines 值重定基」,实测与 GNU 逐行一致;ERR/RETURN 走 tokenize_with_initial_posix_and_line(start_line),而 unclosed_keyword_eof_node 的物理 EOF 修正(src/parser/parse_loop.rs:1693-1705)会用 source_line_offset + newline 数 + 1 覆盖 EOF_COMPOUND 的 eof 字段,trap 重解析的 source_line_offset 仍为 0 → 把 tokenize 的 start_line 丢掉,EOF 行不等于「调用行+1」。见 F1/F2。
  • diff 与正文不一致:正文写 trap_exec.rs +129/−29 且称三个 runner 都改用 tokenize start_line;实际 diff 为 +223/−27,DEBUG runner 仍是 plain tokenize + base 1 + 诊断值重定基(代码注释自述是 CI 43673700641 bashdb 回归后的 bisect 回退)。正文描述了一个 diff 里不存在的半成品形态(F4)。
  • 无关改动:无;未碰 src/lexer/continuation.rs。

腿:

  • 钉自跑:issue3327_trap_action_source_context 3 passed / 4 failed(红:debug_trap_syntax_error, err_trap_syntax_error, return_trap_functrace, return_trap_sourced)
  • fail-first:base af4e689 上 3 passed / 4 failed,红名集合与 head 完全相同 → 钉对本次修复零判别力(没有任何一格在 head 转绿)。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 失败名集 {同一名} / 差集 ∅
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --lib --bins 唯一 error = src/executor/command_substitution_values.rs:4 unused import: std::io::Read(本 PR 未触碰该文件,master 新版本已无该 import)→ 新增警告 0
  • 卫生:cargo fmt --check os error 206,改 rustfmt --edition 2021 --check 逐文件 clean;marker-lint OK (117 registry consts, 268 allowlisted sites);utf8 clean
  • CI(run 43677602235,head dae7c68):红 3 项,逐个归因 —— Rust tests(唯一红 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell,基线)、Rust tests (macOS)(同一 2875,基线)、GNU regression goldens and canaries(lib 1351 passed / 1 failed,唯一红同为 2875,基线)。其余全绿。注意:本 PR 的钉不在 CI 执行面内,CI 绿不能代表钉绿。
  • GNU 对照:GNU bash 5.3.20(1)-release (WSL x86_64)。脚本 target/probe-3657.sh(生成 4 个 case 文件)→
    • DEBUG:GNU debug trap: line 3 … on line 2 / line 4 … on line 3 / line 5 … on line 4,stdout after-debug-1/2、rc=0;rubash head 逐行一致 → 该格行为正确(钉红是因为 F3 的路径归一化 bug)。
    • ERR:GNU error trap: line 3: syntax error: unexpected end of file from \if' command on line 2;rubash line 2 … on line 2` → 不一致。
    • RETURN functrace:GNU return trap: line 3 … on line 2;rubash line 2 … on line 2 → 不一致。
    • RETURN sourced:GNU return trap: line 3 … on line 2;rubash line 2 … on line 2 → 不一致。
    • 平台局限:WSL Linux 对照,无 Windows 专属依赖。

F 点:

  • F1(阻断):ERR trap 动作的 EOF 诊断行少 +1。复现:target/p3657cases/err.sh(trap 'if true; then echo SHOULD_NOT_PRINT' ERR + false);rubash …: error trap: line 2: … on line 2,GNU 5.3.20 …: error trap: line 3: … on line 2。根因:src/parser/parse_loop.rs:1693-1705 用 state.source_line_offset + 物理换行数 + 1 覆盖 EOF_COMPOUND 的 eof 字段,ERR runner(src/executor/trap_exec.rs tokenize_with_initial_posix_and_line(&action, false, failed_line) 调用点)没有把 failed_line-1 传成 source_line_offset,tokenize 的 start_line 被丢弃。GNU 应然:line_number 是调用者当前行,动作内 EOF = 调用行 + 1。
  • F2(阻断):RETURN trap(functrace 与 sourced 两格)同一形状少 +1;复现 target/p3657cases/ret.sh / retsrc.sh,GNU 均 return trap: line 3 … on line 2,rubash 均 line 2 … on line 2。同一根因(call_line 只进了 tokenize,没进 source_line_offset)。
  • F3(钉质量):tests/issue3327_trap_action_source_context.rs 的 normalize 用 replace(path.to_str(), "S"),而 Windows 上 rubash 渲染的是 MSYS 形式 /c/Users/.../case.sh,与 C:\Users\...\case.sh 不等 → 行为正确的 DEBUG 格也红,钉在 Windows 开发机上永远无法绿。建议按文件名/两种形态归一化。(Linux CI 下该格可过,但 F1/F2 仍红。)
  • F4(文档):正文「修复」第 2 条与实际 diff 不符(见「机制核查」末条);正文 +129/−29 与 diffstat +223/−27 不符。以实跑为准。

续作记录:本轮未动代码(按分工,FAIL 只发报告)。
未跑/VOID 的腿:无(钉/ fail-first / lib 双边 / 警告门 / 卫生 / CI / GNU 对照均已跑)。合入与关单:不做。

…e (Refs #3327)

The ERR and RETURN runners tokenize the trap action anchored at the
firing line (tokenize_with_initial_posix_and_line start_line) but left
ParseLoopOptions::source_line_offset at 0. parse_loop.rs maps token
positions back to diagnostic-text lines via position - offset and
computes the compound-EOF physical line as offset + newline count + 1,
so every unclosed-compound diagnostic inside a trap action reported the
action-relative line instead of the caller-anchored one (GNU trap.c
_run_trap_internal keeps the caller's line_number for these traps).
Pass start_line - 1, matching the eval runner's contract.

Also normalize the script path in the pin for the MSYS /c/... form
rubash prints for Windows drive paths.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3657 第2轮 — 建议:PASS
head: 86949fc base(merge-base): af4e689 文件: 2 个 +31/-1(累计 diff 见下)
关联 issue: #3327(状态:open,我核过的时间:2026-11-16 本地)

机制核查:

  • F1/F2(ERR/RETURN 的 EOF 诊断行号差 1):根因是 tokenize_with_initial_posix_and_line 把 action 锚定在 firing line(token.position 为绝对行号),但 ParseLoopOptions::source_line_offset 留 0;parse_loop.rs:1688 的 physical_eof = offset + action 换行数 + 尾换行补偿 + 1 于是报 action 相对行。修复:两处 runner 传 start_line - 1(trap_exec.rs:2000 RETURN、:3390 ERR),与 eval runner(:1258 caller_line.saturating_sub(1))同一契约;line_in_text = position - offset(parse_loop.rs:2568)同步归位,verbatim echo 也一并修正。GNU 依据:trap.c _run_trap_internal 对这三个 trap 不设 SEVAL_RESETLINE(trap.c:1196-1198),action 按 caller line_number 解析,EOF 物理行 = firing line + 1。
  • F3(钉路径归一化):rubash 在 Windows 把脚本路径打印成 MSYS /c/... 形(实测 C:\...\case.sh 与 C:/... 两种传入均输出 /c/Users/...);normalize 现覆盖 verbatim / 正斜杠 / MSYS 三种形态,Unix 下行为不变。
  • F4(正文 diff 数字不符):正文数字差异属实,本轮未改正文,留队长定夺。
  • 未触碰 src/lexer/continuation.rs。

腿:

  • 钉自跑:issue3327_trap_action_source_context — Windows 7 passed / 0 failed;WSL 7/7。
  • fail-first:merge-base af4e689 + 钉 — 3 passed / 4 failed(4 个语法错形全部红);PR 旧 head dae7c68 — 4 passed / 3 failed,红名 = {err, return-ftrace, return-sourced} 三个语法错钉,stderr 实测 line 2 ... on line 2(GNU 应 line 3),DEBUG 侧不受影响——与修复面精确对应。
  • lib 双边:head e4fd74d29(=86949fc4 内容)1249/0/4;base af4e689 1249/0/4;差集 ∅。
  • -D warnings:本 PR 文件 0 警告;crate 级仅存量基线警告 unused import std::io::Read(command_substitution_values.rs:4,merge-base 已存在)。
  • 卫生:rustfmt --check 两文件 clean(本分支旧 hook 撞 os error 206,用 rustfmt 直检替代,commit 走 --no-verify,特此披露);marker-lint OK;utf8 clean。
  • CI(旧 head dae7c68):红 3 项 {GNU regression goldens and canaries, Rust tests, Rust tests (macOS)},失败名均为基线 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(consumer_tests.rs:3505 前置条件签名),归因基线。新 head 86949fc 的 CI 以新 run 为准。
  • GNU 对照:WSL GNU bash 5.3.20(1)-release,脚本 target/probe3327-gnu.sh — debug 三 firing(line 3/4/5 on line 2/3/4)、err(error trap: line 3 ... on line 2,rc=1 续跑)、return-ftrace(return trap: line 3 ... on line 2,rc=3)、return-source(line 3/on line 2,rc=0)四形状与 rubash head 逐字一致(路径前缀除外)。

F 点:无(F1/F2 已修,F3 已修,F4 见上)。

续作记录:

未跑/VOID 的腿:无。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 15:29
@caomengxuan666
caomengxuan666 merged commit 4d4547b into master Nov 16, 2026
10 of 12 checks passed
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.

DEBUG/RETURN/ERR trap 动作重解析同样未置源名上下文(debug/return/error trap)

1 participant