Skip to content

Commit 9099eca

Browse files
committed
refactor: simplify basedpyright installation logic
1 parent 27171fc commit 9099eca

File tree

1 file changed

+19
-39
lines changed

1 file changed

+19
-39
lines changed

src/lsp_client/clients/basedpyright.py

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import shutil
4+
import sys
45
from functools import partial
56
from subprocess import CalledProcessError
67
from typing import Any, override
@@ -53,45 +54,24 @@ async def ensure_basedpyright_installed() -> None:
5354
if shutil.which("basedpyright-langserver"):
5455
return
5556

56-
# Try uv tool install
57-
if shutil.which("uv"):
58-
logger.info("Attempting to install basedpyright via uv tool install...")
59-
try:
60-
await anyio.run_process(["uv", "tool", "install", "basedpyright"])
61-
logger.info("Successfully installed basedpyright via uv")
62-
return
63-
except CalledProcessError:
64-
logger.warning(
65-
"Failed to install basedpyright via uv, trying other methods..."
66-
)
67-
68-
# Try npm
69-
if shutil.which("npm"):
70-
logger.info("Attempting to install basedpyright via npm...")
71-
try:
72-
await anyio.run_process(["npm", "install", "-g", "basedpyright"])
73-
logger.info("Successfully installed basedpyright via npm")
74-
return
75-
except CalledProcessError:
76-
logger.warning(
77-
"Failed to install basedpyright via npm, trying other methods..."
78-
)
79-
80-
# Try pip
81-
import sys
82-
83-
logger.info("Attempting to install basedpyright via pip...")
84-
try:
85-
await anyio.run_process(
86-
[sys.executable, "-m", "pip", "install", "basedpyright"]
87-
)
88-
logger.info("Successfully installed basedpyright via pip")
89-
return
90-
except CalledProcessError as e:
91-
raise ServerInstallationError(
92-
"Could not install basedpyright-langserver. Please install it manually with 'uv tool install basedpyright', 'npm install -g basedpyright' or 'pip install basedpyright'. "
93-
"See https://github.com/detachhead/basedpyright for more information."
94-
) from e
57+
installers = [
58+
(["uv", "tool", "install", "basedpyright"], "uv"),
59+
(["npm", "install", "-g", "basedpyright"], "npm"),
60+
([sys.executable, "-m", "pip", "install", "basedpyright"], "pip"),
61+
]
62+
63+
for cmd, name in installers:
64+
if shutil.which(cmd[0]):
65+
try:
66+
logger.info("Attempting to install basedpyright via {}...", name)
67+
await anyio.run_process(cmd)
68+
return
69+
except CalledProcessError:
70+
continue
71+
72+
raise ServerInstallationError(
73+
"Could not install basedpyright. Please install it manually with 'uv tool install basedpyright', 'npm install -g basedpyright' or 'pip install basedpyright'."
74+
)
9575

9676

9777
BasedpyrightLocalServer = partial(

0 commit comments

Comments
 (0)