Repository navigation
fix(shim): forward the sh invocation name so the host honors its GNU contract - #504
Merged
Merged
Conversation
…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.
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. |
This was referenced Oct 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The installed
sh.exeis the same forwarder binary asbash.execopied under a different name (niubash'sscripts/package-release.ps1Resolve-RubashShim). Windowsargv[0]therefore carries no contract:sh -c 'set -o | grep -w posix'reported off andniuwasniu, while the engine (apply_invocation_shell_mode, src/main.rs) already implements GNU shell.c:733-737 forargv[0]=sh/ash.Fix
Invoked as
sh/ash, the shim forwards itsargv[0]stem (leading-preserved, as GNUset_shell_namesees it) to the host shell inNIU_INVOKED_AS; niu'sShell::newapplies 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 outershrun must not switch a nestedsh -c 'bash -c ...'into POSIX mode, since bash.c keys the contract offargv[0], which this forward erases. A plainbash.exeforward stays byte-for-byte what it always was.Companion niubash PR: unixwin/niubash#262 (consumes
NIU_INVOKED_ASinShell::new, consumes-on-read so children stay clean).Acceptance (installed tree, Windows)
sh -c 'set -o | grep -w posix'posix onsh -c 'echo $0'shbash -c 'set -o | grep -w posix'posix off(unchanged)bash -c 'echo $0'niu(unchanged)bash -c 'exit 3'sh -c 'bash -c ...'sh -c 'sh -c ...'sh -c 'niu -c ...'NIU_INVOKED_ASleak into the shell envUnit tests:
src/bin/bash.rsforwarded_invocation_name(sh/ash forward, login marker, bash/rbash/no-argv0 scrub cases).cargo fmt+cargo build --releaseverified.