LSP-pyright icon indicating copy to clipboard operation
LSP-pyright copied to clipboard

Pyright unable to find venv/pythonPath with WSL2

Open didimelli opened this issue 3 years ago • 14 comments

Hello, I am using SublimeText 4, installed on windows, to open a WSL2 project.

All my venvs are in the proj root dir, in the folder .venv (this means that python is at .venv/bin/python).

My pyright settings are the following:

{
    "settings": {
        "python.venvPath": "./.venv",
        "python.pythonPath": "./.venv/bin/python",
    }
}

When I open the log panel, what I see is this:

LSP-pyright: Pyright language server 1.1.222 starting
LSP-pyright: Server root directory: C:\Users\davide.melli\AppData\Local\Sublime Text\Package Storage\LSP-pyright\14.17.6\language-server\node_modules\pyright/dist/
LSP-pyright: No configuration file found.
LSP-pyright: No pyproject.toml file found.
LSP-pyright: Setting pythonPath for service "leaf-radio": "\Ubuntu-20.04\home\didi\leaf\leaf-radio\.venv\bin\python"
LSP-pyright: venvPath \Ubuntu-20.04\home\didi\leaf\leaf-radio\.venv is not a valid directory.
LSP-pyright: stubPath \Ubuntu-20.04\home\didi\leaf\leaf-radio\typings is not a valid directory.
LSP-pyright: Unable to get Python version from interpreter
LSP-pyright: Assuming Python platform Windows
LSP-pyright: Searching for source files
LSP-pyright: File or directory "\Ubuntu-20.04\home\didi\leaf\leaf-radio" does not exist.
LSP-pyright: No source files found.

Is there any way to make Pyright aware of the WSL mount and resolve the paths correctly?

didimelli avatar Feb 18 '22 09:02 didimelli

Are you opening ST4 from within the WSL2 environment? If so, can you open the console with View > Show Console and run the following in the console: view.file_name()

rwols avatar Feb 18 '22 09:02 rwols

Yep, to open it i do /mnt/c/Program\ Files/Sublime\ Text/subl.exe .

About the console output, i get nothing: image

didimelli avatar Feb 18 '22 10:02 didimelli

Yep, to open it i do /mnt/c/Program\ Files/Sublime\ Text/subl.exe .

About the console output, i get nothing: image

What if you open a py file?

jfcherng avatar Feb 18 '22 10:02 jfcherng

Oh, sorry, so silly of me. Here the output with a .py file: image

didimelli avatar Feb 18 '22 10:02 didimelli

I guess WSL2 path handling is broken (did that ever work?). I would suggest you open the folder from ST4 started in a regular Windows environment.

rwols avatar Feb 18 '22 10:02 rwols

If i open ST4, then menu->file->Open Folder->search a WSL folder from the explorer I get the same exact behaviour.

didimelli avatar Feb 18 '22 10:02 didimelli

I can reproduce this but I didn't work under WSL2.

jfcherng avatar Feb 18 '22 10:02 jfcherng

Fwiw, I tried this with VSCode + Pyright/Pylance emits the same error. Something like

LSP-pyright: venvPath \Ubuntu-20.04\home\didi\leaf\leaf-radio\.venv is not a valid directory.
LSP-pyright: stubPath \Ubuntu-20.04\home\didi\leaf\leaf-radio\typings is not a valid directory.

jfcherng avatar Apr 14 '22 00:04 jfcherng

VSCode has this extension https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl which allows you to seamlessy use the linux filesystem.

Here some logs from the LSP:

