Skip to content

perf(winuxcmd): find the installation from any entry point, and inject its real root - #393

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/winuxcmd-discovery-ancestors
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/winuxcmd-discovery-ancestors

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

The hidden blocker behind #377, found while planning the bash.exe/sh.exe hardlink and fixed on its own so it can land independently.

Why this is needed

Discovery today only looks beside the running executable (crates/niubash-runtime/src/winuxcmd.rs). That worked while niu.exe always sat in the installation root. The moment the shell is reachable as a hardlink named bash.exe/sh.exe in the PATH entry directories — <root>\bin (where wpm puts the entry links), and for agents/editors one or two levels beside the installation (…\Niubash\bin, …\winuxcmd\opt\niugit\bin) — nothing beside that executable is a WinuxCmd tree, so discovery fails, PATH injection never runs and every coreutil disappears inside that shell. That is why "just hardlink it" breaks, and why the shim exists.

What changes

  • bundled_winuxcmd_candidates walks up to six ancestor levels, keeping the existing relative shapes first and adding <base>\bin\winuxcmd.exe (the PATH entry link wpm creates).
  • Each candidate now carries the installation root its shape implies instead of the root being re-derived from the matched path afterwards. That also fixes a live bug in the current layout: from <root>\bin\bash.exe the sibling candidate <root>\bin\winuxcmd.exe matches (wpm creates it for the winuxcmd package), and installation_root then reported bin as the root, injecting only bin into PATH.
    • <root>\usr\bin\winuxcmd.exe, <root>\winuxcmd.exe, <root>\bin\winuxcmd.exe ⇒ <root>
    • <x>\winuxcmd\usr\bin\winuxcmd.exe ⇒ <x>\winuxcmd
    • no filesystem probing: the candidate's shape knows the answer.
  • injected_path_dirs(root, exe) replaces the exe_dir.ends_with("usr/bin") test: a tree whose executable sits below the root injects usr/local/bin, usr/bin, bin and the root in POSIX order; a portable layout that keeps winuxcmd.exe beside its tools still injects only that directory.
  • The root is resolved once and threaded through prepare_winuxcmd_with_override / ensure_on_path_with_override, instead of being recomputed from the path in three places.

Verification

cargo test -p niubash-runtime --lib winuxcmd::tests → 18/18 (16 pre-existing tests unchanged, no regressions). The two new ones:

  • bundled_candidates_climb_to_ancestor_installations — from <root>\bin, from a payload at <root>\winuxcmd\opt\niugit\bin, and from <x>\bin (the niubash layout), the candidate that matches is the canonical usr\bin\winuxcmd.exe and its implied root is the tree, not bin.
  • injected_path_dirs_cover_the_installation_root — the canonical location, the PATH entry link and a package copy deeper in the tree all inject the same four directories; the portable layout injects only its own.

No behaviour change for any layout that works today (the relative shapes stay first, and the usr/bin case still injects the same four directories).

Next steps (not in this PR)

With discovery fixed, the shim can be replaced by a hardlink to niu.exe for bash.exe/sh.exe; the remaining pieces are the packaging change plus re-linking after an update replaces niu.exe (the same maintenance step wpm links rebuild performs for package links).

@caomengxuan666
caomengxuan666 force-pushed the perf/winuxcmd-discovery-ancestors branch from eedbbf3 to 1818a9e Compare October 17, 2026 03:37
…t its real root

The discovery search only looked beside the running executable. That was
enough while `niu.exe` always lived in the installation root, but the shell is
about to be reachable as a hardlink named `bash.exe`/`sh.exe` in the PATH entry
directories (`<root>\bin`, and for agents and editors one or two levels
beside the installation). From those, nothing beside the executable is a
WinuxCmd tree, so discovery failed, PATH injection never happened and every
coreutil disappeared inside that shell - the hidden blocker behind niubash#377.

- `bundled_winuxcmd_candidates` now walks up to six ancestor levels from the
  executable, keeping the existing relative shapes first and adding
  `<base>\bin\winuxcmd.exe` (the PATH entry link wpm creates).
- Each candidate carries the installation root its shape implies instead of
  re-deriving it later from the path. That fixes a real bug on the current
  layout: from `<root>\bin\bash.exe` the sibling candidate `<root>\bin\
  winuxcmd.exe` matched, and `installation_root` then reported `bin` as the
  root, which injected only `bin` into PATH. `<root>\bin\winuxcmd.exe`,
  `<root>\usr\bin\winuxcmd.exe` and `<root>\winuxcmd.exe` all imply `<root>`;
  `<x>\winuxcmd\usr\bin\winuxcmd.exe` implies `<x>\winuxcmd`.
- `injected_path_dirs(root, exe)` replaces the `ends_with("usr/bin")` test:
  a tree whose executable sits below the root injects `usr/local/bin`,
  `usr/bin`, `bin` and the root in POSIX order, while a portable layout that
  keeps `winuxcmd.exe` beside its tools still injects only that directory.
- The install root is resolved once and threaded through prepare/ensure instead
  of being recomputed from the path.

Tests: 18/18 in `winuxcmd::tests` (16 existing, no regressions; two new ones
cover the ancestor walk with implied roots and the injected directory set for
the canonical location, the PATH entry link and a payload deeper in the tree).
@caomengxuan666
caomengxuan666 force-pushed the perf/winuxcmd-discovery-ancestors branch from 1818a9e to 96a7a52 Compare October 17, 2026 03:43
@caomengxuan666
caomengxuan666 merged commit 4a05383 into master Oct 17, 2026
9 checks passed
@caomengxuan666
caomengxuan666 deleted the perf/winuxcmd-discovery-ancestors branch October 17, 2026 04:19
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