emacs-lsp / lsp-pyright

lsp-mode :heart: pyright
https://emacs-lsp.github.io/lsp-pyright
GNU General Public License v3.0
291 stars 25 forks source link

support multiple venvs in a monorepo #88

Open randomphrase opened 1 year ago

randomphrase commented 1 year ago

Hi, I am attempting to use lsp-pyright with a monorepo and multiple venvs. There is an example repo here which can be used to demonstrate the problem. Specifically:

  1. Clone the repo, and create two venvs, one for libs/base and one for libs/fancy. The specific instructions are here under the heading "Sandboxes". This should result in two .venv directories within the two library directories.
  2. In emacs, open libs/base/adder2.py and ensure that lsp mode is active. This should start pyright.
  3. Use lsp-workspace-folders-add to add the libs/base and libs/fancy folders.
  4. Restart pyright.

If I understand correctly, the above steps should result in Pyright correctly able to resolve both internal and external dependencies. However it does not seem to. The problem, I believe, stems from setting the pythonPath incorrectly. Here is an extract from the *lsp-log* buffer.

Setting pythonPath for service "fancy": "/Users/alastair/hack/extern/python-monorepo-example/libs/base/.venv/bin/python"
Loading pyproject.toml file at /Users/alastair/hack/extern/python-monorepo-example/libs/fancy/pyproject.toml
Assuming Python version 3.11
No include entries specified; assuming /Users/alastair/hack/extern/python-monorepo-example/libs/fancy
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Found 4 source files
Setting pythonPath for service "base": "/Users/alastair/hack/extern/python-monorepo-example/libs/base/.venv/bin/python"
Loading pyproject.toml file at /Users/alastair/hack/extern/python-monorepo-example/libs/base/pyproject.toml
Assuming Python version 3.11
No include entries specified; assuming /Users/alastair/hack/extern/python-monorepo-example/libs/base
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Found 4 source files
Setting pythonPath for service "python-monorepo-example": "/Users/alastair/hack/extern/python-monorepo-example/libs/base/.venv/bin/python"
Loading pyproject.toml file at /Users/alastair/hack/extern/python-monorepo-example/pyproject.toml
Pyproject file "/Users/alastair/hack/extern/python-monorepo-example/pyproject.toml" is missing "[tool.pyright]" section.
Assuming Python version 3.11
Found 14 source files

Note specifically that the base venv is used for the fancy service. This is evident from the pythonPath setting.

Apologies if I have misunderstood something, but I believe this is a bug in lsp-pyright, and it should instead pick up the correct .venv directory for each workspace folder (a.k.a. service IIUC).

As an additional observation, I also attempted to get this to work by setting lsp-pyright-venv-directory and creating named venvs in that directory, with corresponding venv settings in each library's pyright.toml. This did not work either, as - again - the pythonPath was set to something completely different and incorrect, namely the system python instance.