The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(lsp): probe Windows .cmd/.exe/.bat wrappers before npm's POSIX shim (#86445)
BrierAinzd6e5cda29ca4
diff --git a/agent/lsp/install.py b/agent/lsp/install.pyindex d4c2f2e4950..405865382a1 100644--- a/agent/lsp/install.py+++ b/agent/lsp/install.py@@ -91,12 +91,18 @@ def hermes_lsp_bin_dir() -> Path:return p-def _native_binary_candidates(base: Path) -> list[Path]:- """Return platform-native executable candidates for a staged binary (``base`` plus Windows wrappers)."""- if not _is_windows():+def _native_binary_candidates(base: Path, *, is_windows: Optional[bool] = None) -> list[Path]:+ """Return platform-native executable candidates for a staged binary, most runnable first.++ On Windows the ``.cmd``/``.exe``/``.bat`` wrappers come BEFORE the bare name: npm writes a+ POSIX ``#!/bin/sh`` shim under the bare name next to its ``.cmd``, ``os.access(X_OK)`` is+ always true there, and ``CreateProcess`` on the shim fails with WinError 193. The bare name+ stays as a last resort for genuinely extension-less executables.+ """+ if not (_is_windows() if is_windows is None else is_windows):return [base]cands: Dict[str, Path] = {}- for c in (base, *(Path(str(base) + s) for s in _WINDOWS_WRAPPER_SUFFIXES)):+ for c in (*(Path(str(base) + s) for s in _WINDOWS_WRAPPER_SUFFIXES), base):cands.setdefault(str(c).lower(), c)return list(cands.values())@@ -106,12 +112,18 @@ def _first_existing(*bases: Path) -> Optional[Path]:return next((c for base in bases for c in _native_binary_candidates(base) if c.exists()), None)+def _npm_bin_dir() -> Path:+ """npm's own ``node_modules/.bin`` under the staging tree, where its ``%~dp0``-relative wrappers work."""+ return hermes_lsp_bin_dir().parent / "node_modules" / ".bin"++def _existing_binary(name: str) -> Optional[str]:- """Probe the staging dir + PATH for a binary named ``name``."""- for staged in _native_binary_candidates(hermes_lsp_bin_dir() / name):+ """Probe the staging dir (+ npm's bin dir on Windows) then PATH for a binary named ``name``."""+ bases = [hermes_lsp_bin_dir() / name] + ([_npm_bin_dir() / name] if _is_windows() else [])+ for staged in (c for base in bases for c in _native_binary_candidates(base)):if staged.exists() and os.access(staged, os.X_OK):return str(staged)- suffixes = ("", *_WINDOWS_WRAPPER_SUFFIXES) if _is_windows() else ("",)+ suffixes = (*_WINDOWS_WRAPPER_SUFFIXES, "") if _is_windows() else ("",)return next((p for s in suffixes if (p := shutil.which(f"{name}{s}"))), None)@@ -198,7 +210,9 @@ def _install_npm(pkg: str, bin_name: str, extra_pkgs: Optional[list] = None) ->return Nonefound = _first_existing(staging / "node_modules" / ".bin" / bin_name)if found is not None:- return _link_into_bin(found)+ # npm's Windows wrappers resolve their payload via ``%~dp0\..\<pkg>``, so a copy or symlink+ # in ``lsp/bin/`` points at nothing; use them where npm put them (``_existing_binary`` probes there).+ return str(found) if _is_windows() and found.suffix.lower() in (".cmd", ".bat") else _link_into_bin(found)logger.warning("[install] npm install for %s succeeded but bin %s not found", pkg, bin_name)return Nonediff --git a/agent/lsp/servers.py b/agent/lsp/servers.pyindex 4efab4db318..e7ccfe016a7 100644--- a/agent/lsp/servers.py+++ b/agent/lsp/servers.py@@ -162,9 +162,11 @@ def _spawn_pyright(root: str, ctx: ServerContext) -> Optional[SpawnSpec]:bin_path = _find_binary(ctx, "pyright", ("pyright-langserver", "pyright"), "pyright")if bin_path is None:return None- # If we got the cli ``pyright``, the langserver is its sibling.- if os.path.basename(bin_path) in {"pyright", "pyright.exe"}:- sibling = os.path.join(os.path.dirname(bin_path), "pyright-langserver")+ # If we got the cli ``pyright``, the langserver is its sibling — same suffix, since on Windows+ # the bare sibling is npm's unrunnable POSIX shim.+ stem, suffix = os.path.splitext(os.path.basename(bin_path))+ if stem == "pyright":+ sibling = os.path.join(os.path.dirname(bin_path), f"pyright-langserver{suffix}")if os.path.exists(sibling):bin_path = sibling# Point pyright at the project venv; its default "python on PATH" rarely is.diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]new file mode 100644index 00000000000..0068f9b25ad--- /dev/null+++ b/contributors/emails/[email protected]@@ -0,0 +1,2 @@+BrierAinz+# PR #73088 salvagediff --git a/tests/agent/lsp/test_windows_binary_resolution.py b/tests/agent/lsp/test_windows_binary_resolution.pynew file mode 100644index 00000000000..bba293b4d9a--- /dev/null+++ b/tests/agent/lsp/test_windows_binary_resolution.py@@ -0,0 +1,32 @@+"""Windows binary resolution must prefer the runnable ``.cmd``/``.exe``/``.bat`` wrapper over npm's+POSIX ``#!/bin/sh`` shim, which shares the bare name and fails ``CreateProcess`` with WinError 193."""++import subprocess+from pathlib import Path++import pytest++from agent.lsp import install+++def test_windows_candidates_order_wrappers_before_bare_shim(tmp_path: Path):+ base = tmp_path / "pyright-langserver"+ win = [p.name for p in install._native_binary_candidates(base, is_windows=True)]+ assert win == ["pyright-langserver.cmd", "pyright-langserver.exe", "pyright-langserver.bat", "pyright-langserver"]+ assert install._native_binary_candidates(base, is_windows=False) == [base]++[email protected]_only+def test_existing_binary_resolves_runnable_cmd_over_posix_shim(tmp_path: Path, monkeypatch):+ """Live: staging dir holds npm's shim AND its .cmd; the resolved path must actually run."""+ monkeypatch.setattr(install, "hermes_lsp_bin_dir", lambda: tmp_path)+ shim = tmp_path / "tool"+ shim.write_text("#!/bin/sh\nexec node \"$0.js\" \"$@\"\n")+ (tmp_path / "tool.cmd").write_text("@echo off\r\necho wrapper-ran\r\n")++ resolved = install._existing_binary("tool")++ assert resolved == str(tmp_path / "tool.cmd")+ out = subprocess.run([str(resolved)], capture_output=True, text=True, encoding="utf-8", errors="replace",+ check=True, stdin=subprocess.DEVNULL)+ assert "wrapper-ran" in out.stdout