Skip to content

invocation/diagnostics: -eu nounset rc, -- separator, niu -o with no arg, error-message branding #705

Closed

Description

@caomengxuan666

Summary

Four invocation / diagnostic divergences from GNU bash. -c is documented as niu's agent/CI contract, so these matter more than their size suggests.

Environment: Niubash 1.4.0 · rubash git e785c3c8f610 — Windows 10 x64.
Method: differential vs GNU bash 5.2.21(1) / 5.3.0.


R7.1 -eu unbound-variable exit code: niu 127, bash 1

$ niu  -eu -c 'echo $UNDEF' ; echo $?      # 127
$ bash -eu -c 'echo $UNDEF' ; echo $?      # 1
$ niu  -u  -c 'echo $UNDEF' ; echo $?      # 127
$ bash -u  -c 'echo $UNDEF' ; echo $?      # 127

With -u alone both give 127; adding -e makes bash switch to 1 while niu stays 127. Same via set -eu in a script. The nounset fatal path does not account for errexit.


R7.2 -- separator is not bash-compatible

$ printf 'echo X\n' | niu -- ; echo $?
niu : stderr "bash: --: option requires a script" ; rc=2
bash: prints X, rc=0        (bash reads stdin)

$ niu -- nosuch.sh ; echo $?
niu : stderr "niu: 系统找不到指定的文件。 (os error 2)" ; rc=1
bash: exit 127
$ niu    nosuch.sh ; echo $?
niu : stderr "niu: nosuch.sh: No such file or directory" ; rc=127   # the normal path is fine

Two defects: -- with no operand rejects stdin (bash reads it), and a missing script after -- exits 1 with a raw, localized OS error instead of the normal message + 127.


R7.3 niu -o with no argument errors instead of listing options

$ niu -o ; echo $?
niu : stderr "bash: -o: option requires an argument" ; rc=2
bash: prints the full option list ; rc=0

R7.4 Error-message branding is inconsistent

Different failure paths identify the shell differently (exit codes/streams otherwise match bash):

command not found        niu: line 1: nosuchcmd: command not found
syntax error             niu: -c: line 1: syntax error ...
unbound variable         niu: line 1: NOPE: unbound variable
exit too many arguments  rubash: exit: too many arguments
invalid long option      bash: --bogus: invalid option ...
invalid shell option     bash: line 0: bogusopt: invalid shell option name

Program name (niu: / rubash: / bash:) and the presence of line N: vary by path. Cosmetic, but it defeats scripts/tests that key on the prefix.

