Skip to content

fix(shim): forward the sh invocation name so the host honors its GNU contract - #504

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/sh-invoked-as
Oct 12, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/sh-invoked-as

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Problem

The installed sh.exe is the same forwarder binary as bash.exe copied under a different name (niubash's scripts/package-release.ps1 Resolve-RubashShim). Windows argv[0] therefore carries no contract: sh -c 'set -o | grep -w posix' reported off and niu was niu, while the engine (apply_invocation_shell_mode, src/main.rs) already implements GNU shell.c:733-737 for argv[0] = sh/ash.

Fix

Invoked as sh/ash, the shim forwards its argv[0] stem (leading - preserved, as GNU set_shell_name sees it) to the host shell in NIU_INVOKED_AS; niu's Shell::new applies the same rules (POSIX mode on, niu = sh, login marker). Every other name actively scrubs the variable instead of forwarding it: an inherited value from an outer sh run must not switch a nested sh -c 'bash -c ...' into POSIX mode, since bash.c keys the contract off argv[0], which this forward erases. A plain bash.exe forward stays byte-for-byte what it always was.

Companion niubash PR: unixwin/niubash#262 (consumes NIU_INVOKED_AS in Shell::new, consumes-on-read so children stay clean).

Acceptance (installed tree, Windows)

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 (unchanged)
bash -c 'echo $0' niu (unchanged)
bash -c 'exit 3' rc 3 (unchanged)
nested sh -c 'bash -c ...' off, $0=bash (GNU-correct)
nested sh -c 'sh -c ...' on, $0=sh (GNU-correct)
nested sh -c 'niu -c ...' off, $0=niu (GNU-correct)
NIU_INVOKED_AS leak into the shell env none (consumed on read)

Unit tests: src/bin/bash.rs forwarded_invocation_name (sh/ash forward, login marker, bash/rbash/no-argv0 scrub cases). cargo fmt + cargo build --release verified.

…contract

The shim binary is installed under every name (bash.exe, sh.exe), so
Windows argv[0] cannot carry GNU shell.c:733-737's contract through
Command::new — the child sees the resolved host path and sh -c runs in
the default bash-compatible mode with $0 = niu.

Invoked as sh/ash, the shim now forwards its argv[0] stem (leading '-'
preserved, as set_shell_name sees it) in NIU_INVOKED_AS; niu's
Shell::new applies the same rules (POSIX mode on, $0 = sh). Every
other name actively scrubs the variable instead of forwarding it: an
inherited value from an outer sh run must not switch a nested
'sh -c 'bash -c ...'' into POSIX mode, since bash.c keys the contract
off argv[0], which this forward erases. A plain bash.exe forward
stays byte-for-byte what it always was.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Interlink: the engine-side posix-mode depth this invocation contract switches on is now audited end-to-end in #559 (75-sub-item GNU posix-mode matrix, 42 implemented) — rebased on current master and its failure set is a strict subset of a fresh master run.

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