Skip to content

Commit 0d507c6

Browse files
committed
Wrap npm prefix check in cache and try-catch
1 parent 657961b commit 0d507c6

File tree

1 file changed

+15
-6
lines changed
  • python_packages/jupyter_lsp/jupyter_lsp

1 file changed

+15
-6
lines changed

python_packages/jupyter_lsp/jupyter_lsp/types.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shutil
99
import subprocess
1010
import sys
11+
from functools import lru_cache
1112
from typing import (
1213
TYPE_CHECKING,
1314
Any,
@@ -231,6 +232,17 @@ def _default_nodejs(self):
231232
shutil.which("node") or shutil.which("nodejs") or shutil.which("nodejs.exe")
232233
)
233234

235+
@lru_cache(maxsize=1)
236+
def _npm_prefix(self):
237+
try:
238+
return (
239+
subprocess.run(["npm", "prefix", "-g"], check=True, capture_output=True)
240+
.stdout.decode("utf-8")
241+
.strip()
242+
)
243+
except Exception as e: # pragma: no cover
244+
self.log.warn(f"Could not determine npm prefix: {e}")
245+
234246
@default("node_roots")
235247
def _default_node_roots(self):
236248
"""get the "usual suspects" for where `node_modules` may be found
@@ -260,12 +272,9 @@ def _default_node_roots(self):
260272

261273
# check for custom npm prefix
262274
if shutil.which("npm"):
263-
prefix = (
264-
subprocess.run(["npm", "prefix", "-g"], check=True, capture_output=True)
265-
.stdout.decode("utf-8")
266-
.strip()
267-
)
268-
roots += [pathlib.Path(prefix) / "lib"]
275+
prefix = self._npm_prefix()
276+
if prefix:
277+
roots += [pathlib.Path(prefix) / "lib"]
269278

270279
return roots
271280

0 commit comments

Comments
 (0)