Skip to content

fix(redir): redirection_error diagnostics honor gnu_errfmt (Refs #1038, #4203) - #4287

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/1038-gnu-errfmt-redirect-prefix
Nov 30, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/1038-gnu-errfmt-redirect-prefix

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #1038, #4203

What

GNU redirection_error() (redir.c:135-228) reports every failure through internal_error() -> error_prolog(1) (error.c:74-86), which honors shopt -s gnu_errfmt (file:N: instead of file: line N:). Rubash's redirect-failure producers used diagnostic_prefix() (always : line N:), so under gnu_errfmt the failing-entry lookup in write_redirect_diagnostic_routed failed to match the prefix, re-applied the failing redirect and printed a second diagnostic: echo hi > /nonexistent/f printed two lines.

Fix

Switch the redirection_error producers (simple-builtin, read, trap/stack builtins, compound bad-fd dup, coproc, comsub, pipeline, fd-var assignment) to error_prefix() / compound_redirect_error_prefix(). Builtin write-error diagnostics keep diagnostic_prefix() (builtins/common.c:83-110 never gates on gnu_errfmt).

Verification

  • issue1038_gnu_errfmt_prefix 9/9 on Windows and WSL Linux; 17 script-file probes byte-identical to GNU Bash 5.3.20.
  • rustfmt, marker-lint, api-ledger clean; LF only.
  • Un-excludes issue1038_gnu_errfmt_prefix from scripts/ci-pin-exclusions.txt.

What this PR deletes

One line from scripts/ci-pin-exclusions.txt; no code deleted (prefix call swaps only).

@caomengxuan666

Copy link
Copy Markdown
Member Author

Round 1 review — needs rework (one item)

Head e9126ad5. The reported bug is fixed and verified (fail-first reproduced on base,
9/9 pins green on this head, oracle byte-identical on all five pin faces), and the CI
board is clean modulo the standing reds. But the fix misses two coproc arms in the same
family it claims to switch (§3 below) — two lines + one pin — so this still needs rework.

CI — clean modulo standing reds

  • Census (job 141563988476): FAIL {issue2340 bare_wait_keeps_last_async_job_entry,
    issue2698, issue3300, issue4181}, 0 SIG. Master (run 46312294455) has {issue2698,
    issue3300, issue3949 amp_coproc_bang_equals_coproc_pid, issue4181} — the 2340/3949
    swap is rotation churn between two documented rotators, not a regression from this PR
    (which touches only diagnostic prefixes).
  • Goldens: exactly the 2 standing snapshot failures
    (upstream_suite_slice_snapshots_errors/_exp), identical to master.
  • Everything else passes: rust-tests, Rustfmt, API ledger tripwire, CLI tests, shell
    functionality tests, both cross-target checks, Windows compile check, niubash builds
    against this tree.
  • All 9 issue1038_gnu_errfmt_prefix pins PASS in the census log. On master the stem
    is excluded and the exclusion entry records the pin failing — this PR correctly
    un-excludes it.

The fix, verified

  • Fail-first on base (devin-mac, master 60ed70e5; compare/60ed70e5...5ff7280f
    touches none of this PR's files, so the base is valid):
    redirect_open_failure_gates_line_segment FAILED with the exact double diagnostic
    from the PR body — d3.sh:2: ... followed by d3.sh: line 2: ....
  • Mechanism confirmed in source: write_redirect_diagnostic_routed
    (redirection.rs:2504) strips error_prefix() to identify the failing entry, while the
    producers composed the message with the ungated diagnostic_prefix() — no match under
    gnu_errfmt, full redirect list re-applied, second diagnostic. error_prefix()
    (public_accessors.rs:3820) honors gnu_errfmt via gnu_error_line; diagnostic_prefix()
    (:3641) never does. The swap is the right one, and the compound_redirect_error_prefix
    arms route through the same gated line.
  • Oracle (GNU bash 5.3.20) byte-identical on all five pin faces: simple redirect open
    failure (:2:, single line), read input failure, compound } >&9 at the closing
    line (:4: 9: Bad file descriptor), routed failure through a prior 2>/dev/null
    (silent, rc 1), buffered-builtin /dev/fd/99 failure — plus the controls: no-gnu_errfmt
    keeps : line N:, and builtin cd keeps : line N: under gnu_errfmt.

§3 — missed: coproc redirect-word expansion errors still ungated

redirect_word_expansion_error (redirection.rs:744: nounset hit / parameter-expansion
error in a redirect word) is reported in the coproc walk with diagnostic_prefix() at
two sites this PR did not switch:

  • src/executor/compound_exec/coproc.rs:1886 (pre-pass arm)
  • src/executor/compound_exec/coproc.rs:2007 (main walk arm)

Oracle (gnu_errfmt on): coproc C1 { echo hi; } > $UNBOUND → ./q4.sh:3: UNBOUND_VAR_XYZ: unbound variable; coproc C2 { echo hi; } > $EMPTY → ./q5.sh:3: $EMPTY: ambiguous redirect. Base prints : line 3: for both. The simple-command and compound paths
already gate these faces (verified on base, byte-identical to the oracle); only the
coproc walk misses, in the same file where this PR switched 11 sibling sites. Fix:
error_prefix() at both sites (matching the neighboring arms) + one pin asserting the
:N: form, single line, under gnu_errfmt.

Note (separate, pre-existing, not blocking): the coproc empty-expansion arm also has
the wrong message — base prints : Is a directory where GNU prints $EMPTY: ambiguous redirect. That is a message face, not a prefix face; I will track it separately.

What is correctly left alone

The remaining diagnostic_prefix() uses in the touched files were all audited:
builtin-family diagnostics (builtin-not-found, source missing filename, eval,
coproc identifier/usage/spawn errors, in-process cat, restricted shell) correctly
keep the ungated prefix — builtins/common.c never gates. The expansion-error family
(pipeline-stage parameter_expansion_error, comsub NUL warning) is a different reporter
family from redirection_error; I found two oracle-proven gaps there too (pipeline
echo ${x:?} | cat and the comsub NUL warning both print : line N: where GNU
prints :N:) and will track them separately under #4203 — not this PR's rework list.

Rework list

  1. coproc.rs:1886 and :2007: diagnostic_prefix() → error_prefix() (+ one pin
    for the coproc expansion-error face under gnu_errfmt).
  2. Nothing else. Net lines +95/−38 (+57; src net −7, tests +65, exclusion −1) are fine.

Refs #4287 (round 1 re-review; not Fixes). Dossier: lane-4287-review-2026-11-30.md.

@caomengxuan666

Copy link
Copy Markdown
Member Author

Round 1 addendum — port-side runtime verification (devin-mac, this head)

The round-1 review's port-side evidence was CI + oracle only (the lane tree was held by a safety check). The tree has since been approved and the probes re-run against a real build of this head (e9126ad5, cargo build --bin rubash on devin-mac; the binary is the real rubash, not the bash shim). Dossier: lane-4287-rt-2026-11-30.md.

  • q4 (coproc nounset redirect target) — rework item CONFIRMED on the real binary. This head prints ./q4.sh: line 3: UNBOUND_VAR_XYZ: unbound variable — still the ungated : line 3: form; the oracle prints ./q4.sh:3: .... The two redirect_word_expansion_error arms at coproc.rs:1886 / :2007 do need the error_prefix() switch, as listed.
  • q5 (coproc empty-expansion target) — prefix already gated on this head; one refinement to round 1. This head prints ./q5.sh:3: : Is a directory — the :3: prefix is already gated (an arm this PR did switch covers it; base printed : line 3:). So the $EMPTY face is not part of the prefix rework. What remains there is only the message text (: Is a directory vs GNU $EMPTY: ambiguous redirect), already tracked separately as a [1.6.0][P0] 清盘 ci-pin-exclusions.txt(104 项)并补齐 PR CI 的 fmt/clippy/MSRV 门禁 #4203-family residual — not this PR's list.
  • Controls pass: p1 (simple redirect open failure), p3 (read input failure), p7 (buffered-builtin input failure) are byte-identical to the oracle, rc 1, on the real binary.

Net effect on the rework list: unchanged in substance (the two coproc.rs sites + one pin), with the q4 face now runtime-confirmed and the q5 prefix explicitly out of scope.

@caomengxuan666
caomengxuan666 force-pushed the fix/1038-gnu-errfmt-redirect-prefix branch from e9126ad to 39cdef9 Compare November 30, 2026 05:55
@caomengxuan666

Copy link
Copy Markdown
Member Author

Round-1 rework applied (head rebased on master ef4ea1b): coproc.rs:1886 and :2007 now use error_prefix(); +1 pin coproc_nounset_redirect_word_gates_line_segment (oracle 5.3.20: <script>:3: UNBOUND_VAR_XYZ: unbound variable). issue1038 stem 10/10 on Windows and WSL; rustfmt clean. The $EMPTY: ambiguous redirect coproc message face is not pinned (separate message bug you are tracking). Awaiting CI.

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