sublimelsp / LSP-pyright

Python support for Sublime's LSP plugin provided through microsoft/pyright.
MIT License
126 stars 13 forks source link

Pyright unable to find venv/pythonPath with WSL2 #136

Open didimelli opened 2 years ago

didimelli commented 2 years ago

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?

rwols commented 2 years ago

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()

didimelli commented 2 years ago

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

About the console output, i get nothing: image

jfcherng commented 2 years ago

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?

didimelli commented 2 years ago

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

rwols commented 2 years ago

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.

didimelli commented 2 years ago

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

jfcherng commented 2 years ago

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

jfcherng commented 2 years ago

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.
didimelli commented 2 years ago

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
jfcherng commented 2 years ago

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.
didimelli commented 2 years ago

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.

jfcherng commented 2 years ago

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

didimelli commented 2 years ago

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

deathaxe commented 1 year ago

\\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.