emacs-lsp / lsp-pyright

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

Pyright sticks to the first virtual enviroment you open #66

Open axeoman opened 2 years ago

axeoman commented 2 years ago

I have multiple python projects with different virtual environments in <project_root>/venv directory. With a help of .dir-locals.el and projectile-project-root, whenever I open a file it will run pyvenv-activate to the right venv. I have pyrightproject.json with "venv": "venv" inside.

However, pyright is kinda "sticks" to the environment of the project that was first opened after emacs restart, e.g. if I open a file from project A and then open a file from project B: project B files will have unresolved library imports error because these libraries are not installed in the environment A.

What am I missing? Not sure if it is a bug or I just need to tweak lsp-pyright / pyrightproject.json more (I have tried using executionEnvironments there but behaviour was the same).

vakker commented 2 years ago

I'm having the exact same issue using pyenv-mode. Did you find a solution? After some digging and trying to manually set lsp-pyright-venv-path that is used in lsp-pyright-locate-python I still can't make it work.

It seems like there's only 1 workspace created, e.g. if I open a file then LSP reports that:

LSP :: pyright:972173 initialized successfully in folders: (/home/user/project1 /home/user/project2 /home/user/.pyenv)

then if I open another file from the other project:

LSP :: Connected to [pyright:972173]

Is that correct?

Here it says that the server will ask for configs for each workspace, but that doesn't seem to happen.

deifactor commented 2 years ago

As a workaround, if you set (setq lsp-pyright-multi-root nil) then it'll run separate processes. You need to set that before you (require 'lsp-pyright), and may need to lsp-workspace-folders-remove your existing folders. You can check whether this is working if lsp-describe-session shows different port numbers for the different pyright instances.

erikcw commented 8 months ago

I'm running into the same issue. Setting (setq lsp-pyright-multi-root nil) early in my config does not seem to help. I removed all workspace folders as started from scratch. Looks like each time I switch into a new project, it gets "appended" to the session from the first virtual environment.

vljosa-vivodyne commented 5 months ago

Hope this helps someone:

I don't fully understand why, but I found that I had to set the python.pythonPath setting of pyright correctly for each project (virtualenv):

(setq lsp-pyright-multi-root nil) ; Must be set before lsp-pyright is loaded. https://github.com/emacs-lsp/lsp-pyright/issues/19#issuecomment-930596080

(defvar lsp-client-settings (make-hash-table :test 'equal)) ; Original does not set :test, so values with string keys cannot be replaced.

(use-package lsp-pyright
  :ensure t
  :config
  (lsp-register-custom-settings
   '(("python.pythonPath" ljosa-lsp-pyright-locate-python)))

  (define-key lsp-mode-map (kbd "C-c C-l") lsp-command-map)
  :hook (python-mode . (lambda ()
                         (require 'lsp-pyright)
                          (lsp))))

I also had to make sure that projectile understood that the virutalenv were in diferent projects. (This was relevant because they were in the same git repo.) I did this by creating empty .projectile files in the root of each project.

After this, it still doesn't work at first: I have to do M-x lsp-reload-session the first time I open a file in a new project (virtualenv).