Skip to content

fix(startup): a top-level exit in the user rc terminates the invocation (#402) - #403

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/402-startup-rc-exit
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/402-startup-rc-exit

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Closes #402. A top-level exit (or an errexit unwind) executed while the startup rc is sourced was flattened into an ordinary command status, so the caller's command line still ran and the process exited 0. GNU ends the shell with the rc's status (jump_to_top_level EXITPROG unwinds reader_loop) and never runs the command line.

Change

  • source_startup_file records the engine's ExitCode unwind (execute_script_inner sets script_exit_jump) and returns the status; run_startup_files / run_startup_rc / run_interactive_startup_rc propagate it.
  • The invokers terminate with that status before running the command line: -i -c, -i script, the piped--i engine driver, -C, and the REPL (which ends the session before the first prompt, mirroring the existing PROMPT_COMMAND jump path).
  • The synthetic . <path> source bypasses the grouped readers (allow_grouped_readers=false); those read history/alias-bearing script text and swallow the unwind before the caller can observe it. The . <path> command is a single simple command, so the tokenize+parse+execute_ast route is equivalent.

Verification (WSL GNU Bash 5.3.0, file-based rc, -i -c 'echo AFTER')

rc contents GNU this PR
echo RAN + exit 3 RAN, rc 3, no AFTER RAN, rc 3, no AFTER
echo RAN + exit 0 RAN, rc 0, no AFTER RAN, rc 0, no AFTER
echo RAN RAN, AFTER, rc 0 RAN, AFTER, rc 0
false AFTER, rc 0 AFTER, rc 0
set -e + false rc 1, no AFTER rc 1, no AFTER

The set -e row was previously swallowed too; the engine classifies exit and errexit with the same ExitCode unwind (GNU EXITPROG/ERREXIT), so both now match GNU.

Tests

  • New tests/startup_files.rs: issue402_exit_in_rcfile_terminates_before_command_line, issue402_exit_in_default_rc_terminates_repl_command, issue402_startup_rc_guards (no-exit / exit 0 / plain failing rc).
  • Green locally: startup_files 29/29, repl_command 11/11, issue359_startup_pty 2/2, niubash-runtime --lib 538/538; cargo fmt --check clean.

…on (#402)

A top-level `exit` (or errexit unwind) executed while the startup rc is
sourced was flattened into an ordinary command status: the command line
still ran and the shell exited 0, where GNU ends the shell with that
status (jump_to_top_level EXITPROG unwinds reader_loop).

`source_startup_file` / `run_startup_files` now report the engine's
`ExitCode` unwind, and the invokers (`-i -c`, `-i script`, the piped `-i`
driver, `-C`, and the REPL) terminate with that status before running the
rest. The synthetic `. <path>` source bypasses the grouped readers, which
otherwise swallow the unwind before the caller can observe it.

Verified against WSL GNU Bash 5.3.0 (file-based rc, `-i -c 'echo AFTER'`):

  rc "echo RAN; exit 3"  -> RAN, rc 3, no AFTER
  rc "echo RAN; exit 0"  -> RAN, rc 0, no AFTER
  rc "echo RAN"          -> RAN, AFTER, rc 0
  rc "false"             -> AFTER, rc 0 (maybe_execute_file: warn + keep going)
  rc "set -e; false"     -> rc 1, no AFTER

The `set -e` case was previously swallowed too; the engine classifies exit
and errexit with the same `ExitCode` unwind (GNU EXITPROG/ERREXIT), so it
now matches GNU. Regression tests added in tests/startup_files.rs.

Closes #402.
@caomengxuan666
caomengxuan666 merged commit 1e0b732 into master Oct 17, 2026
8 of 11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/402-startup-rc-exit branch October 17, 2026 11:54
caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
… startup file's exit ends the invocation (#399)

#402/#403 wired the top-level-exit jump for the rc file faces; the
remaining #399 surface is the login path and the two env-file branches
GNU ends the shell on as well.

- A login shell (-l/--login or a leading-dash argv[0]) now sources
  /etc/profile (resolved through the shell root) and the FIRST EXISTING
  of ~/.bash_profile, ~/.bash_login, ~/.profile - the engine's
  login_profile_files order - instead of falling through to the rc or
  BASH_ENV branches (shell.c:1833), interactive or not, with --rcfile
  never consulted. --noprofile skips the files but keeps the branch.
- The chain rides the #402 exit-jump plumbing: an exit in a profile
  ends the invocation with that status before the prompt, command
  string, or script runs, on every face (REPL, piped -i, -i -c, -l -c,
  -l script, stdin).
- A BASH_ENV/NIU_ENV exit and a posix $ENV exit propagate the same way
  (GNU oracles: rc 6 and rc 5, the command never runs).
- repl.rs: bind run_startup_rc()'s Option before the 'if let' - the
  scrutinee's borrow_mut temporary (edition 2021) made every rc or
  profile exit in the REPL panic 'RefCell already borrowed' and exit 1
  instead of carrying the status.

Verified against WSL GNU Bash 5.3.0 (scratch HOME, stdin method): a
profile exit 9 under -l -c prints the marker and exits 9 with no BODY;
-l -i -c sources /etc/profile plus the profile and never ~/.bashrc or
BASH_ENV; --noprofile sources nothing; -l script with a profile exit
runs no script. Regressions in tests/interactive.rs (the driver gains
spawn_shared_home_with_args).
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.

exit inside a startup rc file is swallowed: niu continues and exits 0 where GNU exits with that status

1 participant