Skip to content

Stamp the diagnostic frame file so sourced-file functions report their definition file (Refs #3035) - #3047

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3035-sourced-function-error-file
Nov 8, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3035-sourced-function-error-file

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

UNVERIFIED-BY-CAPTAIN

Refs #3035

问题

sourced 文件里定义的函数在执行期报错时,rubash 报顶层脚本的文件名
(行号却是定义文件内的行)。GNU 报定义文件:

  • GNU bash 5.3.20:./errlib.sh: line 3: [: 1: unary operator expected
  • rubash(master c4f397d6):isolate-err.sh: line 3: [: 1: unary operator expected

链路面同根:git-contrib 链 A15(GNU git-completion.bash: line 3489
vs rubash driver.sh: line 3489)。

根因

帧机制本身是对的:函数体内 ${BASH_SOURCE[0]} 已是定义文件
(定义时记录 + 调用入场推帧都正确,探针核实)。

错的是 builtins 层一批私有的 diagnostic_prefix(env_vars) 拷贝:
它们把逐帧维护的 __RUBASH_CURRENT_LINE(行,对)与顶层粒度的
__RUBASH_SCRIPT_NAME(名,错)拼成 {script}: line {line}: 。
这正是 executor 侧前缀在 #2212 修过的「行对名错」配对,只是这批
builtins 私有拷贝从未接上。它们接不上也有结构原因:存储的
BASH_SOURCE 镜像只在 source 帧 push/pop 时刷新,函数入场不刷,
env-only 的 builtin 函数读不到活的帧文件——缺一个由 executor
逐命令维护的文件载体。

GNU 锚点

  • error.c:90-120 get_name_for_error:非交互 prolog 名 =
    BASH_SOURCE[0](非空时),否则 $0 / shell 名。
  • make_cmd.c:746-771 make_function_def:定义时
    FUNCTION_DEF.source_file = BASH_SOURCE[0]。
  • execute_cmd.c:5310-5313 execute_function:调用入场
    push_source(..., shell_fn->source_file)——帧文件是定义文件,
    与调用者无关(wrapper 跨文件调用格据此仍报定义文件)。

修法(机制)

完全仿 __RUBASH_CURRENT_LINE 的既有范式:

  1. Executor::set_current_line(ast_exec 循环与 funnel 的共同戳点)
    盖行戳的同时盖 __RUBASH_CURRENT_FILE,值 =
    current_error_file_name()(组合 BASH_SOURCE[0],即
    diagnostic_prolog 用的同一个名字来源,不另立第二套语义);
    双存储(env map + 进程 env 镜像)、等值门(新缓存字段
    file_env_os_value 与 line_env_os_value 同型);名字为空时
    摘除载体,读取侧回退 __RUBASH_SCRIPT_NAME。
  2. init.rs 两处剥离点与 CURRENT_LINE 并排加 CURRENT_FILE
    (validated respawn 子代保留盖戳,与行帧同规则)。
  3. 新共享模块 src/builtins/diagnostic.rs
    (diagnostic_prefix / gnu_errfmt 门的 readonly_error_prefix),
    本波转换无在飞车道占用的拷贝:test、ulimit、trap、cd(两 fn)、
    exec;executor 侧 script_name_prefix 的名字来源改为
    error_report_name()。
  4. parser 家族前缀(*_for_line 等)经调用方审计均为读流/记录行
    语义,名字与记录行同源,不在本 PR 面内。

回归钉

tests/issue3035_sourced_function_error_file_regressions.rs(6 格):
issue 种子([ 于 sourced 函数)、wrapper 跨文件调用、顶层自有
函数对照、ulimit / cd / trap 同族三面。期望值均先经 GNU 5.3.20
探针钉死。

车道自报(待队长复核):

  • fail-first(未修树):5 红 1 绿(对照格绿),红格 left 均为
    顶层名、right 为 GNU 的 ./errlib*.sh;修后 6/6 绿。
  • lib 双边逐数对齐:fix 1197/0/4 == base(c4f397d6)1197/0/4。
  • RUSTFLAGS="-D warnings" cargo check --locked --lib --bins 过。
  • 探针:probe/probe2/probe3 共 7 格 + 嵌套 source / source 期间
    报错 / 顶层报错 3 格与 GNU 逐字节一致(含 rc)。
  • hygiene clean;逐文件 rustfmt --check 过;无 CR。
  • 邻茎:issue2212 15/0、issue2714 7/0、issue2580 2/0、
    issue2947 6/0 全绿。issue1768 为 4/2,但 base 树同茎同为 4/2
    (两红格均既存:-c 格期望 bash: 先于 [B1-11] Diagnostic prolog hardcodes bash instead of the argv[0]-derived name (owner decision: impersonation vs fidelity) #2714 的 argv0 prolog
    裁定;前台死亡通知行号为收割时点敏感)——本 PR 零新增红,
    该茎期望订正不在本 PR 面内,一并报队长。

本 PR 删除了什么

  • 删除 test.rs / ulimit.rs / trap.rs / cd.rs / exec.rs 中 6 份私有
    diagnostic_prefix / readonly_error_prefix 拷贝,统一到
    src/builtins/diagnostic.rs 的共享实现。
  • 连带删除 test.rs、exec.rs 中因此不再使用的 HashMap import;
    test.rs 两个死平台 cfg stub 的签名由 &HashMap 对齐为活变体的
    &VarTable(调用方本就传 VarTable,原靠 Deref coercion)。

残余面(本 PR 不动,请队长立案/派工)

载体与共享 helper 已备,以下转换是机械活,但文件面被在飞车道
占用或签名无 env map:

GNU's diagnostic prolog name is get_name_for_error (error.c:90-120):
BASH_SOURCE[0], which at a function call is the function's DEFINITION
file (make_cmd.c:746-771 records source_file from BASH_SOURCE[0] at
definition time; execute_cmd.c:5310-5313 pushes it at call entry).
The executor's own prefix already read that composed view (#2212),
but the builtin layer's private diagnostic_prefix copies paired the
per-frame __RUBASH_CURRENT_LINE with the top-level-only
__RUBASH_SCRIPT_NAME, so an error inside a function defined in a
sourced file carried the definition file's line under the top-level
script's name. The stored BASH_SOURCE mirror refreshes only at
source-frame push/pop, so env-only builtins cannot derive the frame
file themselves.

Following the __RUBASH_CURRENT_LINE carrier idiom:
- set_current_line also stamps __RUBASH_CURRENT_FILE (=
  current_error_file_name(), the same composed view
  diagnostic_prolog reads) into both storages under the same
  equality gates; init strips it beside the line for non-respawn
  entries.
- New shared builtins::diagnostic builders replace the private
  copies in test/ulimit/trap/cd/exec (cd's readonly twin too).
- script_name_prefix takes error_report_name() instead of
  preferring __RUBASH_SCRIPT_NAME.
Six cells: the seed (test error inside a sourced-file function),
the same error reached through a caller function, a
top-level-defined control, and the ulimit/cd/trap faces of the
same builtin_error family. Expectations were verified against
GNU bash 5.3.20 by probe first: 5 of 6 fail on the base tree
(the control passes); all 6 pass with the carrier stamp.
@caomengxuan666
caomengxuan666 merged commit a7d3176 into master Nov 8, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 8, 2026
…2885)

- markers.rs census: add EXACT entry for __RUBASH_CURRENT_FILE
  (new carrier from #3035/#3047, missing from the E8 census).
- marker_registry_roundtrips: remove all four TYPED_DATA_EXCEPTIONS
  (E002 shapes 1-3 fixed by #2882/#3006; E316 shape 3 by #3016/#3041);
  the stem now asserts every shape round-trips.
- marker-lint-allowlist: delete the two #2882 KNOWN BLIND SPOT entries
  (their own text said delete when #2882 lands), plus two entries made
  stale by master-side changes: set.rs ESC rendering entry (#3044
  restructured the table) and command_prepare contains:QUOTED_NULL_MARKER
  (#3006 converted those consumers to E400-aware helpers).
- marker_leak_golden_pty: un-ignore the read -p cell (#3001 landed the
  read -p decode).
- marker_leak_golden: set-listing C0 scalar cell now expects the
  ansic-quoted GNU form (rubash#3017/#3044), byte-verified against
  GNU bash 5.3.20; the no-marker assertion is unchanged.

Verified on the merged tree (merge bd87ed1 + this commit):
lint OK (117 consts / 258 sites), registry 10/0, PTY 2/0,
golden 20/0, lib 1200/0/4, -D warnings rc=0, hygiene/fmt/CR clean.
caomengxuan666 added a commit that referenced this pull request Nov 9, 2026
…#3143)

Fixes #2907

生态链钉入仓:真实 git contrib 脚本(git-completion.bash / git-prompt.sh,GPL 不入仓、宿主发现)的 live 入口端到端链钉——链 A 驱动已注册补全入口 `__git_wrap__git_main`(含 A15 直调 `__git_main`),链 B 在八态仓库上渲染 `__git_ps1`(含无参打印形与 pcmode 形);同仓库、同 driver 分别在 rubash 与 GNU bash 5.3.20 oracle 下运行,stdout 逐字节一致、stderr 归一脚本路径后逐字一致。同文件第二钉复核 mvdan-n 16 个解析夹具的 `-n` 退出码与 oracle 逐个一致(559 片段差分是否接 CI 仍归 #3033 裁定,本 PR 不预断)。本钉即 eco-2907 两处已修分歧的链级回归钉:B6 格 = #3034(PR #3056)真实命中形,A15 stderr 格 = #3035(PR #3047)真实命中形。

本 PR 删除了什么:无(纯测试与夹具新增)。净行数 +645/−0,4 文件:tests/issue2907_git_contrib_chain.rs、tests/fixtures/ecosuite/git-contrib/{driver,make-repos}.sh、tests/fixtures/ecosuite/MANIFEST.md。
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