sublimelsp / LSP-pyright

Python support for Sublime's LSP plugin provided through microsoft/pyright.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

not loading venv (poetry)

mustafa0x opened this issue · comments

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

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)

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.

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.

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

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

LSP-pyright/plugin.py

Lines 241 to 248 in de805dd

python_path = subprocess.check_output(
command,
cwd=workspace_folder,
shell=True,
startupinfo=get_default_startupinfo(),
stderr=subprocess.STDOUT,
universal_newlines=True,
).strip()
. @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.

That didn't help. =/

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

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

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

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

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

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

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.

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.

Closed as resolved.