Skip to content

fix(startup): login profiles source the GNU chain; every startup file's exit ends the invocation (#399) - #412

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/399-login-rc-exit
Oct 18, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/399-login-rc-exit

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Root cause

-l/login invocations parsed the flag but never sourced any login file: run_startup_files only knew the interactive-rc branch and the non-interactive callers went straight to BASH_ENV, so niu -l -c 'cmd' ran the command with no profile at all (master-verified in the issue: none of .bash_profile/.bash_login/.profile/.niubashrc was read). With no profile sourced, the ticket's exit face had nothing to fire through. Two smaller faces of the same item were also unwired: the BASH_ENV/NIU_ENV sourcing and the posix `` sourcing discarded the #402 exit-jump that source_startup_file already returned, and the REPL's #402 wiring borrowed the shell twice inside an `if let` scrutinee (edition-2021 temporaries), so any rc/profile `exit` in the REPL panicked `RefCell already borrowed` and exited 1 instead of carrying the status.

What this PR does

  • Login profile chain (GNU shell.c:1833/1836-1880, the engine's invocation::login_profile_files order): a login shell — -l/--login or a leading-dash argv[0] — sources /etc/profile (resolved through the shell root, so a winuxcmd etc/profile answers when one ships) and the FIRST EXISTING of ~/.bash_profile → ~/.bash_login → ~/.profile, instead of the rc / BASH_ENV branches, interactive or not. --rcfile is never consulted on the login face; --noprofile skips the files but keeps the branch.
  • Exit-jump on every remaining startup face (exit inside a startup rc file is swallowed: niu continues and exits 0 where GNU exits with that status #402 plumbing): an exit in a profile, in the BASH_ENV/NIU_ENV file, or in a posix `` file ends the invocation with that status before the prompt/command/script runs — REPL, piped -i, `-i -c`, `-l -c`, `-l script`, and the piped stdin route.
  • repl.rs (3 lines): bind run_startup_rc()'s Option before the if let so the EXIT-trap borrow is legal. This is the only repl.rs touch, outside the paste-input editing area (PasteChunkEditMode / translate_event are untouched).

GNU oracle (WSL GNU Bash 5.3.0, stdin method, scratch HOME)

probe GNU master this PR
.bash_profile=echo m; exit 9, -l -c 'echo BODY' m, rc 9, no BODY BODY, rc 0 m, rc 9, no BODY
all three user files staged, -l -c first existing only (.bash_profile) nothing first existing only
only .profile staged, -l -c sourced not sourced sourced
--noprofile -l -c (working flag order; GNU 5.3 misparses -l --noprofile as --) nothing sourced — nothing sourced
-l -i -c with rc + BASH_ENV staged /etc/profile + profile; no rc, no BASH_ENV — same
-l script with profile exit 9 marker, rc 9, script never runs script runs, rc 0 marker, rc 9
BASH_ENV=exit 6, -c marker, rc 6, no BODY BODY, rc 0 marker, rc 6
ENV=exit 5, --posix -i -c marker, rc 5, no BODY BODY, rc 0 marker, rc 5
--rcfile F -i -c + exit 7 (already fixed by #403) rc 7 rc 7 rc 7 (unchanged)

Tests

10 new cases in tests/interactive.rs (issue399_*, execution-level markers + exit statuses, --test-threads=1); driver gains spawn_shared_home_with_args so a test can stage profile files before the process exists. Full interactive target: all tests pass except 5 pre-existing environment failures (continuation_single_quote/paren, ctrl_c_interrupts_external_command, ctrl_d_after_background_job, gitbash_factory_prompt) that fail identically on a pristine origin/master build on this host. startup_files, host_contract, niubash-runtime lib tests, and the invocation-surface targets all pass.

Closes #399 (items 2/3 of the ticket body — the -i -c option set (himBHc) — are a separate surface and untouched here; item 1 is this PR plus the already-merged #403).

… 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).

@caomengxuan666 caomengxuan666 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR verdict: approve (no blocking findings) — core claims independently verified

I re-derived this on top of 7c42373 with my own probes (WSL GNU Bash 5.3.0, plus a built base-vs-branch comparison on this host).

Verified:

  • Root cause: on 7c42373, -l/--login is parsed (legacy -c -l shape, help text) but no startup path sources any profile — run_startup_files only knows the posix-ENV and rc branches and source_non_interactive_env goes straight to BASH_ENV/NIU_ENV. Confirmed at source level and at runtime.
  • The claimed RefCell latent bug is real and is exactly as described: on a pristine 7c42373 build, a plain REPL session whose rc ends with exit 5 panics RefCell already borrowed at repl.rs:1974 (the if let scrutinee temporary holds the borrow while the EXIT-trap path re-borrows) and exits 1. On this branch the same session exits 5, no panic, no prompt rendered. The fix (bind the Option before the if let) is minimal and correct, and the paste area is untouched (repl.rs diff is exactly the 3-line bind; no PasteChunkEditMode/translate_event changes).
  • GNU order: /etc/profile → FIRST EXISTING of .bash_profile/.bash_login/.profile, never the rc — matches my WSL probes (.bash_login beats .profile once .bash_profile is removed; -l -i sources no rc). -l --noprofile misparse pinned correctly: GNU 5.3 here also fails with bash: --: invalid option; the working order --noprofile -l skips the chain while keeping the login branch (no rc fall-through — probe-confirmed).
  • Exit-jump plumbing on every remaining face (profile, BASH_ENV, posix $ENV, REPL login) mirrors the #402 discipline; the completion-probe call correctly discards the Option (synthetic surface, never login).
  • Tests: the 10 issue399_* cases pass 4/4 runs at --test-threads=1. Full interactive target: only the base-confirmed environment failures fail — I reproduced continuation_single_quote, continuation_subshell_paren, gitbash_factory_prompt identically on a pristine 7c42373 build (ctrl_c/ctrl_d passed in my run — flaky-env, consistent with your list). startup_files/completion_probe/repl_command/issue404_406_invocation_surface/smoke green; host_contract rubash_setopt... and niubash-runtime test_get_builtin_commands fail identically on base (pre-existing env).
  • Diff surface: files match the PR description exactly; no GPL fragments or translation-style wording.

Non-blocking findings

  1. GNU parity overclaimed on three additive/posix faces — shells.rs:1260-1261 ("never BASH_ENV"), :1137 (login branch before posix), :1291. My WSL 5.3 probes: (a) bash -l -c with BASH_ENV staged sources profile AND BASH_ENV — BASH_ENV is not gated on login-ness for non-interactive shells; (b) bash --posix -l -c sources nothing (posix suppresses the login chain; no ENV non-interactively); (c) sh -l -i -c sources profiles and $ENV. This PR makes login strictly exclusive, so niu diverges on those rows while its comments cite shell.c as the authority. The oracle table only covers -l -i -c + BASH_ENV (where interactivity already suppresses BASH_ENV), so the exclusivity was never GNU-checked. Minor in practice (BASH_ENV+login and --posix+login are rare), and internal consistency with the rubash engine's own (equally exclusive) dispatch is preserved — but the comment wording presents GNU-verified behavior that isn't. Either soften the wording or follow the additive model.

  2. The RefCell fix ships with no regression test. None of the 10 new cases covers the plain (non-login) REPL rc-exit face — the exact path the 3-line repl.rs fix repairs (and no pre-existing test covers it either, which is how the panic survived #402). I verified it manually both ways; please add one spawn_custom case (rc ends exit 5 → session status 5, no prompt).

Merge-order note

Whichever of #412/#413 merges second needs a trivial EOF conflict resolution in tests/interactive.rs (both append their block at the same anchor); driver.rs and shell.rs auto-merge.

@caomengxuan666
caomengxuan666 merged commit 6ed0e55 into master Oct 18, 2026
9 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
…orm; login env gates stay independent (review round)

CR round on #413 and #412.

#413 blocking - activation timing: the rbash face armed the restricted
option inside Shell::new, before any startup file runs, while the
-r/--restricted flag form activates in the engine's
ShellInvocation::finish_startup (invocation.rs: 'after the host
finishes startup files' - the deliberate GNU order). The divergence was
runtime-visible: 'rbash.exe -c' refused a BASH_ENV file with
'.: <path>: restricted' and 'rbash -l -c' refused the whole profile
chain, where the flag form and GNU load both fine. Shell::new now only
records the face mark (one line keyed off unmarked_name, dropping the
redundant .exe strip and '-' strip); the option write moved into
Shell::finish_startup, right beside the flag write. Startup-file
equivalence is pinned by a test: argv0 face and flag form load
BASH_ENV and the profile chain identically and go restricted for the
command line. CR's two blocking repros both pass.

#412 follow-ups: three comments over-claimed GNU exclusivity and the
behavior followed them - the login profile chain replaces only the
interactive-RC branch, while the env-file gates are independent of
login state: 'bash -l -c' sources the chain AND BASH_ENV (shell.c
1214-1220 keys on interactivity/posix), and 'sh -l -i -c' sources the
chain AND $ENV (shell.c 1238-1246 keys on posix+interactive). Both
re-measured against the WSL 5.3.0 oracle and implemented. Also adds
the missing regression for the run_repl RefCell fix: a plain REPL with
'exit 5' in the rc ends with status 5 and no panic.
caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
…orm; login env gates stay independent (review round)

CR round on #413 and #412.

#413 blocking - activation timing: the rbash face armed the restricted
option inside Shell::new, before any startup file runs, while the
-r/--restricted flag form activates in the engine's
ShellInvocation::finish_startup (invocation.rs: 'after the host
finishes startup files' - the deliberate GNU order). The divergence was
runtime-visible: 'rbash.exe -c' refused a BASH_ENV file with
'.: <path>: restricted' and 'rbash -l -c' refused the whole profile
chain, where the flag form and GNU load both fine. Shell::new now only
records the face mark (one line keyed off unmarked_name, dropping the
redundant .exe strip and '-' strip); the option write moved into
Shell::finish_startup, right beside the flag write. Startup-file
equivalence is pinned by a test: argv0 face and flag form load
BASH_ENV and the profile chain identically and go restricted for the
command line. CR's two blocking repros both pass.

#412 follow-ups: three comments over-claimed GNU exclusivity and the
behavior followed them - the login profile chain replaces only the
interactive-RC branch, while the env-file gates are independent of
login state: 'bash -l -c' sources the chain AND BASH_ENV (shell.c
1214-1220 keys on interactivity/posix), and 'sh -l -i -c' sources the
chain AND $ENV (shell.c 1238-1246 keys on posix+interactive). Both
re-measured against the WSL 5.3.0 oracle and implemented. Also adds
the missing regression for the run_repl RefCell fix: a plain REPL with
'exit 5' in the rc ends with status 5 and no panic.
@caomengxuan666
caomengxuan666 deleted the fix/399-login-rc-exit branch October 21, 2026 08:14
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.

[host] interactive/login startup path: an rc file exit is ignored, -i -c sets no monitor/histexpand, and -l does not source .bash_profile

1 participant