Skip to content

fix(sh): honor the sh invocation contract through the shim forward - #262

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/sh-shim-posix-forward
Oct 12, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/sh-shim-posix-forward

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem

The installed sh.exe (and usr/bin/sh.exe) is the 226KB forwarder stub copied from the rubash shim, so Windows argv[0] never reaches niu as sh: sh -c 'set -o | grep -w posix' reported off and niu was niu. The embedded engine already implements the GNU contract (rubash apply_invocation_shell_mode, shell.c:733-737) but nothing fed it the invocation name.

Fix (product side of the rubash shim PR)

  • Shell::new maps the shim-forwarded NIU_INVOKED_AS stem to the GNU set_shell_name semantics: niu = sh (or -sh login-marked), POSIX mode on for sh/ash, leading - marks a login shell. Plain niu and the bash shim keep the historical niu default byte-for-byte.
  • The variable describes one process's invocation, so Shell::new consumes it on read (removed from the process environment): a nested sh -c 'niu ...' keeps its own argv[0]-keyed contract instead of inheriting POSIX mode.
  • Login-marker handling: the --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 through argv[0] — same bytes as bash.exe forward no name and stay bash-compatible.

Companion rubash PR: unixwin/rubash#504 (the shim sets NIU_INVOKED_AS for sh/ash and scrubs it for every other name).

Acceptance (installed tree, Windows, live probes)

probe result
sh -c 'set -o | grep -w posix' posix on
sh -c 'echo $0' sh
bash -c 'set -o | grep -w posix' posix off — byte-identical to pre-fix baseline
bash -c 'echo $0' niu — unchanged
bash -c 'exit 3' rc 3 — unchanged
nested sh -c 'bash -c ...' off, $0=bash
nested sh -c 'sh -c ...' on, $0=sh
nested sh -c 'niu -c ...' off, $0=niu

Tests: crates/niubash-runtime/src/shell.rs invocation_name_tests (4 cases incl. consume-on-read). cargo fmt, cargo build --release, targeted cargo test -p niubash-runtime --lib invocation_name green.

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

Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
Member Author

Update-branch pushed (6280358, merge of origin/master through c3df161). CI on the merge commit shows exactly one failure: history_and_fc_use_the_host_history_provider (history -c leaves the host file as "first command\nthird command\n" instead of empty).

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 history -c expectation in tests/host_contract.rs:89. Master-side triage needed: either the session-end write should skip when the in-memory history was cleared by history -c, or the test's expectation needs updating to the #265 contract.

This branch's own surface is verified on the merge commit: invocation_name unit tests 8/8, cargo fmt --all --check clean, and the artifact-level e2e battery 11/11 (sh posix on / $0=sh / bash byte-identical: posix off, $0=niu, rc passthrough / nested sh→bash, sh→sh, sh→niu / NIU_INVOKED_AS consume-on-read / one-shot sh -c $0=sh — the one-shot path carries the shim name through #270's new_one_shot, which was the merge conflict's substance).

@caomengxuan666
caomengxuan666 merged commit 2a6a12a into master Oct 12, 2026
6 of 7 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/sh-shim-posix-forward branch October 13, 2026 02:06
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
…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.
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