Skip to content

WIP: trap-visible $BASH_COMMAND/$BASH_SOURCE truth under compound redirects (#1872) - #1875

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/w3-trapstate-1872
Oct 27, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/w3-trapstate-1872

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1872
Refs #1720

What

Two invariant breaks in trap-visible state, one family — fixed at the invariants, no bats/group-shape special cases.

A. $BASH_COMMAND baked in an enclosing compound's redirects. GNU stamps a command with its OWN node text (print_cmd.c make_command_string prints the node's own redirection list; execute_cmd.c:779 updates BASH_COMMAND from that text before running traps). Rubash splices a compound's redirects into body element nodes — marked injected (support_names::injected_group_redirect, contract: "NOT the command's own redirect"). Execution consumers honor the mark; the text renderers did not, so inside { …; } >>f 2>>f the DEBUG trap reported echo one >> f 2>> f. bash_command_text and append_source_redirects (command_text.rs) now filter marked redirects through one funnel helper (own_redirect), covering every splice face (brace groups, case clauses, loops, function calls, source) at any depth; an element's own redirects still render.

B. $BASH_SOURCE scalar/declare froze at the last sourced file. GNU keeps one live dynamic array (variables.c:1892 init_dynamic_array_var get_self; execute_cmd.c:5313 push_source on function entry; evalfile push/pop for source) — scalar, element and declare reads observe the same stack. Rubash's env mirror cell refreshed only at source-frame push/pop while function entry/exit moved only the live stack, so after any function call returned, scalar $BASH_SOURCE (the form bats' DEBUG trap passes) and declare -p returned the frozen frame. bats classified every test command as bats-internal and its failure stack traces came out empty — the main root of the bats-core suite reds in #1720. dynamic_parameter_value gains a BASH_SOURCE arm and dynamic_declaration_view gains BASH_SOURCE/BASH_LINENO arms rendering from the composed views element reads already use (FUNCNAME/#1394 precedent); declare listing mode merges the two views like PIPESTATUS.

Regression pins

tests/issue1872_trap_visible_state_regressions.rs — 11 pins: group simple/function elements, element-own-redirect preservation, ERR trap in group, function-call body stamps, source-face stamps, trap-argument ladder across a loader-sourced definition, scalar checkpoints, declare operand + listing forms, and the full fd-4 bats geometry. All 11 fail on master 1a4e86f, all 11 pass here (oracle: GNU bash 5.3.20, script files).

Gates (local, this tree)

Cluster report: ~/workspace/issue-clearance/clusters/w3-trapstate/REPORT.md

…under compound redirects (#1872)

Mechanism A — $BASH_COMMAND. GNU stamps a command with its OWN node
text: print_cmd.c make_command_string prints the node's own
redirection list, and execute_cmd.c:779 updates BASH_COMMAND from
that text before running any traps. Rubash splices an enclosing
compound's redirects into body element nodes (marked injected —
support_names::injected_group_redirect, whose contract is "NOT the
command's own redirect"); execution consumers honor the mark but
the text renderers did not, so inside { ...; } >>f 2>>f the DEBUG
trap reported `echo one >> f 2>> f`. bash_command_text and
append_source_redirects (command_text.rs) now filter marked
redirects through one funnel helper, restoring the invariant for
every splice face (brace groups, case clauses, loops, function
calls, source) at any depth, while an element's own redirects
still render.

Mechanism B — $BASH_SOURCE. GNU keeps one live dynamic array
(variables.c:1892 init_dynamic_array_var get_self;
execute_cmd.c:5313 push_source on function entry; evalfile push/pop
for source): scalar reads, element reads and declare -p all observe
the same stack. Rubash's env mirror cell refreshed only at
source-frame push/pop while function entry/exit moved only the
live stack, so scalar $BASH_SOURCE (the form bats' DEBUG trap
passes) and declare -p froze at the last sourced file after any
function call returned — bats then classified every test command
as bats-internal and emitted empty failure stack traces.
dynamic_parameter_value gains a BASH_SOURCE arm and
dynamic_declaration_view gains BASH_SOURCE/BASH_LINENO arms
rendering from the composed views element reads already use (the
FUNCNAME/#1394 precedent); declare's listing mode merges the two
views like PIPESTATUS.

Regression: tests/issue1872_trap_visible_state_regressions.rs —
11 pins (group/function/source splice faces, ERR trap, scalar/
declare/listing reads, fd-4 bats geometry), all failing on master
1a4e86f, all passing here. Gates: lib 773/1/1 (baseline set),
diffcorpus 306/306, trap/sig/#1844 adjacent targets green,
-D warnings clean (lib+bins).
@caomengxuan666

Copy link
Copy Markdown
Member Author

Gate evidence (local, tree at 4529dc9; baseline master 1a4e86f):

  • Regression pins: tests/issue1872_trap_visible_state_regressions.rs — 11/11 pass here; verified 11/11 FAIL on pristine master (stash double-run).
  • lib: 773 passed / 1 failed / 1 ignored — failure set identical to baseline (sole ownership_unary_operators_check_existing_file).
  • diffcorpus 306 full run: 306/306 PASS (failure set empty, subset of {ms10}).
  • Adjacent targets: 14 test targets, 66 tests, all green (trap families incl. [bug] DEBUG trap under functrace: function call inside $() reports the outer command in $BASH_COMMAND and fires once instead of twice #1473 functrace, [fd] 重定向失败的 pipeline stage 状态漏入管线 $?(GNU 0 / rubash 1) #1844 redirect-failure pipeline, host_internal_trap_suppression, debugger_flag_gnu_parity).
  • -D warnings: clean for lib+bins (the niu-CI surface). The --all-targets failure sits in the pre-existing issue530 test file's naming lints — zero mentions of this PR's files.
  • bats eco recheck (restored vendor tree, private copy, RECHECK=1, shell = this tree's binary): file_setup_teardown.bats 13 ok / 4 not-ok, vs 11 ok / 6 not-ok for the same recheck on master (w3-1720-final). Remaining 4 (tests 5-8) are setup_file/teardown_file failure and CTRL-C abort semantics — a different family, out of this PR's scope.
  • rustfmt applied to changed files; CR count 0; continuation.rs untouched.

Full cluster report with root-cause analysis, GNU citations (execute_cmd.c:779, execute_cmd.c:5313, variables.c:1892, print_cmd.c make_command_string), the minimal BASH_SOURCE isolation ladder, and three REPORT-only residual observations: ~/workspace/issue-clearance/clusters/w3-trapstate/REPORT.md

@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 27, 2026 13:58
@caomengxuan666
caomengxuan666 merged commit 4eea937 into master Oct 27, 2026
9 of 10 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/w3-trapstate-1872 branch October 30, 2026 01:41
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.

[trap] 组重定向污染 trap 可见的 $BASH_COMMAND/$BASH_SOURCE(bats 失败诊断整体丢失)

1 participant