具体是这样的,今天下午我运行了一下 pip install --upgrade pyright
,把 pyright 从 1.1.275 更新到了 1.1.278。
然后打开 .py
文件发现 lsp 迟迟不能就绪。
查看进程管理发现竟然有个进程的名字叫做 npm install [email protected]
:
查看 *lsp-bridge*
缓冲发现出错了:
--- Lsp server process 60652 already exited!
Exit server: /Users/gqj/Downloads/test_py/test.py#pyright
--- Send initialize for /Users/gqj/Downloads/test_py/test.py (pyright)
--- Send (29537): initialize
npm ERR! code ENOTEMPTY
npm ERR! syscall rename
npm ERR! path /private/var/folders/m1/.../T/pyright-python-langserver.gqj/node_modules/pyright
npm ERR! dest /private/var/folders/m1/.../T/pyright-python-langserver.gqj/node_modules/.pyright-XANTzgdw
npm ERR! errno -66
npm ERR! ENOTEMPTY: directory not empty, rename '/private/var/folders/m1/.../T/pyright-python-langserver.gqj/node_modules/pyright' -> '/private/var/folders/m1/.../T/pyright-python-langserver.gqj/node_modules/.pyright-XANTzgdw'
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/gqj/.npm/_logs/2022-11-09T06_38_34_625Z-debug-0.log
Traceback (most recent call last):
File "/Users/gqj/.pyenv/versions/3.7.0/bin/pyright-langserver", line 8, in <module>
sys.exit(entrypoint())
File "/Users/gqj/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyright/langserver.py", line 57, in entrypoint
sys.exit(main(*sys.argv[1:]))
File "/Users/gqj/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyright/langserver.py", line 26, in main
return run(*args, **kwargs).returncode
File "/Users/gqj/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyright/langserver.py", line 47, in run
node.run('npm', 'install', f'pyright@{version}', cwd=str(TEMP_DIR), check=True)
File "/Users/gqj/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyright/node.py", line 112, in run
return subprocess.run(node_args, env=env, **kwargs)
File "/Users/gqj/.pyenv/versions/3.7.0/lib/python3.7/subprocess.py", line 468, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/Users/gqj/.nodenv/shims/npm', 'install', '[email protected]']' returned non-zero exit status 190.
--- Lsp server exited, exit code: 1
b''
为何 pyright/langserver.py
会启动安装命令?我好奇打开看了一下。顿时明白了,pip 安装的只不过是个“安装脚本”,而且里面有坑:
def run(
*args: str,
**kwargs: Any,
) -> Union['subprocess.CompletedProcess[bytes]', 'subprocess.CompletedProcess[str]']:
TEMP_DIR.mkdir(exist_ok=True, parents=True)
version = os.environ.get('PYRIGHT_PYTHON_FORCE_VERSION')
if version is None:
version = node.latest('pyright')
pkg = TEMP_DIR / 'node_modules' / 'pyright' / 'package.json'
if pkg.exists():
current_version = json.loads(pkg.read_text()).get('version')
else:
current_version = None
# TODO: use the same install location as the pyright CLI
if current_version is None or current_version != version:
node.run('npm', 'install', f'pyright@{version}', cwd=str(TEMP_DIR), check=True)
binary = TEMP_DIR / 'node_modules' / 'pyright' / 'langserver.index.js'
if not binary.exists():
raise RuntimeError(f'Expected language server entrypoint: {binary} to exist')
return node.run('node', str(binary), '--', *args, **kwargs)
- pip 和 npm 仓库更新不同步,前者停留在 1.1.278,而后者更新到了 1.1.279.
- 每次开启 pyright 时,就发现两个版本不匹配(哪怕比较一下版本大小也好啊),然后安装新版本。(但不知为何出错了)
- (手动更新 pyright 成功,) 重新加载走到第 2 步骤还是不匹配。
上边的源代码里还“贴心”地准备了一个环境变量,让用户可以手动解除窘境:PYRIGHT_PYTHON_FORCE_VERSION
。当然也可以直接屏蔽更新语句。
从 pip 安装也是看了 pyright 官方说明。他们难道不知道自己在挖坑吗?明明 npm 就可以安装。
刚刚 pip 和 npm 版本终于同步上了:(注意截图中的时间)