Skip to content

Commit f4beb25

Browse files
committed
feat(cli): mention nodejs extra if nodeenv install fails
1 parent e2d0748 commit f4beb25

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

pyright/node.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def _postfix_for_target(target: Target) -> str:
4444
def _ensure_node_env(target: Target) -> Path:
4545
log.debug('Checking for nodeenv %s binary', target)
4646

47-
path = BINARIES_DIR.joinpath(target + _postfix_for_target(target))
48-
47+
path = _get_nodeenv_path(target)
4948
log.debug('Using %s path for binary', path)
5049

5150
if path.exists() and not NODE_VERSION:
@@ -59,6 +58,10 @@ def _ensure_node_env(target: Target) -> Path:
5958
return path
6059

6160

61+
def _get_nodeenv_path(target: Target) -> Path:
62+
return BINARIES_DIR.joinpath(target + _postfix_for_target(target))
63+
64+
6265
def _get_global_binary(target: Target) -> Optional[Path]:
6366
log.debug('Checking for global target binary: %s', target)
6467

@@ -85,7 +88,13 @@ def _install_node_env() -> None:
8588
args += ['--node', NODE_VERSION, '--force']
8689
args.append(str(ENV_DIR))
8790
log.debug('Running command with args: %s', args)
88-
subprocess.run(args, check=True)
91+
92+
try:
93+
subprocess.run(args, check=True)
94+
except subprocess.CalledProcessError as exc:
95+
raise RuntimeError(
96+
'nodeenv failed; for more reliable node.js binaries try `pip install pyright[nodejs]`'
97+
) from exc
8998

9099

91100
class GlobalStrategy(NamedTuple):

tests/test_node.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
import subprocess
34
from typing import TYPE_CHECKING, Tuple
45
from pathlib import Path
@@ -90,6 +91,27 @@ def test_node_version_env() -> None:
9091
assert maybe_decode(proc.stdout).strip() == 'v13.1.0'
9192

9293

94+
@mock.patch('pyright.node.USE_GLOBAL_NODE', False)
95+
@mock.patch('pyright.node.USE_NODEJS_WHEEL', False)
96+
@mock.patch('pyright.node.NODE_VERSION', None)
97+
@mock.patch('pyright.node.BINARIES_DIR', pyright.node.BINARIES_DIR.joinpath('empty'))
98+
def test_nodeenv_flaky_error(fake_process: FakeProcess) -> None:
99+
"""A helpful error is raised when nodeenv fails."""
100+
fake_process.register_subprocess( # pyright: ignore[reportUnknownMemberType]
101+
[sys.executable, '-m', 'nodeenv', str(pyright.node.ENV_DIR)],
102+
stdout='this thing is flaky',
103+
returncode=1,
104+
)
105+
106+
with pytest.raises(RuntimeError, match=r'install pyright\[nodejs\]'):
107+
pyright.node.run(
108+
'node',
109+
'--version',
110+
stdout=subprocess.PIPE,
111+
stderr=subprocess.STDOUT,
112+
)
113+
114+
93115
def test_update_path_env(tmp_path: Path) -> None:
94116
"""The _update_path_env() function correctly appends the target binary path to the PATH environment variable"""
95117
target = tmp_path / 'bin'

0 commit comments

Comments
 (0)