microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.71k stars 765 forks source link

Pyright is using Incorrect Interpreter Path #5432

Open mbway opened 9 months ago

mbway commented 9 months ago

Type: Bug

Behaviour

Expected vs. Actual

The python plugin sends the incorrect interpreter path to langauge server plugins such as pyright if the name of the virtual environment name begins with "python" (e.g. python_env) (as far as I can tell from trying many different names).

For some reason this only seems to affect the name of the virtualenv itself. If the virtualenv has a path such as /tmp/python_envs/venv/ then the plugin detects the interpreter path correctly.

Steps to reproduce:

  1. create the following settings.json
    {
    "python.languageServer": "None",
    }
  2. install the python and pyright plugins
  3. create a virtual environment with a name beginning with python (e.g. python_env)
  4. activate the virtual environment
  5. open vscodium in an empty directory
  6. create an open a .py file

The output of the python plugin:

2024-01-09 21:30:31.040 [info] Telemetry level is off
2024-01-09 21:30:31.040 [info] Experiments are disabled, only manually opted experiments are active.
2024-01-09 21:30:31.065 [info] Default formatter is set to null for workspace /tmp/workspace
2024-01-09 21:30:31.073 [info] VS Code was launched from an activated environment: 'python_env', selecting it as the interpreter for workspace.
2024-01-09 21:30:31.076 [info] Test server listening.
2024-01-09 21:30:31.079 [info] Python interpreter path: ~/.virtualenvs/python_env/bin/python
2024-01-09 21:30:31.112 [info] Editor support is inactive since language server is set to None.
2024-01-09 21:30:31.159 [info] Send text to terminal: source ~/.virtualenvs/python_env/bin/activate

The output of the pyright plugin:

[Info  - 21:30:31] Pyright language server 1.1.345 starting
[Info  - 21:30:31] Server root directory: file:///home/matthew/.vscode-oss/extensions/ms-pyright.pyright-1.1.345/dist
[Info  - 21:30:31] Starting service instance "workspace"
Received pythonPath from Python extension: /home/matthew/.virtualenvs/python_env
[Info  - 21:30:31] Setting pythonPath for service "workspace": "/home/matthew/.virtualenvs/python_env"
[Info  - 21:30:31] Unable to get Python version from interpreter
[Info  - 21:30:31] Found 1 source file

pyright is receiving the root directory of the virtualenv instead of the interpreter path.

A simple workaround is to rename the virtual environment but this is still a bug as the result is unexpected.

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

``` XXX ```

User Settings

``` languageServer: "None" ```

Extension version: 2023.22.1 VSCodium version: VSCodium 1.85.1 (08e6c15293922dd53a864bb041be381322fee401, 2023-12-14T20:36:42.296Z) OS version: Linux x64 6.1.71-1-lts Modes:

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i7-10870H CPU @ 2.20GHz (16 x 3599)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
video_decode: enabled
video_encode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off| |Load (avg)|2, 1, 2| |Memory (System)|31.05GB (21.93GB free)| |Process Argv|--ms-enable-electron-run-as-node .| |Screen Reader|no| |VM|0%| |DESKTOP_SESSION|plasma| |XDG_CURRENT_DESKTOP|KDE| |XDG_SESSION_DESKTOP|KDE| |XDG_SESSION_TYPE|x11|
karrtikr commented 9 months ago

Hi @mbway, it seems to me that https://github.com/microsoft/pyright might be using an older API to getting interpreter path instead of the updated API: https://github.com/microsoft/vscode-python/wiki/Python-Environment-APIs#extension-api-usage. I'm transferring this issue to Pylance to further investigate.

debonte commented 9 months ago

I was able to repro this on Linux and Windows. It won't repro until you launch VS Code from the project directory with the python_env activated. After that, you can deactivate the venv and it seems to keep reproing, at least within the same terminal session. Can't explain that. I guess something is getting cached somewhere.

Pyright is using Python's settings.getExecutionDetails API which I think confirms Kartik's suggestion that we're using an outdated API. This doesn't repro in Pylance which uses environments.getActiveEnvironmentPath + environments.resolveEnvironment.

I was able to repro this in the debugger by setting the following in the launch.json config. Replace the venv path with the path to your venv. It doesn't need to be under .virtualenvs. But on Linux you need to use /home/foo instead of ~.

            "env":{
                "VSCODE_CLI": "1",
                "VIRTUAL_ENV": "/home/erikd/.virtualenvs/python_env"
            }