Skip to content

The agent that grows with you

fix(lsp): pin Windows wrapper resolution through _existing_binary and the pyright sibling

teknium1966959da0896
diff --git a/agent/lsp/install.py b/agent/lsp/install.py
index 405865382a1..8fb429837df 100644
--- a/agent/lsp/install.py
+++ b/agent/lsp/install.py
@@ -107,9 +107,9 @@ def _native_binary_candidates(base: Path, *, is_windows: Optional[bool] = None)
return list(cands.values())
-def _first_existing(*bases: Path) -> Optional[Path]:
+def _first_existing(*bases: Path, is_windows: Optional[bool] = None) -> Optional[Path]:
"""First platform-native candidate of any ``base`` that exists on disk."""
- return next((c for base in bases for c in _native_binary_candidates(base) if c.exists()), None)
+ return next((c for base in bases for c in _native_binary_candidates(base, is_windows=is_windows) if c.exists()), None)
def _npm_bin_dir() -> Path:
@@ -117,13 +117,17 @@ def _npm_bin_dir() -> Path:
return hermes_lsp_bin_dir().parent / "node_modules" / ".bin"
-def _existing_binary(name: str) -> Optional[str]:
- """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)):
+def _existing_binary(name: str, *, is_windows: Optional[bool] = None) -> Optional[str]:
+ """Probe the staging dir (+ npm's bin dir on Windows) then PATH for a binary named ``name``.
+
+ ``is_windows`` overrides the host check so the Windows resolution is testable as data on every lane.
+ """
+ win = _is_windows() if is_windows is None else is_windows
+ bases = [hermes_lsp_bin_dir() / name] + ([_npm_bin_dir() / name] if win else [])
+ for staged in (c for base in bases for c in _native_binary_candidates(base, is_windows=win)):
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 win else ("",)
return next((p for s in suffixes if (p := shutil.which(f"{name}{s}"))), None)
diff --git a/tests/agent/lsp/test_windows_binary_resolution.py b/tests/agent/lsp/test_windows_binary_resolution.py
index bba293b4d9a..b7ba51e3b1a 100644
--- a/tests/agent/lsp/test_windows_binary_resolution.py
+++ b/tests/agent/lsp/test_windows_binary_resolution.py
@@ -1,19 +1,44 @@
"""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 os
import subprocess
from pathlib import Path
import pytest
-from agent.lsp import install
+from agent.lsp import install, servers
-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]
+def _npm_shim_pair(bin_dir: Path, name: str) -> Path:
+ """npm's Windows layout: POSIX shim under the bare name, runnable ``.cmd`` beside it (both X_OK)."""
+ bin_dir.mkdir(parents=True, exist_ok=True)
+ shim = bin_dir / name
+ shim.write_text("#!/bin/sh\nexec node \"$0.js\" \"$@\"\n", encoding="utf-8")
+ cmd = bin_dir / f"{name}.cmd"
+ cmd.write_text("@echo off\r\necho wrapper-ran\r\n", encoding="utf-8")
+ for p in (shim, cmd):
+ os.chmod(p, 0o755)
+ return cmd
+
+
+def test_windows_resolution_picks_cmd_wrapper_from_npm_bin_over_posix_shim(tmp_path: Path, monkeypatch):
+ """Drives the production entry with ``is_windows`` as data (no sys.platform patch): the pair
+ lives where npm leaves it (``lsp/node_modules/.bin``), and pyright's langserver sibling keeps
+ the resolved suffix instead of falling onto the bare shim."""
+ monkeypatch.setattr(install, "hermes_lsp_bin_dir", lambda: tmp_path / "bin")
+ (tmp_path / "bin").mkdir()
+ cmd = _npm_shim_pair(tmp_path / "node_modules" / ".bin", "tool")
+
+ assert install._existing_binary("tool", is_windows=True) == str(cmd)
+ # POSIX resolution never looks in npm's bin dir and never appends wrappers.
+ assert install._existing_binary("tool", is_windows=False) is None
+
+ pyright_cmd = _npm_shim_pair(tmp_path / "node_modules" / ".bin", "pyright")
+ langserver_cmd = _npm_shim_pair(tmp_path / "node_modules" / ".bin", "pyright-langserver")
+ ctx = servers.ServerContext(workspace_root=str(tmp_path), binary_overrides={"pyright": [str(pyright_cmd)]})
+ spec = servers._spawn_pyright(str(tmp_path), ctx)
+ assert spec is not None and spec.command[0] == str(langserver_cmd)
@pytest.mark.windows_only
@@ -21,8 +46,8 @@ def test_existing_binary_resolves_runnable_cmd_over_posix_shim(tmp_path: Path, m
"""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")
+ shim.write_text("#!/bin/sh\nexec node \"$0.js\" \"$@\"\n", encoding="utf-8")
+ (tmp_path / "tool.cmd").write_text("@echo off\r\necho wrapper-ran\r\n", encoding="utf-8")
resolved = install._existing_binary("tool")