Skip to content

fix(history): honor the shell's own history option at interactive init; HISTFILE default applies once pre-startup (niubash#660/#661) - #2155

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/660-661-history-write
Oct 30, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/660-661-history-write

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes unixwin/niubash#660, fixes unixwin/niubash#661. Two commits, one per issue. Not to be self-merged (owner-directed review).

Commit 1 — niubash#660 (set +o history did not stop recording)

set +o history on the engine's interactive surface did not stop the session's history stream: a startup-file opt-out was overwritten, the session recorded, and finish_shell saved the lines to $HISTFILE anyway.

Root causes (rubash engine):

  1. initialize_interactive_history (script_driver.rs) and apply_init_interactive_defaults (main.rs) forced set -o history ON unconditionally at the interactive-history init, which runs after the startup files. GNU applies the default once, before the startup files, and only when nothing set the option: shell.c:1835-1837 init_interactive (if (enable_history_list == -1) enable_history_list = 1;; shell.c:1866-1867 init_interactive_script for the -i script arm). The post-startup load site shell.c:806-811 never touches the option. Both sites now write the carrier only when absent, so an opt-out — and any preset — stands.
  2. The startup file load ran unconditionally. GNU gates the whole load on shell_initialized == 0 && history_lines_this_session == 0 (shell.c:810-811): startup-file lines (history -s in an rcfile) must not be doubled by the file read, and the HISTSIZE/HISTFILESIZE defaults plus the sv_histsize("HISTFILESIZE") truncation live inside load_history (bashhist.c:319-355) and inherit that gate.
  3. Re-arming had no reload. builtins/set.def:456-472 bash_set_history, FLAG_ON arm: if (history_lines_this_session == 0) load_history(); — set -o history at the prompt resumes the session from $HISTFILE. GNU has no already-loaded guard: verified against bash 5.3.20 (rcfile set +o history, prompt set -o history, history lists the seeded file twice — startup load plus re-arm load). New Executor::reload_history_file_on_rearm is hooked where the option is applied ON (fast -o arm in apply_set_positional_operands, slow execute_set by transition); the rubash#1668 first-arm loader keeps its own already-loaded guard.

Commit 2 — niubash#661 (HISTFILE path-of-record)

The engine re-defaulted HISTFILE at the post-startup load site, so startup-file decisions did not stick: an rcfile unset HISTFILE was resurrected to ~/.bash_history (the session then loaded and saved the default file anyway), and an rcfile assignment was honored only by luck of set_if_not.

GNU order (verified 5.3.20): HISTFILE is defaulted once at shell.c:580 shell_initialize → variables.c:654-661 (if (remember_on_history) set_if_not("HISTFILE", bash_default_histfile()), bashhist.c:420 → ~/.bash_history), before the startup files (shell.c:722); the load site reads whatever HISTFILE then holds (bashhist.c:336 at load time) with no post-startup re-default; the exit save reads the live value (bashhist.c:495). The default now lives in apply_interactive_history_defaults (pre-startup; -i script gets the same arm — shell.c:546-547 forced_interactive reaches init_interactive there too), and the post-startup site only truncates and reads the live value.

Verification

  • GNU bash 5.3.20 (WSL /usr/local/bin/bash) script-file A/B on every changed shape: rcfile opt-out creates no history file; first set -o history loads the seeded file; the reload is gated on session lines (no duplicate); the opt-out re-arm lists the seeds twice with the exit save appending only session lines; rc-unset/rc-assignment/live-export/bare--w all byte-matched.
  • tests/issue660_history_stop_record_regressions.rs (5 pins) and tests/issue661_histfile_live_redirect_regressions.rs (4 pins) — all green.
  • psmux ConPTY interactive A/B vs the pristine origin/master binary: pristine still resurrects the default after the rcfile unset and still saves the opted-out session; the fixed binary honors the startup-file decision. In-pane stop/resume/export-redirect flows GNU-identical on both.
  • A/B vs pristine: the regression target's failure set is identical (64 pre-existing failures on both sides); history-family test files (1016/1072/1073/1254/1255, 1629, 1633, 1668, 1852, 1830, 1288, 1314, 1660, 1071, 2072, script_driver_history_in, issue312) unchanged — 1830 and 1852 failures are pre-existing at the base tip.
  • Rebased onto the current master tip (2503977); battery re-run green.

Disclosure: cargo fmt fails on this host with os error 206 (argument-list too long, the standing hook precedent) — rustfmt was applied to each touched file and the commits are made with --no-verify for that reason only.

