sublimelsp / LSP-pyright

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

not loading venv (poetry) #308

Closed mustafa0x closed 7 months ago

mustafa0x commented 8 months ago
LSP-pyright: INFO: poetry.lock detected. Run subprocess command: poetry env info -p
LSP-pyright: WARN: subprocess failed: 
LSP-pyright: INFO: Using python path "/usr/local/bin/python3"

This used to work, not sure what changed. I suspect that poetry env info -p is not run from the current directory.

>>> import os; os.getcwd()
'/Applications/Sublime Text.app/Contents/MacOS'

Ok, it's not a CWD issue, since adding -C :dir didn't help. For some reason subprocess is failing. I tried the same command in a normal python repl and it worked fine.

$> /usr/bin/python3
Python 3.9.6
[Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run('poetry env info -p -C :dir'.split())
/Users/:user/Library/Caches/pypoetry/virtualenvs/app-F-py3.12
jfcherng commented 8 months ago

Potentially

but they has been like 3 months ago already.

subprocess.run('poetry env info -p -C :dir'.split())

We use subprocess.run('poetry env info -p -C :dir', shell=True)

mustafa0x commented 8 months ago

In sublime console,

>>> print(subprocess.run('poetry -C :dir env info', shell=True, capture_output=True).stdout.decode())

Virtualenv
Python:         3.11.4
Implementation: CPython
Path:           NA
Executable:     NA

System
Platform:   darwin
OS:         posix
Python:     3.11.4
Path:       /usr/local/opt/python@3.11/Frameworks/Python.framework/Versions/3.11
Executable: /usr/local/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/bin/python3.11

If I add the -p flag then returncode=1 and stdout=''. In either case, poetry is not seeing the environment. I'm not sure if it's due to it using homebrew's python. It shouldn't be.

rwols commented 8 months ago

You seem to be using python3.9 installed from Homebrew in your shell environment, but a system python3.11 in the ST env. This may point to $PATH issues.

mustafa0x commented 8 months ago

Yes I believe there's a PATH issue. It's strange that it affects poetry. I'll continue to investigate.

jfcherng commented 8 months ago

We use subprocess.run('poetry env info -p -C :dir', shell=True)

Yes I believe there's a PATH issue. It's strange that it affects poetry. I'll continue to investigate.

I noticed that we didn't add env=os.environ in https://github.com/sublimelsp/LSP-pyright/blob/de805dd140bfdbefd82f2c428b9149c7623f633e/plugin.py#L241-L248 . @mustafa0x Do you know how to add it on your machine for testing?


Find this on Python's docs... seems not env=os.environ issue here.

If env is not None, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’ environment.

mustafa0x commented 8 months ago

That didn't help. =/

I confirmed that it registered the code change by tweaking the log message on line 256.

mustafa0x commented 8 months ago

LSP-pyright.sublime-settings

{
  "settings": {
    "python.pythonPath": "/Users/:user/.local/share/mise/installs/python/3.12/bin/python"
  }
}

This helped, but subproccess.run still fails. I wonder if the fact that poetry is a symlink affects things.

$> stat /usr/local/bin/poetry
  File: /usr/local/bin/poetry -> /Users/:user/.local/share/mise/installs/poetry/latest/bin/poetry
mustafa0x commented 8 months ago

Ok, the symlink can't be the reason. This also fails,

subprocess.run('/Users/:user/.local/share/mise/installs/poetry/latest/bin/poetry -C :dir env info', shell=True, capture_output=True).stdout.decode()

mustafa0x commented 8 months ago

Ok, prepending mise x -- works. Not sure what it changes.

>>>subprocess.run('mise x -- poetry -C :dir env info -p', shell=True, capture_output=True).stdout
/Users/:user/Library/Caches/pypoetry/virtualenvs/:app-F-py3.12
jfcherng commented 8 months ago

Could you try modify PATH env variable in ~/.profile? And then maybe re-login and restart ST.

rchl commented 8 months ago

On Mac you modify .zprofile and it's only necessary to restart ST (not the system).

More info at https://lsp.sublimetext.io/troubleshooting/#updating-the-path-used-by-lsp-servers

mustafa0x commented 8 months ago

Yes, modifying PATH fixes. In the sublime console I entered:

import os
os.environ['PATH'] = '/Users/:user/.local/share/mise/shims:' + os.environ['PATH']

subprocess.run('poetry -C :dir env info', shell=True, capture_output=True, env=os.environ).stdout

Output:

Virtualenv
Python:         3.12.1
Implementation: CPython
Path:           /Users/:user/Library/Caches/pypoetry/virtualenvs/:app-py3.12
Executable:     /Users/:user/Library/Caches/pypoetry/virtualenvs/:app-py3.12/bin/python
Valid:          True

System
Platform:   darwin
OS:         posix
Python:     3.12.1
Path:       /Users/:user/.local/share/mise/installs/python/3.12.1
Executable: /Users/:user/.local/share/mise/installs/python/3.12.1/bin/python3.12

I'll add that PATH entry in ~/.bash_profile.

rchl commented 8 months ago

I told you that you are supposed to modify (or create if it doesn't exist) .zprofile if you are using default shell which is zsh.

ST loads variables on start using /bin/zsh -l. You can see a line like that in ST console when it's starting.

jfcherng commented 7 months ago

Closed as resolved.