Skip to content

fix(builtins): report each missing declare -p name once, in operand order - #3378

Merged
caomengxuan666 merged 2 commits into
unixwin:masterfrom
Uarz:fix/issue-3345
Nov 14, 2026
Merged

caomengxuan666 merged 2 commits into
unixwin:masterfrom
Uarz:fix/issue-3345

Conversation

@Uarz

@Uarz Uarz commented Nov 11, 2026

Copy link
Copy Markdown
Member

Fixes #3345

What was fixed

declare -p reported missing names in the wrong order and twice: each name should be reported exactly once, in operand order, and declare -p x --help should not re-report --help.

In -p mode there were two reporting sources for invalid-identifier operands (option-shaped names -x/-a/-p/-n/+x, --, --help, X(BR), a[], a[bad): the validation loop reported first ({name}: not found, ahead of all other diagnostics) and the print loop (print_declare_names) reported again in operand order. Measured: declare -p nope1 -x nope2 printed -x, nope1, -x, nope2; declare -p x --help printed --help, x, --help.

The fix touches only src/builtins/declare.rs (+26/−12, one line of logic): in -p mode the validation loop no longer reports and no longer drops invalid names — they flow into assign_names and the print loop becomes the single reporter, in operand order. Deleting the report without letting the names through would have broken the all-invalid-operands shape (names empty triggers the early return before the print loop runs). The non--p not a valid identifier path is untouched. typeset -p is fixed by the same mechanism; declare -f -p was already correct.

GNU (builtins/declare.def:389-406): the if (pflag) branch walks operands in order through show_name_attributes, reports a missing name immediately with sh_notfound, and increments any_failed — one loop, one report, in place order. That is GNU's assembly order.

What was deleted

Nothing. No files, features, or tests were removed.

Verification (all run, per-item counts)

  • Regression pin tests/issue3345_declare_p_missing_name_report_order.rs: 21/21 pass (expectations from GNU 5.3.20 measurements; covers operand order, --help once/centered/repeated, --, -p/-a/-A/-n/+x as operands, X(BR) single/all, dv=6, X(BR)=1, a[1]/a[]/a[bad, typeset same-order, exit code 1). Fail-first on the unpatched tree (supervisor re-ran with the patch method): 5 passed / 16 failed — every double-report/wrong-order shape was red; the green ones were shapes already correct (single X(BR), all-invalid, dv=6, subscript-name order, lone --).
  • GNU 5.3.20 oracle (binary built from third_party/bash): 4 probe scripts (~40 shapes) under env -i; after the fix probe 4 is zero-diff and the rest match except the noted noise below.
  • Adjacent: declare family across 20 test files 110/110 green; cargo test --lib declare 7/7 green; local-related 4 files 21/21 green.
  • cargo build -j 2 clean, cargo fmt --check clean. src/lexer/continuation.rs untouched.

Owed

  1. local -p still differs on two shapes (a different mechanism: execute_local's pre-pass reports missing plain names first and deletes them from args, and its guard then swallows trailing option-shaped operands): local -p nope1 -x (GNU reports -x, rubash misses it) and local -p x --help (GNU reports --help, rubash misses it). The double-report on local -p v nope1 -x is already gone with this fix. Fixing it needs restructuring the pre-pass report assembly (frame-scope adjudication + operand-order merge) — suggest a separate issue.
  2. Merged 2>&1 stdout/stderr interleaving: declare -p v nope1 -x nope2 w — GNU interleaves per name (v print → nope1 → -x → nope2 → w print); rubash emits all of stdout before all of stderr (a repo-level known limitation of buffered builtin output). Each stream's own order already matches GNU; the pin asserts per stream. Fixing it means touching the buffered-output plumbing, out of scope.
  3. Probe noise: BASH path and FUNCNAME rendering differences are pre-existing and unrelated.

…rder

In -p mode there were two reporting sources for invalid-identifier operands: the validation loop reported first (before all other diagnostics) and the print loop reported again in operand order, so 'declare -p nope1 -x nope2' printed -x, nope1, -x, nope2 and 'declare -p x --help' re-reported --help. The validation loop no longer reports or drops invalid names in -p mode; they flow into assign_names and the print loop becomes the single reporter in operand order. GNU declare.def:389-406 does one loop, one report, in place order. typeset -p is fixed by the same mechanism; declare -f -p was already correct.

Refs unixwin#3345

Regression pin tests/issue3345_declare_p_missing_name_report_order.rs (21 tests; fail-first 5 passed / 16 failed on the unpatched tree, 21/21 pass after; supervisor verified both rounds with the patch method). Declare family 110/110, local-related 21/21. cargo fmt clean.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 14, 2026 03:50
@caomengxuan666
caomengxuan666 merged commit 8b48c16 into unixwin:master Nov 14, 2026
8 of 11 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.

[bug] declare -p 缺名诊断报告序与 GNU 不同(选项形名在先)

2 participants