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

`lsp-pyright` has a very long initialization time when opening a python file, and Emacs often loses response even during use. #62

Closed hongyi-zhao closed 3 years ago

hongyi-zhao commented 3 years ago

When using Emacs for python development, I use the following configuration:

(use-package lsp-mode
  :hook (python-mode . lsp-deferred))

(use-package dap-mode
  :after lsp-mode
  :config
  (setq dap-python-debugger 'debugpy)
  (require 'dap-python))

;# To install pyright globally: 
;$ proxychains-ng-socks5 sudo npm install -g npm@latest
;#or
;$ proxychains-ng-socks5 sudo npm install -g pyright  
;#To update to the latest version: 
;$ proxychains-ng-socks5 sudo npm update -g pyright
(use-package lsp-pyright
  :hook (python-mode . (lambda ()
                        (require 'lsp-pyright)
                          (lsp-deferred))))  

But when I open a python file with Emacs, I always noticed a very long initialization time with the above configuration, and furthermore, Emacs often loses response even during use. Any hints for this problem?

Also see the relevant issue here.

Regards, HZ

yyoncho commented 3 years ago

make sure that your project root is the correct one. Use M-x lsp-describe-session

hongyi-zhao commented 3 years ago

How to know whether the project root directory is correct or not? For example, the following one:

image

yyoncho commented 3 years ago

Project root is the folder that contains the files that you want to work on. It is usually git repository. In your case you have selected your home folder which is wrong. As a result the language server is scanning all files in your home folder and that's why it is crashing.

Use M-x lsp-workspace-folders-add/remove to fix the project root.

hongyi-zhao commented 3 years ago

Can I only add a new workspace-folder without removing the old one, as shown below?

image

yyoncho commented 3 years ago

Can I only add a new workspace-folder without removing the old one, as shown below?

Is there anything preventing you from testing this on your side?

hongyi-zhao commented 3 years ago

Thank you, I reinstalled pyrigt as follows, according to the commented here:

$ yarn global add pyright
$ export PATH=$HOME/.yarn/bin:$PATH

Then I use pyvenv to automatically set the current workspace-folder as follows:

(use-package pyvenv
  :init
  (setenv "WORKON_HOME" "~/.pyenv/versions")
  ;;https://lists.gnu.org/archive/html/help-gnu-emacs/2021-09/msg00535.html
  (defun try/pyvenv-workon ()
    (when (buffer-file-name)
      (let* ((python-version ".python-version")
             (project-dir (locate-dominating-file (buffer-file-name) python-version)))
        (when project-dir
      (progn
        ;; https://github.com/emacs-lsp/lsp-pyright/issues/62#issuecomment-942845406
        (lsp-workspace-folders-add project-dir)
        (pyvenv-workon
             (with-temp-buffer
               (insert-file-contents (expand-file-name python-version project-dir))
               (car (split-string (buffer-string))))))
      ))))
  :config
  (pyvenv-mode 1)

  (setq pyvenv-post-activate-hooks
        (list (lambda ()
                (when (executable-find "ipython3")
                  (setq python-shell-interpreter "ipython3"
                        python-shell-interpreter-args "-i --matplotlib=inline --automagic --simple-prompt --pprint"
                        ;; https://gitlab.com/python-mode-devs/python-mode/-/issues/112#note_699461188
                        py-ipython-command "ipython3"
                        py-ipython-command-args '("-i" "--matplotlib=inline" "--automagic" "--simple-prompt" "--pprint")
                        )))))
  (setq pyvenv-post-deactivate-hooks
        (list (lambda ()
                (setq python-shell-interpreter "python3"))))
  )

The above method will fix the problem discussed here. See the info given by M-x lsp-describe-session:

image