fannheyward / coc-pyright

Pyright extension for coc.nvim
MIT License
1.29k stars 47 forks source link

coc-pyright using a ton of resources due to __pypackages__ directory #889

Closed dbragdonmw closed 1 year ago

dbragdonmw commented 1 year ago

Hello, I'm running into an issue where pyright is using way too many resource when editing files. Here's my resource usage after saving a file three times in a row:

image

What's the output of :CocCommand pyright.version

coc-pyright 1.1.293 with Pyright 1.1.294

What's the output of :CocCommand workspace.showOutput Pyright

Workspace: /home/dylan/repos/test-project
Using python from /usr/bin/python

[Info  - 12:55:04 PM] Pyright language server 1.1.294 starting
[Info  - 12:55:04 PM] Server root directory: /home/dylan/.config/coc/extensions/node_modules/coc-pyright/node_modules/pyright/dist/
[Info  - 12:55:04 PM] Starting service instance "test-project"
[Info  - 12:55:04 PM] Starting service instance "<default>"
[Info  - 12:55:04 PM] Setting pythonPath for service "test-project": "/usr/bin/python"
[Info  - 12:55:04 PM] Loading configuration file at /home/dylan/repos/test-project/pyrightconfig.json
[Info  - 12:55:04 PM] Assuming Python version 3.10
[Info  - 12:55:04 PM] Assuming Python platform Linux
[Info  - 12:55:04 PM] No include entries specified; assuming /home/dylan/repos/test-project
[Info  - 12:55:04 PM] Auto-excluding **/node_modules
[Info  - 12:55:04 PM] Auto-excluding **/__pycache__
[Info  - 12:55:04 PM] Auto-excluding **/.*
[Warn  - 12:55:04 PM] stubPath /home/dylan/repos/test-project/typings is not a valid directory.
[Info  - 12:55:04 PM] Searching for source files
[Info  - 12:55:04 PM] Found 4677 source files
[Info  - 12:55:04 PM] No pyproject.toml file found.
[Info  - 12:55:04 PM] Setting pythonPath for service "<default>": "/usr/bin/python"
[Warn  - 12:55:04 PM] stubPath typings is not a valid directory.
[Info  - 12:55:04 PM] Assuming Python version 3.10
[Info  - 12:55:04 PM] Assuming Python platform Linux
[Info  - 12:55:04 PM] Searching for source files
[Info  - 12:55:04 PM] No source files found.

Here is my coc-config.json:

{
    "python.pythonPath": "/usr/bin/python",
    "python.venvPath": "/home/dylan/python_envs/",
    "suggest.noselect": true,
    "python.linting.lintOnSave": true,
    "python.analysis.extraPaths": ["__pypackages__/3.10/lib"],
    "pyright.enable": true,
    "python.linting.enabled": true,
    "python.formatting.provider": "black",
    "python.analysis.autoImportCompletions": true,
    "pyright.inlayHints.variableTypes": false,  
    "python.analysis.typeCheckingMode": "basic",
    "pyright.testing.provider": "pytest",
    "coc.preferences.formatOnSaveFiletypes": ["json"]
} 

For some background, I am using PEP 582, meaning that my project's dependencies are contained in the __pypackages__ folder. I believe that pyright is scanning all of these files every time it starts linting. If I remove __pypackages__ from my working directory, the resource issue seems to go away. Likewise, if I remove "python.analysis.extraPaths": ["__pypackages__/3.10/lib"], from my coc-config.json, the problem doesn't occur. The problem though is that I do need this folder to be searched for auto import completion and such. If I removed the above line from my coc-config.json, then I get a warning like this from the linter:

[Pyright reportMissingImports] [E] Import "numpy" could not be resolved

since it is no longer checking the __pypackages__ directory to find the current project's dependencies.

Is there a setting I can add to my coc-config.json that ignores this folder when linting? Is there something else I can do to fix this issue? Any help would be appreciated.

I want to say too that I'm not sure if this is particularly a coc-pyright issue, more than it is a general pyright issue. I'm mostly curious if coc-pyright exposes a feature of pyright that ignores certain directories during the linting process.

Edit: Just tried adding the following to my pyrightconfig.json in the root of my project:

{
    "exclude": [
        "**/__pypackages__"
    ]
}

This did not fix the resource issue. I think pyright is still combing through the __pypackages__ folder, as stated in the pyright config documentation:

"Note that files in the exclude paths may still be included in the analysis if they are referenced (imported) by source files that are not excluded."

fannheyward commented 1 year ago

after saving a file three times in a row

Can you do a test with :noa w to do saving?

From your settings, you have enabled black formatter, coc-pyright will run black on saving. And I found python.linting.enabled in your coc-settings.json, but didn't find any linter enabled.

fannheyward commented 1 year ago

Try coc-pyright v1.1.296

dbragdonmw commented 1 year ago

Hi, thank you for your help. I updated to v1.1.296. To answer your first comment, I tried :noa w and it seems like that's stopping my CPU usage from spiking when saving, although I'm still getting decently high spikes here and there just from opening the file (around ~25%) although that might be unrelated.

As for my linting, I have this in my `.init.vim:

augroup black_on_save
    autocmd!
    autocmd BufWritePre *.py Black
    autocmd BufWritePre *.py Isort
    augroup end

It runs black and Isort for me, unrelated to the language server I'm using.

Now that I've updated, it seems like the resources aren't as strained. When I save multiple times in a row now I'm still getting around ~20% CPU usage, which isn't ideal, but much lower than 80%. My CPU is pretty powerful so I do think it's still somewhat of an issue that I'm even hitting 20% from saving a file in neovim. I also know that pyright is doing a lot in the background, and again I'm not even sure if this is a coc-pyright issue or just a pyright issue.

fannheyward commented 1 year ago

autocmd BufWritePre *.py Black

You run black with this autocmd, you should disable formatting by coc-pyright, set python.formatting.provider to none.