Skip to content

The agent that grows with you

fix(pm): skip System32/WindowsApps bash.exe, prefer Program Files Git

ethernet3917f11d7941
diff --git a/pm/shell.py b/pm/shell.py
index f6a495640fd..8c0372505f0 100644
--- a/pm/shell.py
+++ b/pm/shell.py
@@ -8,9 +8,12 @@ of hunting fixed locations.
Resolution order:
1. Windows: the git Package's staged bash (via facts.json) — the store
structurally guarantees it in a bundle; no hunt.
- 2. Provisioned PATH: shutil.which("bash") — the store dirs are on the
+ 2. Windows: conventional Git for Windows under Program Files, then PATH —
+ minus C:\Windows\System32\bash.exe (the WSL launcher stub, first on PATH
+ on most machines; #116818) and WindowsApps\bash.exe (an MSIX alias that
+ only spawns inside its package). See windows_bash_candidates().
+ 3. Provisioned PATH: shutil.which("bash") — the store dirs are on the
process PATH after pm.activate() ran.
- 3. Conventional Git for Windows under Program Files, even with an empty PATH.
4. POSIX fallback table for non-bundle / daemon-launch PATH edge cases
(/usr/bin/bash, /bin/bash, $SHELL, /bin/sh). A systemd/cron-launched
gateway may have a minimal PATH and macOS /bin/bash is not on PATH by
@@ -19,8 +22,10 @@ Resolution order:
from __future__ import annotations
+import ntpath
import os
import shutil
+from collections.abc import Mapping
from pm import paths
from pm.lock import Facts
@@ -52,6 +57,29 @@ def _staged_bash() -> str | None:
return None
+# PATH dirs whose bash.exe is not a shell: System32 holds the WSL launcher
+# stub (prints "no installed distributions", exits 1) and WindowsApps holds
+# MSIX execution aliases that fail with WinError 5 from an arbitrary process.
+_WINDOWS_BASH_STUB_DIRS = ("system32", "windowsapps")
+
+
+def windows_bash_candidates(on_path: str | None, env: Mapping[str, str]) -> list[str]:
+ """Ordered bash.exe candidates for a Windows host, as pure data: the
+ conventional Git for Windows dirs first, then ``on_path`` (the
+ ``shutil.which("bash")`` result) unless it is a stub. Program Files beats
+ PATH because System32 precedes Git on most PATHs (#116818)."""
+ programfiles = env.get("ProgramFiles", r"C:\Program Files")
+ candidates = [
+ ntpath.join(programfiles, "Git", "bin", "bash.exe"),
+ ntpath.join(programfiles, "Git", "usr", "bin", "bash.exe"),
+ ]
+ if on_path:
+ norm = ntpath.normpath(on_path).lower()
+ if not any(stub in norm for stub in _WINDOWS_BASH_STUB_DIRS):
+ candidates.append(on_path)
+ return candidates
+
+
def bash() -> str | None:
"""Resolve the bash binary to use, or None if none is available."""
staged = _staged_bash()
@@ -59,18 +87,12 @@ def bash() -> str | None:
return staged
on_path = shutil.which("bash")
- # An MSIX-packaged bash (WindowsApps alias) only spawns inside its
- # package's context — from an arbitrary process (dev checkout, test
- # child, emulated-python CreateProcess) it fails with WinError 5.
- # Prefer a conventional install when one exists.
- if on_path and "windowsapps" not in on_path.lower():
- return on_path
if os.name == "nt":
- programfiles = os.environ.get("ProgramFiles", r"C:\Program Files")
- for rel in (("Git", "bin", "bash.exe"), ("Git", "usr", "bin", "bash.exe")):
- cand = os.path.join(programfiles, *rel)
- if os.path.isfile(cand):
- return cand
+ return next(
+ (c for c in windows_bash_candidates(on_path, os.environ) if os.path.isfile(c)),
+ None,
+ )
+ if on_path:
return on_path
# POSIX fallbacks for minimal-PATH daemon launches / macOS /bin/bash.
diff --git a/tests/pm/test_shell_candidates.py b/tests/pm/test_shell_candidates.py
new file mode 100644
index 00000000000..889cc67d749
--- /dev/null
+++ b/tests/pm/test_shell_candidates.py
@@ -0,0 +1,24 @@
+"""Windows bash resolution as pure data: Program Files Git beats PATH, and
+the WSL / MSIX stubs never win (#116818). Host-independent — the candidate
+ladder takes the ``which`` result and env as arguments."""
+import pytest
+
+from pm.shell import windows_bash_candidates
+
+PF = r"D:\Progs"
+
+
+ "stub",
+ [r"C:\Windows\System32\bash.exe", r"C:\WINDOWS\system32\bash.exe",
+ r"C:\Users\u\AppData\Local\Microsoft\WindowsApps\bash.exe"],
+)
+def test_system_stub_bash_is_never_a_candidate(stub):
+ assert stub not in windows_bash_candidates(stub, {"ProgramFiles": PF})
+
+
+def test_program_files_git_precedes_the_path_bash():
+ on_path = r"C:\msys64\usr\bin\bash.exe"
+ candidates = windows_bash_candidates(on_path, {"ProgramFiles": PF})
+ assert candidates[0] == PF + r"\Git\bin\bash.exe"
+ assert candidates[-1] == on_path
diff --git a/tools/environments/local.py b/tools/environments/local.py
index e811c09cde7..e3d21de3974 100644
--- a/tools/environments/local.py
+++ b/tools/environments/local.py
@@ -473,34 +473,6 @@ def strip_launch_profile_env(env: dict, target_home: "str | Path | None" = None)
# --- Shell discovery ---
-def _windows_bash_candidates(custom: "str | None") -> list[str]:
- """Ordered bash.exe candidates on Windows: HERMES_GIT_BASH_PATH, our portable Git
- under %LOCALAPPDATA%\\hermes\\git (PortableGit ``bin`` and MinGit ``usr\\bin``),
- known Git-for-Windows dirs, then PATH last — ``shutil.which`` may return WSL's
- bash, which fails silently on Windows paths."""
- getenv = os.environ.get
- lad = getenv("LOCALAPPDATA", "")
- roots = [
- lad and os.path.join(lad, "hermes", "git", "bin"),
- lad and os.path.join(lad, "hermes", "git", "usr", "bin"),
- os.path.join(getenv("ProgramFiles", r"C:\Program Files"), "Git", "bin"),
- os.path.join(getenv("ProgramFiles(x86)", r"C:\Program Files (x86)"), "Git", "bin"),
- lad and os.path.join(lad, "Programs", "Git", "bin"),
- ]
- raw = [custom or "", *(os.path.join(r, "bash.exe") for r in roots if r)]
- candidates = list(dict.fromkeys(c for c in raw if c and os.path.isfile(c)))
- found = shutil.which("bash")
- if found and found not in candidates:
- # Skip WSL/system bash.exe (C:\Windows\System32\bash.exe or
- # WindowsApps bash.exe) — it is a stub launcher, not a usable shell.
- norm = os.path.normpath(found).lower()
- if "system32" in norm or "windowsapps" in norm:
- logger.debug("Skipping WSL/system bash.exe at %s", found)
- else:
- candidates.append(found)
- return candidates
-
-
def _find_bash() -> str:
"""Resolve the shell Hermes runs commands with. Owned by pm (the store
is the authority on bundled bash); this is a thin wrapper over