Resolve the npm directory with dirname so a patched os.name cannot pick the wrong Path flavour

_install_command used Path(npm).parent. pathlib chooses PosixPath or WindowsPath
from os.name at call time, so a caller that overrides os.name (the install tests
set it to posix) builds the flavour the host cannot instantiate, and the call
raised NotImplementedError on Windows. os.path.dirname gives the same answer
without consulting os.name. An empty result now leaves PATH alone rather than
prepending the current directory.
This commit is contained in:
danielhanchen 2026-07-29 08:11:52 +00:00
commit a5c4c07faf

View file

@ -2560,9 +2560,13 @@ def _install_command(install_hint: str) -> tuple[list[str], Optional[dict]]:
)
args = shlex.split(install_hint)
env = dict(os.environ)
npm_dir = str(Path(npm).parent)
# dirname, not Path().parent: Path picks its flavour from os.name, so a caller that
# overrides it (the tests do) builds the wrong one. Empty means npm is a bare name,
# and prepending "" would put the cwd on PATH.
npm_dir = os.path.dirname(npm)
current_path = env.get("PATH", "")
env["PATH"] = os.pathsep.join([npm_dir, current_path]) if current_path else npm_dir
if npm_dir:
env["PATH"] = os.pathsep.join([npm_dir, current_path]) if current_path else npm_dir
if os.name == "nt":
command = "& " + " ".join(_powershell_quote(arg) for arg in [npm, *args[1:]])
return (