[Info  - 11:56:02 AM] (12839) Pylance language server 2022.4.0 (pyright 67ee16be) starting
[Info  - 11:56:02 AM] (12839) Server root directory: /home/didi/.vscode-server/extensions/ms-python.vscode-pylance-2022.4.0/dist
[Info  - 11:56:02 AM] (12839) No configuration file found.
[Info  - 11:56:02 AM] (12839) pyproject.toml file found at /home/didi/leaf/leaf-radio.
[Info  - 11:56:02 AM] (12839) Setting pythonPath for service "leaf-radio": "/home/didi/leaf/leaf-radio/.venv/bin/python"
[Info  - 11:56:02 AM] (12839) Loading pyproject.toml file at /home/didi/leaf/leaf-radio/pyproject.toml
[Error - 11:56:02 AM] (12839) Pyproject file "/home/didi/leaf/leaf-radio/pyproject.toml" is missing "[tool.pyright]" section.
[Warn  - 11:56:02 AM] (12839) stubPath /home/didi/leaf/leaf-radio/typings is not a valid directory.
[Info  - 11:56:02 AM] (12839) Assuming Python version 3.8
[Info  - 11:56:02 AM] (12839) Assuming Python platform Linux
[Info  - 11:56:02 AM] (12839) Search paths for /home/didi/leaf/leaf-radio
[Info  - 11:56:02 AM] (12839)   /home/didi/.vscode-server/extensions/ms-python.vscode-pylance-2022.4.0/dist/typeshed-fallback/stdlib
[Info  - 11:56:02 AM] (12839)   /home/didi/leaf/leaf-radio
[Info  - 11:56:02 AM] (12839)   /home/didi/leaf/leaf-radio/typings
[Info  - 11:56:02 AM] (12839)   /home/didi/.vscode-server/extensions/ms-python.vscode-pylance-2022.4.0/dist/typeshed-fallback/stubs/...
[Info  - 11:56:02 AM] (12839)   /home/didi/.vscode-server/extensions/ms-python.vscode-pylance-2022.4.0/dist/bundled/stubs
[Info  - 11:56:02 AM] (12839)   /home/didi/.pyenv/versions/3.8.12/lib/python3.8
[Info  - 11:56:02 AM] (12839)   /home/didi/.pyenv/versions/3.8.12/lib/python3.8/lib-dynload
[Info  - 11:56:02 AM] (12839)   /home/didi/leaf/leaf-radio/.venv/lib/python3.8/site-packages
[Info  - 11:56:02 AM] (12839) Adding fs watcher for library directories:
 /home/didi/.pyenv/versions/3.8.12/lib/python3.8
/home/didi/.pyenv/versions/3.8.12/lib/python3.8/lib-dynload
/home/didi/leaf/leaf-radio/.venv/lib/python3.8/site-packages
[Info  - 11:56:02 AM] (12839) Adding fs watcher for directories:
 /home/didi/leaf/leaf-radio

didimelli avatar Apr 14 '22 07:04 didimelli

VSCode has this extension marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl which allows you to seamlessy use the linux filesystem.

Not open sourced so we have no idea what it does. Without its aid, VSCode just emits the same error message. I.e., something like

LSP-pyright: venvPath \Ubuntu-20.04\home\didi\leaf\leaf-radio\.venv is not a valid directory.
LSP-pyright: stubPath \Ubuntu-20.04\home\didi\leaf\leaf-radio\typings is not a valid directory.

jfcherng avatar Apr 14 '22 07:04 jfcherng

as the docs says:

With VS Code and the Remote - WSL extension combined, VS Code’s UI runs on Windows, and all your commands, extensions, and even the terminal, run on Linux. You get the full VS Code experience, including autocomplete and debugging, powered by the tools and compilers installed on Linux.

didimelli avatar Apr 14 '22 07:04 didimelli

I think this should happen in every non-VSCode editors. Maybe there is already a solution in them (neovim, etc...).

jfcherng avatar Apr 14 '22 08:04 jfcherng

I am quite sure that the Intellij suite has a similar solution, but unfortunately not open source!

didimelli avatar Apr 14 '22 08:04 didimelli

\\wsl.localhost\<distro>\... is the path windows applications can use to access WSL2 filesystem. It doesn't matter whether they are executed directly from Windows or from within WSL2.

A path translation algorithm just needs to be aware of all possible combinations of running [ Linux | Windows ] App in [ WSL | Windows ] accessing [ Linux | Windows ] paths.

Some inspiration may be taken from https://github.com/SublimeText/WslBuild/blob/ebf26d251d5aeab99f515fca3c5947314c5841aa/plugin.py#L260-L276. Those lines have also found their way into GitGutter, to enable it to use Git for Windows to diff files located within WSL2 filesystem.

deathaxe avatar Feb 18 '23 09:02 deathaxe