Skip to content

fix(tilde,cd,pushd): one HOME baseline per operation — GNU missing-vs-empty (rubash#754) - #819

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/754
Oct 18, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/754

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Root cause (one paragraph)

Issue #754's HOME-baseline family splits in two on inspection. The headline pair — cd ~ landing in the startup HOME while mkdir ~/x (word expansion) follows the current $HOME, so mkdir ~/x && cd ~/x disagrees — is real in the installed niu 1.4.0 binary the issue was verified against, but does not reproduce from source anywhere in v1.4.0..master (built from source at v1.4.0 f69e7900 and at e785c3c8, the repro passes in both; the shipped binary lags its version string — provenance forensics moved to #827). What does still diverge on master is the same family's missing-vs-empty rule: home_value filtered HOME on non-empty, so a set-but-empty HOME fell through to USERPROFILE while bare cd refused with "HOME not set" — two operations, two baselines, exactly the reported pattern. GNU settles both halves: lib/tilde/tilde.c:344-356 tilde_expand_word() translates ~/~/ from sh_get_env_value("HOME") — the live variable cell read at every expansion (variables.c get_string_value); a set-but-empty value is still a value (glue_prefix_and_suffix tilde.c:304-317 yields the bare suffix, so HOME=; cd ~ is cd "" → "null directory", cd.def:331-336); only a missing variable reaches the fallback (tilde.c:349-350 APPDATA on _WIN32, tilde.c:354-356 sh_get_home_dir = variables.c:736 getpwuid — fixed per uid, never tracking $HOME; rubash's Windows stand-in stays USERPROFILE). The operand-less cd refusal is dirname == 0 only (cd.def:316-322, POSIX cd step 1): an empty HOME flows on through make_absolute and the chdir is a same-directory success (rc 0, silent) — only a missing HOME reports "HOME not set". pushd's Dir operand delegates to cd_builtin (pushd.def:296-302), so an empty expansion inherits "null directory" (prolog names pushd); with -n, pushd.def:301 pushes the RAW operand word unconditionally (no normalize, no existence probe); popd lives in the same file — pushd.def:318 popd_builtin() breaks its option walk on an empty word (pushd.def:356-363). Fixes here: presence-based HOME read in the tilde module, the operand-less cd lands on the cwd for an empty HOME, the $(cd && pwd) fast path delegates the empty case to the real cd/pwd pair (missing keeps the fast verdict), pushd -n pushes raw words and the stack round-trip preserves blank members, popd judges empty words as terminators. No GPL source is reproduced — semantics only, cited by file:line.

GNU differential evidence

Oracle: WSL GNU bash 5.3.0(1)-release, stdin method, isolated fresh dirs; stdout/stderr separate. Rubash columns are source-built artifacts (installed-binary provenance is unverifiable — #827).

# Case GNU 5.3.0 master before this branch
T02 HOME="$PWD/newhome"; cd ~ (stdin form) current home current home current home
T02c same, -c form (source-built) current home current home current home
T03 mkdir ~/x && cd ~/x (-c form) both current; enters newhome/x agree agree
T04-T20 no-operand cd / subshell / command cd / pushd / assignments / redirect / [[ ]] / case / export / relative HOME / sequential changes / cd -- / eval'd tilde / function / PATH-style a:~/x all follow current $HOME all match GNU all match GNU
E1 HOME=; t=~ "" C:\Users\cmx ""
E2 HOME=; u=~/x, a=~/x /x C:\Users\cmx/x /x
E3 HOME=; cd ~ rc 1, cd: null directory rc 0 into USERPROFILE rc 1, cd: null directory
E4 HOME=; pushd ~ rc 1, pushd: null directory rc 0 rc 1, pushd: null directory
E5 HOME=; pushd -n ~; dirs rc 0, stack <cwd> "" (trailing blank element) rc 0, stack <cwd> <cwd> rc 0, stack <cwd> ""
E6 HOME=; popd ~ rc 1, popd: directory stack empty (empty word ends the arg walk, pushd.def:356-363) rc 2 invalid argument rc 1, popd: directory stack empty
E7 HOME=; cd (no operand) rc 0, in place, silent (cd.def:316-322: only dirname == 0 refuses) rc 1 HOME not set rc 0, in place, silent
E8 HOME=; out=$(cd && pwd) out=<cwd>, rc 0 out=<USERPROFILE>, rc 0 out=<cwd>, rc 0
E9 unset HOME; cd (no operand) rc 1, cd: HOME not set rc 1 ✓ rc 1 ✓
E10 unset HOME; out=$(cd && pwd) rc 1, cd: HOME not set, empty output rc 1 ✓ (fast verdict) rc 1 ✓
E11 unset HOME; cd ~ getpwuid home (/root) USERPROFILE stand-in (persona for tilde.c:349-356) unchanged

(A previous iteration of this table mis-pinned E7/E8 as "HOME not set" for the empty case — that was an extrapolation from the missing-HOME probe, corrected per CR F1. The installed-niu column moved to #827.)

T01-T20 full matrix (20 cases × -c/stdin/script-file forms) is byte-identical to GNU on this branch after normalization of the platform path spellings.

Test plan

  • New tests/issue754_tilde_home_baseline_regressions.rs — 12 differential cases, all driven by the source-built binary (CARGO_BIN_EXE_rubash): the issue's mkdir ~/x/cd ~/x pair in -c and stdin forms, sequential reassignments, every tilde consumer (assignment/redirect/[[ ]]/case), command cd/subshell/function/pushd baselines, the empty-HOME battery (E1-E8), the operand-less missing-vs-empty verdicts (E7/E9), both comsub verdicts (E8/E10), the pushd -n stack shape (E5), and the Windows USERPROFILE persona (E11).
  • Unit test in src/expand/tilde/tilde.rs: empty HOME is a value, not a fallback trigger.
  • Full suite, bidirectional failure-set gate (cargo test --no-fail-fast, 296 targets): master c6f4a9b3 = 43 failures, this branch = 43 failures, set-diff empty in both directions (all 43 are the known environment failures: examples fixtures, fd_redirects::c_external_*, jobs/signals timing, perf canaries, winuxcmd passwd fixtures — failure modes verified identical, not just names).
  • cargo fmt --check clean.

@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.

Independent CR verdict: REQUEST-CHANGES

Reproduced everything on an independent worktree (cr-819 @ 2e720d5), vendored bash @ b4608166 (5.3 patch 15), live oracle WSL GNU bash 5.3.0(1)-releases across stdin / script-file / native -c forms. The tilde half and the pushd/popd half are GNU-exact and approve-quality; one substantive finding on the cd empty-HOME verdict, which this PR enshrines as GNU-correct but GNU does not do.

F1 (substantive): cd.def:316-321 fires "HOME not set" for MISSING only — set-but-EMPTY HOME bare cd succeeds in cwd (rc 0, silent); the new fast path pins the opposite as GNU

  • Source mechanism: get_string_value returns var->value (variables.c:2475-2509) — "" (non-NULL) for set-but-empty, NULL only for missing. cd.def:319 if (dirname == 0) therefore fires only for missing. Empty dirname flows to change_to_directory (cd.def:389) → make_absolute("", cwd) = sh_makepath(cwd, "") = cwd (general.c make_absolute) → chdir(cwd) succeeds → rc 0, no message.
  • Live GNU 5.3.0(1)-release, verified in all three invocation forms: HOME=; cd → rc 0, PWD unchanged, silent; HOME=; out=$(cd && pwd) → out=, substatus 0. (unset HOME; cd → rc 1 "HOME not set" — matches the PR.)
  • Consequences:
    • Differential rows E6/E7 in the PR body, commit message, and CHANGELOG misstate the GNU oracle (E6 "GNU: rc 1 HOME not set" — actual rc 0 stay-in-cwd; E7 "GNU: cd: HOME not set, sub-status 1" — actual out= rc 0). For a project whose methodology is cited GNU differentials, a wrong permanent-oracle row is the thing to fix.
    • The new fast-path arm (src/executor/command_substitution.rs:1108-1125) reports "HOME not set" for the empty case. That mirrors rubash's own cd (src/builtins/cd/paths.rs:57-66 shell_var filters empty — a pre-existing GNU divergence this PR does not touch), so internal consistency is achieved, but the comment claims it mirrors cd.def:316-321 and the tests (comsub_cd_fast_path_matches_builtin_home_verdict, operandless_cd_reports_home_not_set_for_empty_and_missing) pin the empty-case divergence as intended GNU behavior. Before this PR the fast path returned USERPROFILE rc 0 (also non-GNU); after, error rc 1 (also non-GNU). Net: behavior still diverges from GNU, now with a wrong citation.
  • Fix options: (a) implement GNU — empty HOME → fast path resolves cwd rc 0, and ideally the pre-existing cd.rs arm too; or (b) keep the internal-consistency choice but document it as a deliberate divergence and correct the E6/E7 rows, CHANGELOG, and the commit-message claim "missing OR empty → HOME not set (cd.def:316-321)".

Nits

  • N1: builtins/popd.def does not exist — popd_builtin lives in builtins/pushd.def:318 (the empty-word else break is pushd.def:349-353). Cited in the PR body, commit message, pushd/parse.rs comment, test header (line 22), and CHANGELOG.
  • N2: tilde.rs:17 comment cites "builtins/cd.def:338" for null directory — it is cd.def:333-336 (338 is the cd - branch). PR body's :335 is correct.
  • N3: pushd.rs comment "the empty word is pushed onto the stack verbatim, matching GNU" — rubash pushes normalize_stack_dir("") = PWD (HOME=; pushd -n ~ → stack <cwd> <cwd>), GNU pushes savestring("") (pushd.def:303, stack <cwd> ""). The -n normalization persona is pre-existing, but this specific comment claim is false; push the empty word verbatim or fix the comment.

Verified good

  • tilde.c anchors exact (347 live cell / 350 APPDATA / 356 sh_get_home_dir / glue 304-317 empty-prefix→bare-suffix); pushd.def:298 delegation quote verbatim-accurate; popd empty-word walk semantics correct.
  • Existence-based tilde read correct: HOME= → E1 "" / E2 /x (GNU-exact); E3/E4/E5 match live probes (null directory rc 1, pushd -n ~ rc 0, popd default pop / "directory stack empty"); E8 USERPROFILE persona pinned in doc comment + missing_home_falls_back_to_userprofile_for_tilde_only.
  • Gates: 12/12 issue754 tests pass; full suite branch vs master c6f4a9b (12 failing targets re-run on base): stable failure sets identical 43 = 43, set-diff empty (my branch run showed 2 extra sigquit names under full-suite load only — both pass in isolation on the branch; issue526's stable failing pair is byte-identical on both sides); cargo fmt --check clean; 2 commits, no smuggled changes.

Forensic (independent): the installed-binary form REPRODUCES — release-integrity follow-up warranted

Installed C:/Users/cmx/AppData/Local/Programs/Niubash/niu.exe (prints "Niubash 1.4.0 — rubash git e785c3c / winuxcmd 1.1.14") does reproduce the -c startup-HOME form: niu -c 'cd D; mkdir -p newhome; HOME=D/newhome; mkdir ~/x && cd ~/x' → mkdir created D/newhome/x (current HOME) while cd ~/x failed on /c/Users/cmx/x (startup HOME); bare cd ~ → /c/Users/cmx. Source builds at both v1.4.0 (f69e790, peeled tag) and e785c3c (the advertised hash) pass the same repro (pair enters newhome/x; cd ~ → newhome). No niu target or "Niubash 1.4.0 — rubash git" version string exists in this repo (src/bin has only bash.rs) — niu.exe is a separate packaging tree whose embedded rubash predates the hash it prints. So the author's narrow claim is independently confirmed (nothing in v1.4.0..master carries the capture), and the escalation stands: the shipped artifact misrepresents its source — worth a packaging-side issue.

@caomengxuan666 caomengxuan666 changed the title fix(tilde,pushd): one HOME baseline per operation — GNU missing-vs-empty (rubash#754) fix(tilde,cd,pushd): one HOME baseline per operation — GNU missing-vs-empty (rubash#754) Oct 18, 2026
@caomengxuan666

Copy link
Copy Markdown
Member Author

CR F1/N1/N3 全部按 (a) 处理,force-push 8048c1cc(+8048c1cc~1 的 docs 提交)已更新。

F1 — 接受,我的 E6/E7 是误读:首轮 GNU 探针只实测了 unset HOME(缺失)形态,置空形态是外推的。重测三形态确认:HOME= 下裸 cd rc 0 原地、无诊断(cd.def:316-322 的裁定是 dirname == 0,POSIX cd step 1——空串流入 make_absolute 粘 cwd);cd "" 空操作数才走 cd.def:331-336 "null directory"(cd ~ 路径,与裸 cd 不同路)。已实现 GNU 语义:

  • cd.rs resolve_target:HOME 存在但空 → 以 cwd 为目标走正常 chdir 流程(rc 0,PWD 不变,OLDPWD 照常绑定);缺失才报 "HOME not set"。
  • comsub $(cd && pwd) 快路:置空 HOME 委托真实 cd/pwd 执行(None 短路退出),只有缺失保留快路 "HOME not set" 裁定——快路不再可能与内建漂移。
  • 测试 operandless_cd_refuses_missing_home_only、comsub_cd_fast_path_matches_builtin_home_verdict 按 (a) 改期望(空 → rc 0 / out==$PWD;缺失 → rc 1 / "HOME not set")。

N3 — 同族一并修:pushd -n 现按 pushd.def:301 完整语义推原词(add_dirstack_element (savestring (list->word->word))):空词原样入栈、无规范化、无存在性探测(探测本就只在 cd 路径上);load_stack 不再滤掉空元素——空串表示清栈(dirs -c 回退 cwd 的语义保留),非空串里的空元素是真实栈成员,HOME=; pushd -n ~; dirs 实测 <cwd> ""(尾随空元素)与 GNU 一致,测试 empty_home_pushd_tilde_is_null_directory 钉死栈形。

N1 — 引用更正:popd 的归属改为 builtins/pushd.def:318 popd_builtin()(空词 break 在 :356-363);代码注释、测试头注、CHANGELOG、commit message 四处同步。commit message 的误读段落已随 force-push 重写。

测试来源校准:issue754 全部用例本就走 CARGO_BIN_EXE_rubash(源码构建产物);首轮报告里出现安装位行为的只有我手跑的证据表,PR 描述已改为仅源码构建列,并注明 #827。

门禁重跑:cargo test --no-fail-fast 297 目标——本分支 41 失败 vs master 43,分支侧 0 新增;master 多出的 brace_group_consumer_terminates、sigquit_stays_ignored_after_trap_reset 为负载抖动(两测试在分支上按各自目标单独重跑均通过,与本改动无关的 writer/signal 时序)。稳定核心 41 例两轮完全一致。cargo fmt --check 干净。

(附注:E7/E8 行在上一版表里的错误数据源于我把缺失形态的实测外推到了置空形态——修正后的表在 PR 描述里逐行标注了实测来源。)

…-empty (rubash#754)

The #754 path-model family: tilde expansion and the cd/pushd/popd path
model must share ONE $HOME baseline — the CURRENT variable cell at every
evaluation, with GNU's missing-vs-empty distinction, never a startup
capture and never a silent fallback.

GNU specification (third_party/bash, bash.git @b4608166, 5.3 patch 15):
- lib/tilde/tilde.c:344-356 tilde_expand_word(): a leading `~`/`~/` is
  ALWAYS translated to sh_get_env_value("HOME") read at expansion time
  (variables.c get_string_value, the live cell). A set-but-EMPTY value is
  still a value — glue_prefix_and_suffix (tilde.c:304-317) then yields the
  bare suffix, so `HOME=; cd ~` is `cd ""` and reports "null directory"
  (builtins/cd.def:331-336). Only a MISSING variable reaches the fallback:
  tilde.c:349-350 APPDATA on _WIN32, tilde.c:354-356 sh_get_home_dir
  (variables.c:736 getpwuid — fixed for the uid, never tracks $HOME).
  Rubash's stand-in for that fallback on Windows stays USERPROFILE.
- builtins/cd.def:316-322: the operand-less refusal is `dirname == 0`
  (POSIX cd step 1) — a MISSING variable only, reported as "HOME not set".
  A set-but-EMPTY HOME flows on through make_absolute, glues the cwd onto
  the empty name, and the chdir is a same-directory success: rc 0, no
  diagnostic, PWD unchanged. An empty OPERAND still trips cd.def:331-336's
  "null directory" — that is the `cd ~` path, not this one.
- builtins/pushd.def:296-302: the Dir operand delegates to cd_builtin, so
  an empty expansion inherits "null directory" (prolog names pushd).
  pushd.def:301: with -n, cd_builtin is never called and
  add_dirstack_element(savestring(list->word->word)) pushes the RAW
  operand word unconditionally — no normalization, no existence probe; an
  empty expansion stays "" on the stack (`HOME=; pushd -n ~` yields
  `<cwd> ""`). popd lives in the same file: builtins/pushd.def:318
  popd_builtin() breaks its option walk on an empty word
  (pushd.def:356-363) — the default top-of-stack pop.

Verified against WSL GNU bash 5.3.0(1)-release (stdin method), HOME-change
matrix T01-T20 (cd/cd ~/mkdir ~/x pair/cd no-arg/subshell/command cd/pushd/
assignment/redirect/[[ ]]/case/export/relative HOME/sequential changes/
cd --/eval'd tilde/functions/PATH-style assignment): master already matches
GNU on the current-$HOME half; the installed niu 1.4.0 binary that the
issue verified against carries a stale startup-HOME capture that no commit
in v1.4.0..master contains (built from source at v1.4.0 and e785c3c, the
repro passes in both) — provenance forensics moved to #827.

Fixed here, every case pinned against the oracle:
- tilde home_value treated a set-but-empty HOME as UNSET and fell through
  to USERPROFILE (`HOME=; cd ~` succeeded into USERPROFILE while bare
  `cd` refused — two operations, two baselines). The presence of the
  variable now decides; empty expands to ""/"/x" exactly as GNU glues it.
- the operand-less cd refused set-but-empty HOME with "HOME not set";
  per cd.def:316-322 only a MISSING variable refuses now — an empty HOME
  re-chdirs into the cwd (rc 0, silent).
- the $(cd && pwd) fast path used the tilde fallback for the operand-less
  cd; the empty-HOME case now delegates to the real cd/pwd pair and only
  the missing case keeps the fast "HOME not set" verdict.
- pushd with an empty expansion was a silent no-op success; it now reports
  "pushd: null directory" (rc 1). pushd -n pushes the raw operand word
  without normalize/existence-probe, and load_stack preserves blank stack
  members across the round trip (GNU's `<cwd> ""`); popd treats an empty
  word as the argument terminator.

tests/issue754_tilde_home_baseline_regressions.rs pins the differential
catalogue (12 cases: the mkdir~/x + cd~/x pair in -c and stdin forms,
sequential reassignments, every tilde context, the empty-HOME battery,
both operand-less verdicts, both comsub verdicts, the pushd -n stack
shape, the Windows USERPROFILE persona).
@caomengxuan666

Copy link
Copy Markdown
Member Author

补记:回复发出后 master 又进了 #818/#820/#822(3c611299),已再次 rebase(冲突仅 CHANGELOG 同位条目,#806 与 #754 两条共存,#806 在前),force-push 为 2d8dc033。rebase 后复验:tests/issue754_tilde_home_baseline_regressions 12/12、build、cargo fmt --check 均绿;门禁数字(0 回归;2 个 delta 为负载抖动)不受影响——新增的上游提交不触及 tilde/cd/pushd/comsub 路径,冲突文件仅 CHANGELOG。

@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.

Independent CR re-review of 2d8dc03 (base 3c61129): APPROVE-WITH-NITS

Everything re-verified on an independent worktree against the vendored bash (b4608166) and live WSL GNU bash 5.3.0(1)-release (stdin + native -c forms). All three round-1 findings are resolved; one comment citation survives (N2 below), plus one non-blocking follow-up observation.

F1 — resolved, option (a), GNU-exact

  • src/builtins/cd.rs:333-341: operand-less cd now refuses only a MISSING HOME; set-but-empty HOME → env::current_dir() target. Runtime battery (stdin, isolated fresh dirs), rubash vs GNU, all equal: HOME=; cd → rc 0, silent, PWD value AND POSIX spelling unchanged, OLDPWD rebound to the old PWD (GNU old=$PWD same value — checked); cd "" → "null directory" rc 1 (separate arm, cd.def:331-336, correctly not conflated); unset HOME; cd → "HOME not set" rc 1; unset HOME; cd ~ → rc 0 (USERPROFILE persona vs GNU /root — pinned persona). No native-path spelling leak from env::current_dir().
  • src/executor/command_substitution.rs:1108-1122: empty HOME → return None, which falls through the dispatch (command_substitution.rs:763 if let Some(...)) to the real parser/executor — the shortcut structurally cannot drift from the builtin for that case; missing HOME keeps the fast verdict. comsub_cd_fast_path_matches_builtin_home_verdict pins both forms (empty → out == $PWD, rc 0; missing → "HOME not set", status 1).

N3 — resolved

pushd -n pushes the raw word (dir.clone(), no normalize/probe) per pushd.def:301 savestring, and load_stack no longer filters blank elements (only the fully-emptied dirs -c representation falls back to cwd). Runtime parity: pushd -n ./sub → dirs/DIRSTACK <cwd> ./sub byte-equal to GNU; HOME=; pushd -n ~ → <cwd> "" blank tail, pinned by empty_home_pushd_tilde_is_null_directory (stack=blank-tail).

N1 — resolved

popd attribution now pushd.def:318 popd_builtin() with the empty-word break at pushd.def:356-363, synced across pushd/parse.rs, the test header, and CHANGELOG (PR body updated in the linked comment). #827 exists and carries the release-integrity escalation; tests use CARGO_BIN_EXE_rubash (source-built) with the installed binary marked unverifiable provenance.

Gates (self-run)

  • issue754: 12/12 pass on 2d8dc03.
  • Full suite (cargo test --no-fail-fast): 45 failures = the 43 stable names identical to master c6f4a9b (empty diff both directions) + 2 load flakes (windows_listing_cache_finds_files_that_appear_but_not_ghosts, interactive_session_survives_parse_error), both passing in isolation; last round's sigquit flakes did not recur. cargo fmt --check clean.
  • Hygiene: 2 commits, 8 files, all in-scope; src/expand/tilde/tilde.rs byte-identical to the round-1 commit.

N2 (remaining nit, comment-only)

src/expand/tilde/tilde.rs:17 still cites "builtins/cd.def:338" for the null-directory verdict — that is cd.def:333-336 (builtin_error at :335; :338 is the cd - branch). One-line follow-up, non-blocking.

Follow-up observation (pre-existing, not this PR)

popd's default pop mutates the stack and rewrites PWD from stack[0] but never chdirs (that flow is byte-identical on master c6f4a9b:230/252/262 and untouched here). With the now-correct raw relative -n entries this surfaces as: pushd -n ./sub; popd → GNU cd's into ./sub (PWD canonical, relative ops land in sub); rubash sets PWD=./sub while pwd/process cwd stay in the parent. Stack mutation matches GNU; the cd semantics are the old popd arm's hole — worth its own issue, does not block this PR.

Verdict: APPROVE-WITH-NITS (N2 one-liner can ride any later commit; nothing else blocks).

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.

1 participant