astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
19.9k stars 586 forks source link

pyright language server crashes when run from inside venv created by uv #6399

Open DetachHead opened 3 weeks ago

DetachHead commented 3 weeks ago

i'm not sure what's causing this, but for some reason all of my language servers are crashing with strange errors when running from a venv created by uv (tested with both ruff and basedpyright language servers).

to reproduce

  1. install uv using pyprojectx:
    Invoke-WebRequest https://github.com/pyprojectx/pyprojectx/releases/latest/download/wrappers.zip -OutFile wrappers.zip; Expand-Archive -Force -Path wrappers.zip -DestinationPath .; Remove-Item -Path wrappers.zip
    ./pw --add uv
  2. ./pw uv add ruff
  3. ./pw uv add basedpyright
  4. install the ruff and basedpyright vscode extensions
  5. set the python interpreter (F1 > "Python: Select Interpreter" > ".venv")
  6. restart vscode (probably not necessary but just in case)
  7. click the Output tab in the terminal and select "Ruff" from the dropdown

    2024-08-22 11:34:42.274 [info] Using interpreter: c:\Users\user\project\.venv\Scripts\python.exe
    2024-08-22 11:34:42.274 [error] Error while trying to find the Ruff binary: Error: No pyvenv.cfg file
    
    2024-08-22 11:34:43.149 [info] Falling back to bundled executable: c:\Users\user\.vscode\extensions\charliermarsh.ruff-2024.42.0-win32-x64\bundled\libs\bin\ruff.exe

    my venv does include the pyvenv.cfg file so i don't know why that error is occurring:

    image

  8. check the basedpyright output:
    [Error - 12:04:17 PM] Server process exited with code 3221225477.
    [Error - 12:04:17 PM] Server initialization failed.
      Message: Pending response rejected since connection got disposed
      Code: -32097 
    [Info  - 12:04:17 PM] Connection to server got closed. Server will restart.

environment info

windows 11, python 3.12, vscode 1.92.2, uv 0.3.1, ruff 0.6.1

charliermarsh commented 3 weeks ago

Thanks, that's really strange. @dhruvmanila, would you have any ideas here?

dhruvmanila commented 3 weeks ago

Interesting, not sure. I'd need to look

dhruvmanila commented 3 weeks ago

Installing pyprojectx requires a pyproject.toml file. What does that look like? Does it need to be specific to pyprojectx?

DetachHead commented 3 weeks ago

sorry i shouldve updated the issue. pyprojectx is not required to reproduce the issue, i included those steps just in case but a colleague was able to reproduce the issue by installing uv normally.

i should also mention that i can't reproduce those ruff errors anymore, only the basedpyright errors.

dhruvmanila commented 3 weeks ago

sorry i shouldve updated the issue. pyprojectx is not required to reproduce the issue, i included those steps just in case but a colleague was able to reproduce the issue by installing uv normally.

So, should the steps be the following?

  1. Install uv
  2. uv init
  3. uv add ruff
  4. uv add basedpyright
  5. Install the "basedpyright" VS Code extension, disable Pylance extension
  6. Open VS Code, select the virtual environment

It's working fine on MacOS at least, let me (or ask someone) try it on a Windows machine.

DetachHead commented 3 weeks ago

hmm this is very odd, i can't seem to reproduce the issue anymore. i'll just close this issue for now and i will investigate further and reopen it once i have more info. sorry to waste your time

DetachHead commented 3 weeks ago

nevermind it does still happen with those steps, i just forgot to restart vscode so it was using the version of basedpyright bundled with the extension instead of the one in the venv.

DetachHead commented 3 weeks ago

also reproduced with the pyright pypi package

  1. uv add pyright
  2. uv run pyright --version to trigger the wrapper thing to install the npm package (might not be necessary)
  3. install generic lsp client (3rd party extension is required because pyright/pylance doesn't have a way to run the lsp from your python environment)
  4. add the following to your .vscode/settings.json:
    {
        "glspc.languageId": "python",
        "glspc.serverCommand": ".venv\\Scripts\\pyright-langserver.exe",
        "glspc.serverCommandArguments": [
            "--stdio"
        ]
    }
  5. restart vscode
  6. open a python file
  7. go to the Output tab and select "glspc" from the dropdown:
    starting glspc...
    Server process exited with code 3221225477 and signal null
    Server process exited with code 3221225477 and signal null
    Server process exited with code 3221225477 and signal null
    Server process exited with code 3221225477 and signal null
    Server process exited with code 3221225477 and signal null
DetachHead commented 2 weeks ago

so i tried to investigate this further on my end and came to the conclusion that it's something to do with the basedpyright-langserver.exe wrapper binary in .venv/Scripts. after installing basdepyright with uv, replacing the one created by uv with the one created by pip makes it work again.

i also found that passing shell: true when launching the language server from the vscode extension fixes it. https://github.com/DetachHead/basedpyright/pull/613