uv icon indicating copy to clipboard operation
uv copied to clipboard

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

Open DetachHead opened this issue 1 year ago • 9 comments

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

DetachHead avatar Aug 22 '24 01:08 DetachHead

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

charliermarsh avatar Aug 22 '24 03:08 charliermarsh

Interesting, not sure. I'd need to look

dhruvmanila avatar Aug 22 '24 03:08 dhruvmanila

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

dhruvmanila avatar Aug 22 '24 03:08 dhruvmanila

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.

DetachHead avatar Aug 22 '24 03:08 DetachHead

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.

dhruvmanila avatar Aug 22 '24 04:08 dhruvmanila

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 avatar Aug 22 '24 04:08 DetachHead

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 avatar Aug 22 '24 04:08 DetachHead

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 avatar Aug 22 '24 05:08 DetachHead

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

DetachHead avatar Aug 24 '24 03:08 DetachHead

looks like this was the same issue as #6464 which has been fixed

DetachHead avatar Nov 09 '25 04:11 DetachHead