Activity

  1. added
    bug
    P2-medium
    on Oct 17, 2026
  2. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Dependency note: three of the four items here live in src/invocation.rs, which is being rewritten by open PR #697 (fix/382-option-cluster, the clustered-option parser plus the generated SHORT_OPTIONS table). Working on R7.1 (-eu nounset rc), R7.2 (-- separator) or R7.4 (error-message branding) before that lands will conflict with it.

    Suggested order: land #697, then rebase this work on it. R7.3 (niu -o with no argument) probably belongs with the same table, so it may be worth folding in rather than fixing separately.

  3. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Version note (correction to the Method line): the oracle was reached with bare wsl.exe -e bash, which resolves through PATH to /usr/local/bin/bash = GNU bash 5.3.0(1) — not 5.2.21. The 5.2.21 build lives at /bin/bash, and every item in this report was re-checked on it as well; results were identical on both.

    So the two versions are listed in the wrong order above: 5.3.0 was the default, 5.2.21 the cross-check.

    No finding here changes; noting it only so the numbers reproduce. (Incidentally 5.3.0 is also the oracle cited in rubash#641, "the repository oracle is WSL 5.3.0".)

  4. added
    area:cmdline
    on Oct 17, 2026
  5. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Re-checked all four items on current master (7490feb0) against WSL GNU bash 5.3.0(1) and fixed the three unambiguous ones in #778:

    • R7.1 reproduced (127 vs GNU 1) and fixed in the executor: shell.c:1471 only remaps the nounset FORCE_EOF jump to 127 with errexit off; with -e, error.c:169 report_error exits via EXITPROG with 1 first. -u alone stays 127, -eu is 1.

    • R7.2 reproduced on the ShellInvocation route: -- with no operand now reads stdin and the lone - form now ends options too (shell.c:905-911). The engine's -- nosuch.sh route already prints the normal message + rc 127. Host follow-up (niubash repo, not #778): niu -- nosuch.sh exits 1 with the raw localized OS error because the launcher's run_shell_invocation script branch does std::fs::read_to_string(...)? instead of its normal missing-script handling (niu: <name>: No such file or directory, rc 127).

    • R7.3 reproduced on the ShellInvocation route; fixed: argument-less -o/+o is the list_minus_o_opts form (shell.c:931-938), now emitted during option application. (Adjacent, unreported: -O/+O with no argument still errors; GNU prints the shopt table rc 0.)

    • R7.4 — open, product-copy call. Not changed. GNU vs niu 1.4.0 vs engine, same exit codes/streams:

      path GNU bash 5.3.0 niu 1.4.0 engine
      command not found bash: line 1: nosuchcmd: command not found niu: line 1: … bash: line 1: …
      syntax error bash: -c: line 2: syntax error: unexpected end of file from \if' command on line 1` niu: -c: line 2: … byte-identical to GNU
      unbound variable bash: line 1: NOPE: unbound variable niu: line 1: … bash: line 1: …
      exit: too many args bash: line 1: exit: too many arguments rubash: exit: too many arguments rubash: exit: too many arguments
      invalid long option bash: --bogus: invalid option bash: --bogus: invalid option match
      invalid shell option bash: line 0: bash: bogusopt: invalid option name bash: line 0: bogusopt: invalid shell option name byte-identical to GNU

      The decision is two entangled sub-questions: (1) should the engine's own diagnostics print the canonical bash: (the reporter's scripts/tests key on it) or follow the invocation face, when src/builtins/exit.rs:64 currently hardcodes rubash: while every other engine path prints bash:; and (2) the GNU line N: segment that exit: too many arguments drops (not branding, fixable once (1) is decided). The niu: rows are the launcher's own strings and live in the niubash repo.

  6. added 2 commits that reference this issue on Oct 17, 2026
  7. added a commit that references this issue on Oct 18, 2026
  8. added 2 commits that reference this issue on Oct 18, 2026
  9. caomengxuan666 commented on Oct 18, 2026

    @caomengxuan666
    MemberAuthor

    Closed by b6ca9ea + 5f3fb55/eee1aa6 — all four R7 items done.

    R7.1 / R7.2 / R7.3 — confirmed fixed earlier

    R7.1 (-eu nounset rc), R7.2 (-- separator reads stdin, missing script after -- exits 127 with the normal message), R7.3 (-o with no operand lists options): all verified against the engine plus their pinned suites (tests/issue705_nounset_errexit_status.rs, issue705_dashdash_separator.rs, issue705_option_list.rs — green).

    R7.4 — branding + the exit-error semantics behind it

    The probe battery for this item surfaced something bigger than branding: bash 5.2 and 5.3 genuinely diverge on the exit error paths, and the repo baseline is 5.3.0 (architecture.md:55). Every case below was re-pinned against a locally built bash-5.3.0(1)-release binary (probe letters A-X/W/Y from the 2026-10-18 battery); earlier 5.2.21-based conclusions in this thread are superseded wherever they disagree.

    Fixed in b6ca9ea:

    1. exit 1 2 (too many arguments) is no_args(), a DISCARD — not a plain failure. GNU prints the reader-prolog diagnostic, puts 2 into $? (set_exit_status (EX_BADUSAGE)), then top_level_cleanup + jump_to_top_level (DISCARD): the rest of the current command list is abandoned with every parse_and_execute level popped. Script/stdin continue with the next command ($?=2); a -c string is abandoned whole and shell.c:1477's DISCARD catch exits 1. Implemented on the failglob abort scope: GNU abandons the rest of the line, niu only the failing command (one extra statement runs after the error) #753 evalerror vehicle behind a new evalerror_exit_toomany_pending class marker: eval'd/sourced strings unwind WHOLE (no line-contained skip — probe V2), the caller continues (W1/W6), subshells contain it (P6/S9), and ONLY this class triggers the -c stop — failglob/subscript keep their line-contained -c shape (probe G1).

    2. exit abc keeps 5.3's continue-with-$?=2 shape (probe D: -c 'exit abc; echo AFTER' prints AFTER). 5.2 exited rc=2; rubash already matched 5.3, now pinned. get_exitstat checks the first word's numeric validity before no_args, so exit abc def reports the numeric error (Y7).

    3. exit -- is skipped per ISOPTION(word, '-') — exit -- exits with $?, exit -- 1 exits 1, exit -- 1 2 is the too-many error (X4-X6); exit - stays the numeric error (X3). Previously rubash reported exit: --: numeric argument required for all three.

    4. Prologs: too-many/numeric diagnostics carry the executor's diagnostic_prefix (bash: line N: under -c, <script>: line N: in scripts, the exit command's own body line inside functions — U3). The old builtin-level helper read __RUBASH_* through the process environment, which the engine never exports, so the line segment was silently dropped. That was the R7.4 holdout.

    5. Usage errors exit 2 per GNU: enable -f missing operand and invalid option (enable.def:163 default arm, return (EX_USAGE)), cd a b (cd.def:329-330 return (EX_USAGE)). An earlier working-tree pass of this fix recorded cd too-many as rc 1 from a mis-read probe — the 5.3.0 source AND binary both say 2; corrected.

    6. cd diagnostics survive exec 2>file / exec >file rebinds — the buffered-output flush falls back when the route reports no default endpoint, the contract external_redirects.rs already follows. Silent-drop bug found while probing the branding (both channels).

    7. export's usage line gains the GNU [-f] tail (setattr.def:53).

    Known residual micro-divergences (recorded, not fixed)

    • (exit 1 2): GNU leaves 2 behind a script-file subshell but 1 under -c (mode-dependent catch); rubash leaves 2 in both modes. $(exit 1 2): GNU leaves 1 in both modes; rubash leaves 1 under -c (fork copy) but 2 in script mode. Status value only — control flow matches everywhere.
    • The interactive / and logout faces of exit are untouched.

    Tests

    tests/issue705_error_branding.rs — 25 cases, ids 705.20+, each citing its 5.3.0 probe letter per the #746 method: 18 [ratchet] cases proven red on the pre-fix engine by revert-gate, plus [control]/[guard] cases for the numeric shapes, healthy exits, and the -- skip. 5f3fb55 (void; superseded by eee1aa6) / eee1aa6 additionally updated the executor_command_chaining assertions that keyed on the retired rubash: brand and the old exit 1 2 semantics, and fixed the out-of-bounds panic #789 shipped in mark_literal_ifs_chars.

    Two regressions of the 2026-10-18 PR wave that surfaced during the combined-suite verification are filed separately as #795/#796.

  10. added a commit that references this issue on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2-mediumarea:cmdlinebug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions