emacs-lsp / lsp-pyright

lsp-mode :heart: pyright

Home Page:https://emacs-lsp.github.io/lsp-pyright

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for excluding paths

blarghmatey opened this issue · comments

If a venv is located inside a project directory then it can cause a long startup time for pyright due to it analyzing all of the contents of the third party packages. The configuration documentation for pyright shows that there is support for an exclude option to specify paths that should not be scanned for type information unless imports are made that trigger it. https://github.com/microsoft/pyright/blob/master/docs/configuration.md

Looking at the code for lsp-pyright it seems that it should be possible to expose a variable that gets added to lsp-register-custom-settings to manage that setting. https://github.com/emacs-lsp/lsp-pyright/blob/master/lsp-pyright.el#L199-L213

Okay I have faced the similar problem and the cause of the problem is lsp-file-watcher. You can reduce the startup time by simply putting (setq lsp-enable-file-watchers 'nil) in your config file. Moreover, it is fair to say that the lsp-file-watcher is terribly initialized. It has a variable called ls-file-watch-ignored-directories which controls the list of directoiry to watch. I used the pyrightconfig.json file's exclude list to manipulate the lsp-file-watch-ignored-directories in my config file. If I add all the unwanted directories to the exclude list in the pyrightconfig.json file then my startup time is negligible even with tramp.

The related elisp in my init file to achieve this:

  (add-hook 'lsp-mode-hook
	    (lambda ()
	      (if (file-exists-p (concat (lsp--workspace-root (cl-first (lsp-workspaces))) "/pyrightconfig.json"))
		   (progn
		   (setq lsp-enable-file-watchers t)
		   (setq lsp-file-watch-ignored-directories (eval (car (get 'lsp-file-watch-ignored-directories 'standard-value))))
		   (require 'json)
		   (let* ((json-object-type 'hash-table)
			      (json-array-type 'list)
			      (json-key-type 'string)
			      (json (json-read-file (concat (lsp--workspace-root (cl-first (lsp-workspaces))) "/pyrightconfig.json")))
			      (exclude (gethash "exclude" json)))
		   (dolist (exclud exclude)
		       (push exclud lsp-file-watch-ignored))))
		(setq lsp-enable-file-watchers 'nil)
		(setq lsp-file-watch-ignored-directories (eval (car (get 'lsp-file-watch-ignored-directories 'standard-value)))))
	      ))

My sample pyrightconfig.json file

{
  "exclude": [
      "./vox_crop",
      "./vox_crop_broken",
      "./extra_files"
  ],

    "stubPath": "/home/soumya/anaconda3/lib/python3.7/site-packages",
    "reportMissingImports": true,
    "reportMissingTypeStubs": true
}

Awesome, thank you for that. I'll give it a try 😄

Thanks for the snippet! solved my issue 😄