Repository navigation
perf(shell): relink bash.exe and sh.exe as hardlinks to niu.exe - #397
Merged
Merged
Conversation
The shell has been reached through a 243 KB forwarding shim that sets `NIU_INVOKED_AS` and re-executes `niu.exe`, which costs one extra process on every `bash -c` / `sh -c` - measured at +12.8 ms per call, and the per-command tax AI agents, CI and git hooks pay constantly (#377). The same faces are free as hardlinks: the engine already selects `$0`, `BASH_VERSION` and the POSIX mode from `argv[0]`, and discovery now finds the installation from any entry point, so a name in `<root>\bin` is enough. - `niubash_runtime::shell_links` creates `bash.exe` and `sh.exe` as hardlinks to the running `niu.exe` in `<root>\bin`, `<root>\winuxcmd\bin` and `<root>\winuxcmd\usr\bin`, whichever of those exist. `find_installation_root` walks up to the directory that holds `niu.exe` next to the bundled winuxcmd tree, so the same root is found whether the shell was reached as `niu.exe`, as `<root>\bin\bash.exe` or from inside the winuxcmd tree. - It is idempotent, and an existing name that is not this `niu.exe` (the shipped shim, or a link left on the previous inode after an update replaced `niu.exe`) is kept unless `--force` is given. With `--force` the name is parked first, linked, and only then dropped - a shell running right now keeps its bytes (the technique from unixwin/WinuxCmd#1194). - Exposed as `niu --relink-shells [--force] [--quiet]`, and called from the installer's `[Run]` right after `wpm links rebuild`, so an update that replaces `niu.exe` refreshes the names instead of leaving them stale. - Hardlinks are created on the user's machine, never in the distribution: a zip would store the 12 MB payload once per name. Portable layouts and filesystems without hardlinks keep the shipped shim, which produces the same identity through `NIU_INVOKED_AS` - both paths stay supported. - `docs/src/installer.md` now documents that contract instead of forbidding the `argv[0]` route. Measured in a sandbox with six fake shims: `--relink-shells --force` replaced all six with links to `niu.exe` (no leftovers), a second run reports `0 created, 6 already current`, and `bash -c exit` costs the same as `niu.exe -c exit`. `sh` still selects POSIX mode and `bash` does not.
…aming the shell Two things a hardlinked `bash.exe`/`sh.exe` makes visible, both fixed here. **Product verbs no longer leak through a shell face.** `bash setup` used to open Niubash's setup wizard, because the subcommand table was matched under every face. GNU bash reads the first non-option word as the script to run, so `bash setup` runs a file named `setup` and reports ENOENT (127) when there is none - verified both ways, with `niu setup` unchanged. The face comes from one helper (`shell_face_name`), which the launcher-word dispatch already needed for `--help`/`--version`. **The shell-path contract names the shell, not the face.** Invoked through a hardlink, `current_exe()` is `...\bash.exe`, so `NIU_SHELL` and `WINUXSH_SHELL` used to follow the face; rubash's shim target search and the tooling that reads them want the real binary, so they now come from `shell_links::resolved_shell_path` (the installation root's `niu.exe`, falling back to the invoked path). `$BASH`/`$SHELL` deliberately still follow the invoked name, which is the GNU reading - `$BASH` names the shell that is running, so `bash` sees `...\bash.exe`. Tests: `tests/host_contract.rs` gains `shell_faces_do_not_expose_product_verbs_and_name_the_real_shell` (ENOENT surface, the script actually running, the environment contract, and the product verbs still present on the niu face). `cargo test --test host_contract` 48 passed, `cargo test -p niubash-runtime --lib` 538 passed.
`relink_shell_faces` was inserted between a `#[cfg(windows)]` attribute and the function it belonged to, so the attribute attached to the new function and the Windows terminal-profile implementation lost its gate. On unix hosts that made the windows-only code compile (references to `shortcut::plan_for_installation` and friends) and left `relink_shell_faces` itself unqualified, which is what the cross-target jobs reported: error[E0428]: the name `install_windows_terminal_shortcuts` is defined multiple times error[E0425]: cannot find function `relink_shell_faces` in this scope The helper now lives at the end of the file, where it cannot steal another function's attributes, and the cfg attribute sits immediately above its own implementation again. The link-planning test builds its expectations with `join` instead of Windows literals so it does not depend on the separator. Verified locally against the same target the CI cross jobs use: `cargo check --tests --target x86_64-unknown-linux-gnu` and the Windows `cargo test -p niubash-runtime` shell_links suite both pass.
caomengxuan666
force-pushed
the
perf/hardlink-shell-faces
branch
from
October 17, 2026 04:28
14167a4 to
771acf8
Compare
caomengxuan666
added a commit
to unixwin/rubash
that referenced
this pull request
Oct 18, 2026
…n console (#751) `suppress_child_console_window` (niubash#379) chose CREATE_NO_WINDOW so a GUI parent spawning the shim with piped stdio does not make Windows allocate a *visible* console for the niu.exe child. That flag still allocates a console - a hidden one, with conhost.exe - on every invocation in exactly the case agents, CI and git hooks hit (all three streams redirected). With all three streams redirected the child has no console to inherit and does not need one, so DETACHED_PROCESS gives it that for free, and it keeps the anti-flash property: a console-subsystem child started with DETACHED_PROCESS gets no automatic console either. The gate is unchanged (all three streams non-console; an interactive session keeps the real console, as before), and so is cancellation: neither flag lets the child receive the parent's Ctrl+C, since a CREATE_NO_WINDOW child has a console of its own rather than the parent's. Measured here (median of 25, interleaved, all stdio redirected, NIU_SHELL pointed at hostname.exe so only the shim is timed): 22.9 ms -> 21.8 ms. The latency win is small - an earlier report credited this flag change with ~22 ms, which did *not* reproduce on this machine; conhost is evidently shared or warm. The real cost of a forwarding hop is the extra process, and the durable fix for `bash`/`sh` is a hardlink to the shell (unixwin/niubash#397), which leaves this shim as the fallback for layouts without hardlink support. What this change buys is that the fallback no longer allocates a console per invocation. Verified: piped stdin/stdout round-trip, exit-code passthrough (`exit 7` -> 7), face selection unchanged (`$0` = bash, POSIX off; sh.exe -> POSIX on), and no conhost process for the duration of a 2 s child (only niu.exe).
This was referenced Oct 18, 2026
Closed
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.
Supersedes #394, which GitHub closed when its base branch (#393's) was deleted on merge. Rebased onto master, where #393 now is — discovery has to work from a hardlinked entry name before the shim can be replaced.
The problem
bash.exe/sh.exeare currently a 243 KB forwarding shim (rubash'sbashbin, staged byscripts/package-release.ps1) that setsNIU_INVOKED_ASand re-executesniu.exe. That is one extra process on everybash -c/sh -cinvocation — measured at +12.8 ms per call on this machine (49.6 ms through the shim vs 36.8 ms forniu.exeitself) — and it is exactly the per-command tax AI agents, CI and git hooks pay (#377).The same faces are free as hardlinks: the engine already selects
$0,BASH_VERSIONand POSIX mode fromargv[0], and with #393 discovery finds the installation from any entry point.What this adds
niubash_runtime::shell_links— createsbash.exeandsh.exeas hardlinks to the runningniu.exein<root>\bin,<root>\winuxcmd\binand<root>\winuxcmd\usr\bin, whichever of those exist.find_installation_rootwalks up to the directory holdingniu.exenext to the bundledwinuxcmdtree, so the same root is found whether the shell was reached asniu.exe, as<root>\bin\bash.exe, or from inside the winuxcmd tree (and it falls back to the executable's own directory for a portable layout).niu.exe(the shipped shim, or a link left on the previous inode after an update replacedniu.exe) is kept unless--force. With--forceit is parked first, linked, and only then dropped, so a shell running right now keeps its bytes — the technique from fix(wpm): replace held payload files by parking them, not by refusing WinuxCmd#1194.niu --relink-shells [--force] [--quiet], called from the installer's[Run]right afterwpm links rebuild— so an update that replacesniu.exerefreshes the names instead of leaving them stale.NIU_INVOKED_AS. Both paths stay supported deliberately.docs/src/installer.mddocuments that contract instead of forbidding theargv[0]route.Verification
Sandbox with six fake forwarding shims and a built
niu.exe:$0,BASH_VERSION=5.3.0(1)-releaseandset -o | grep -w posixbehave the same as before:bash→ posix off,sh→ posix on.bash -c exitcosts 38–41 ms (debug build; the same asniu.exe, versus +12.8 ms through the shim).cargo test -p niubash-runtime --lib→ 538 passed, including four newshell_linkstests (planned links, root discovery from a hardlinked name, idempotence/stale refresh/parking, missing directories).Face semantics (second commit)
A hardlinked
bash.exemakes two pre-existing leaks visible, both fixed:bash setupused to open Niubash's setup wizard because the subcommand table was matched under every face. GNU bash reads the first non-option word as the script to run, sobash setupnow runs a file namedsetupand reports ENOENT (127) when there is none — verified both ways, withniu setupunchanged. The face comes from one helper (shell_face_name), which the launcher-word dispatch already needed for--help/--version.current_exe()is…\bash.exe, soNIU_SHELL/WINUXSH_SHELLfollowed the face; they now come fromshell_links::resolved_shell_path(the installation root'sniu.exe).$BASH/$SHELLdeliberately still follow the invoked name — the GNU reading, where$BASHnames the shell that is running.Measured through the sandbox hardlinks:
NIU_SHELL=<root>\niu.exe,BASH=<root>/bin/bash.exe,bash setup→ rc 127 "No such file or directory",bash setupwith a script of that name → runs the script,niu setup -h→ unchanged,bash --version→ GNU banner,sh -c 'set -o | grep posix'→ on.Not in this PR
C:\Program Files\Git\bin\bash.exeslot (a 5 KB .NET launcher that currently costs 91 ms because it starts the CLR and then the shim) is being analysed separately; the same--relink-shellsmachinery is what that fix needs.bashshim upstream is its own decision (separate release train); as long as some layouts still ship it, the shim stays the fallback.