Repository navigation
fix(sh): honor the sh invocation contract through the shim forward - #262
Conversation
The installed sh.exe is the same 226KB forwarder binary as bash.exe (scripts/package-release.ps1 Resolve-RubashShim), so Windows argv[0] could not carry the GNU invocation contract to niu: 'sh -c 'set -o | grep -w posix'' reported off and $0 was niu. Forward the invocation name the way the platform can carry it: the rubash shim (src/bin/bash.rs) sends its argv[0] stem as NIU_INVOKED_AS when invoked as sh/ash, and Shell::new maps it to $0 and the POSIX-mode contract GNU shell.c:733-737 applies for those names (leading '-' marks a login shell). The variable describes one process's invocation, so it is consumed on read in niu and actively scrubbed by the shim on every other name: a nested 'sh -c 'niu ...'' or 'sh -c 'bash -c ...'' keeps its own argv[0]-keyed contract, and a plain bash.exe forward behaves byte-for-byte as before. End state: sh -c enters POSIX mode and shows $0 = sh; bash -c stays in the default bash-compatible mode with $0 unchanged.
|
Interlink: the engine-side posix-mode behavior this shim contract switches on is audited in unixwin/rubash#559 (75-sub-item GNU posix-mode matrix, 42 implemented); the artifact-level acceptance table above stays the release-time deployment path. |
Conflict: crates/niubash-runtime/src/shell.rs — master�s #270 one-shot fast path gave new_with_script_name a one_shot parameter and a hard-coded niu name in new_one_shot. Resolution carries the shim-forwarded invocation name through BOTH construction paths: the shim contract (GNU shell.c set_shell_name) keys off the invocation name, not off interactivity, so new_one_shot now uses default_script_name() too - sh stays on the #270 fast path. Verified: invocation_name unit tests (8) green; cargo fmt --all --check clean; artifact-level e2e battery 11/11 (sh posix on / $0=sh / bash byte-identical / nested sh->bash, sh->sh, sh->niu / NIU_INVOKED_AS consume-on-read / one-shot $0=sh).
|
Update-branch pushed (6280358, merge of origin/master through c3df161). CI on the merge commit shows exactly one failure: Not from this branch — that same test fails identically on master's own push CI, which ran hours before this merge: run 38196596891 (2026-10-12T02:41, same assertion, same left/right values). It is the #265 session-end history write interacting with the This branch's own surface is verified on the merge commit: |
…1.4.0 section (#326) - The [Unreleased] entries (#284/#287, #293-#304, #313) move into [1.4.0]; [Unreleased] is empty again. - Newly recorded (today's wave): Git Bash compatible factory default prompt (#292, headline), WT-form shortcuts (#290/#211), the paste burst trailing-Enter fix (#308/#291), self-update SHA256 + release pinning (#311/#312), managed-block convergence (#324), completion tables/NIU_COMPDEFS/NIU_ENV probe (#305-#307), -s on-demand stdin (rubash#586 host half), bash/rbash invocation faces (#321/#322). - Older gaps recorded: closed-stdout pipe contract (#245/#286), history session-end write-back (#265), sh POSIX contract (#262), PWD persona alignment (#283), one-shot fast path (#270), --cwd-state (#272), --encoded-command (#271), REPL-command highlighting (#281), docs passes (#255/#225/#226/#227), MSRV (#267), easter-egg prune (#264). CHANGELOG-only change; no version claims.
Problem
The installed
sh.exe(andusr/bin/sh.exe) is the 226KB forwarder stub copied from the rubash shim, so Windowsargv[0]never reaches niu assh:sh -c 'set -o | grep -w posix'reported off andniuwasniu. The embedded engine already implements the GNU contract (rubashapply_invocation_shell_mode, shell.c:733-737) but nothing fed it the invocation name.Fix (product side of the rubash shim PR)
Shell::newmaps the shim-forwardedNIU_INVOKED_ASstem to the GNUset_shell_namesemantics:niu=sh(or-shlogin-marked), POSIX mode on forsh/ash, leading-marks a login shell. Plain niu and the bash shim keep the historicalniudefault byte-for-byte.Shell::newconsumes it on read (removed from the process environment): a nestedsh -c 'niu ...'keeps its own argv[0]-keyed contract instead of inheriting POSIX mode.--stripped name drives the POSIX contract (GNU set_shell_name); the marker sets the login-shell bridge.scripts/package-release.ps1: the TODO(posix-mode) comment now documents that the shim self-identifies throughargv[0]— same bytes asbash.exeforward no name and stay bash-compatible.Companion rubash PR: unixwin/rubash#504 (the shim sets
NIU_INVOKED_ASfor sh/ash and scrubs it for every other name).Acceptance (installed tree, Windows, live probes)
sh -c 'set -o | grep -w posix'posix onsh -c 'echo $0'shbash -c 'set -o | grep -w posix'posix off— byte-identical to pre-fix baselinebash -c 'echo $0'niu— unchangedbash -c 'exit 3'sh -c 'bash -c ...'sh -c 'sh -c ...'sh -c 'niu -c ...'Tests:
crates/niubash-runtime/src/shell.rsinvocation_name_tests(4 cases incl. consume-on-read).cargo fmt,cargo build --release, targetedcargo test -p niubash-runtime --lib invocation_namegreen.