… option; re-arm reloads $HISTFILE (niubash#660)

Fixes niubash#660: `set +o history` did not stop the session's history
stream on the engine's interactive surface — a startup-file opt-out was
overwritten and the session recorded and saved to $HISTFILE anyway.

Root causes (rubash engine, script_driver.rs / main.rs):

1. initialize_interactive_history (script_driver.rs:4098) and
   apply_init_interactive_defaults (main.rs:1553) forced
   `set -o history` ON unconditionally at the interactive-history init,
   which runs AFTER the startup files. An rcfile `set +o history` was
   therefore clobbered: recording re-armed and finish_shell saved the
   session lines. GNU applies the default once, BEFORE the startup
   files, and only when nothing set the option: shell.c:1835-1837
   init_interactive `if (enable_history_list == -1) enable_history_list
   = 1;` (shell.c:1866-1867 init_interactive_script for the `-i script`
   arm). The post-startup load site shell.c:806-811 never touches the
   option. Both sites now write the carrier only when absent, so an
   opt-out — and any preset — stands.

2. The startup file load ran unconditionally. GNU gates the whole load
   on `shell_initialized == 0 && history_lines_this_session == 0`
   (shell.c:810-811): startup-file lines (`history -s` in an rcfile)
   must not be doubled by the file read, and the HISTSIZE/HISTFILESIZE
   defaults plus the sv_histsize("HISTFILESIZE") truncation live inside
   load_history (bashhist.c:319-355) and inherit that gate.

3. Re-arming had no reload. builtins/set.def:456-472 bash_set_history,
   FLAG_ON arm: `if (history_lines_this_session == 0) load_history();`
   — a `set -o history` at the prompt resumes the session from
   $HISTFILE. There is NO already-loaded guard on GNU's arm: verified
   against bash 5.3.20 (rcfile `set +o history`, prompt `set -o
   history`, `history` lists the seeded file twice — startup load plus
   re-arm load). The new Executor reload_history_file_on_rearm is
   hooked where the option is applied ON (the fast `-o` arm in
   shell_options.rs apply_set_positional_operands, and the slow
   execute_set path by transition), while the rubash#1668 first-arm
   loader keeps its own already-loaded guard.

Verification:
- GNU bash 5.3.20 (WSL /usr/local/bin/bash) script-file A/B: rcfile
  opt-out creates no history file; first `set -o history` loads the
  seeded file (l1/l2/history listing); the reload is gated on
  session lines (no duplicate); the opt-out re-arm lists the seeds
  twice and the exit save appends only session lines — all matched.
- tests/issue660_history_stop_record_regressions.rs: 5 pins covering
  the in-pane record/save gate, the rcfile opt-out, the re-arm reload
  (with the GNU duplication face), and both script shapes.
- A/B vs pristine origin/master (8755dd7): the regression target's
  failure set is identical (64 pre-existing failures both sides);
  history-family test files (1016/1072/1073/1254/1255, 1629, 1633,
  1668, 1830, 1288) unchanged — 1830's failure is pre-existing.
- fmt: cargo fmt hit os error 206 (hook precedent); rustfmt applied
  per touched file, commit made with --no-verify for that reason.
…iles (niubash#661)

Fixes niubash#661: the engine re-defaulted HISTFILE at the
post-startup interactive-history load site (initialize_interactive_
history), so a startup-file decision did not stick as the path of
record:

- an rcfile `unset HISTFILE` was resurrected to the default
  (~/.bash_history) — the session then loaded and SAVED the default
  file although the user's rc had detached it;
- an rcfile assignment was honored only by luck of set_if_not.

GNU order (verified 5.3.20): HISTFILE is defaulted ONCE, at
shell_initialize (shell.c:580 -> variables.c:654-661 initialize_shell_
variables: `if (remember_on_history) { name = bash_default_histfile();
set_if_not("HISTFILE", name); }`), BEFORE the startup files run
(shell.c:722); the load site reads whatever HISTFILE then holds
(bashhist.c:336 `hf = get_string_value ("HISTFILE")` at load time) and
there is no post-startup re-default; maybe_save_shell_history
(bashhist.c:495) reads the live value at save time. The bashhist.c:420
bash_default_histfile value is the user's ~/.bash_history (rubash#1216
keeps that value; only its placement changes).

Changes:
- main.rs: the default moves into apply_interactive_history_defaults,
  the pre-startup defaults site (already reached by apply_init_
  interactive_defaults for the bare `-i` / `-i -c` / `-i -s` drivers),
  gated on remember_on_history (the just-armed option carrier) with
  the same set_if_not shape.
- main.rs run_script_file_with_init: `-i script` reaches GNU
  init_interactive too (shell.c:546-547 forced_interactive), so the
  same pre-startup arm applies there; without it, dropping the
  post-startup re-default would leave the script-file path with no
  history default at all.
- script_driver.rs initialize_interactive_history: the re-default is
  gone; the site only truncates and reads the live HISTFILE.

Verification (GNU bash 5.3.20, WSL /usr/local/bin/bash, script-file
A/B): rcfile `unset HISTFILE` + piped `-i` session creates no history
file on either side; rcfile HISTFILE=<custom> receives the session
lines and the default never appears; an in-pane `export HISTFILE`
redirects the exit save and the previous file stays byte-identical;
bare `history -w` follows the live value and the pre-assignment name
is never written — all matched. psmux ConPTY interactive A/B vs the
pristine origin/master binary: pristine still resurrects the default
after the rcfile unset and saves the opted-out session, the fixed
binary honors the startup-file decision.

tests/issue661_histfile_live_redirect_regressions.rs: 4 pins (export
redirect, rc-unset finality, rc assignment honored, bare -w live
value). A/B vs pristine: the regression target's failure set stays
identical (64 pre-existing failures both sides); history-family test
files unchanged.
@caomengxuan666
caomengxuan666 merged commit dc65111 into master Oct 30, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/660-661-history-write branch October 30, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant