Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly find npm on Python 3.12 (win) #47

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/sphinx_theme_builder/_internal/nodejs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""

import os
import pathlib
import platform
import shlex
import shutil
import subprocess
Expand Down Expand Up @@ -41,6 +43,26 @@ def _get_bool_env_var(name: str, *, default: bool) -> bool:
)


def _resolve_executable_win_312(name: str, path: str) -> str | None:
resolved_name = shutil.which(name, path=path)
resolved_path = pathlib.Path(resolved_name)
# Python 3.12.0 prioritises extensionless `which` results
# If we found something with an extension, it is correct
if resolved_path.suffix:
return resolved_name

try:
extensions = os.environ["PATHEXT"]
except KeyError:
return resolved_name

for extension in extensions.split(os.pathsep):
candidate_path = resolved_path.with_suffix(extension)
if candidate_path.exists():
return os.fsdecode(candidate_path)
return resolved_name


def run_in(
nodeenv: Path, args: List[str], *, production: bool = False, **kwargs: Any
) -> "Optional[subprocess.CompletedProcess[bytes]]":
Expand All @@ -57,8 +79,13 @@ def run_in(
}

# Fully qualify the first argument.
resolved_name = shutil.which(args[0], path=env["PATH"])
if not resolved_name:
# On Windows with Python 3.12.0, we need to handle modified behavior in which
if ((3, 12, 0) <= sys.version_info < (3, 12, 1)) and platform.system() == "Windows":
resolved_name = _resolve_executable_win_312(args[0], path=env["PATH"])
else:
resolved_name = shutil.which(args[0], path=env["PATH"])

if resolved_name is None:
raise FileNotFoundError(resolved_name)
args[0] = resolved_name

Expand Down