astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
1.1k stars 53 forks source link

Ruff server startup overwhelms CPU #637

Closed guidocioni closed 2 days ago

guidocioni commented 2 days ago

Whenever I open a notebook or python script the startup of ruff server uses about 70-90% of the CPU. If this spike would be limited to just 1-3 seconds that wouldn't really be an issue. However, this seems to take quite long (about 15 seconds), which makes vscode (and the entire laptop) quite unresponsive for that time. This becomes a bottleneck if I'm trying to open a large project or just one file after another, as multiple ruff server instance are spawned.

Is there any way to control the threads used by ruff server, or just reduce the load at startup time?

I don't have the most powerful CPU out there, but also not the "worst" one: i7-9750H on a Macbook Pro 15-inch of 2019. Shouldn't this machine be capable of handling the load on server startup without hogging?

dhruvmanila commented 2 days ago

Sorry that you're facing this issue. This could be related to either https://github.com/astral-sh/ruff/issues/11366 or https://github.com/astral-sh/ruff/issues/13686 but I want to make sure which one it is so I'll need some additional information to understand the root cause of this specific issue. Can you provide the following?

  1. Ruff extension version
  2. Debug logs for both the extension and the server. Refer to the troubleshooting guide. These logs will also include the ruff executable used and the version of it.
  3. What's the location of this notebook or Python script? Is it in a project or is it in a random directory on your file system?
  4. Is there a Ruff configuration (pyproject.toml / ruff.toml / .ruff.toml) that's being used for this file / notebook?
  5. Can you provide the settings that you've set for the Ruff extension? This would be the one starting with ruff. i.e., the ones mentioned in https://docs.astral.sh/ruff/editors/settings/.
guidocioni commented 2 days ago

Sorry that you're facing this issue. This could be related to either astral-sh/ruff#11366 or astral-sh/ruff#13686 but I want to make sure which one it is so I'll need some additional information to understand the root cause of this specific issue. Can you provide the following?

  1. Ruff extension version

v2024.52.0

  1. Debug logs for both the extension and the server. Refer to the troubleshooting guide. These logs will also include the ruff executable used and the version of it.
``` 2024-10-29 11:26:30.516 [info] Name: Ruff 2024-10-29 11:26:30.516 [info] Module: ruff 2024-10-29 11:26:30.516 [info] Python extension loading 2024-10-29 11:26:30.516 [info] Waiting for interpreter from python extension. 2024-10-29 11:26:30.516 [info] Python extension loaded 2024-10-29 11:26:30.516 [info] Using interpreter: /Users//miniconda3/envs/default/bin/python 2024-10-29 11:26:30.656 [info] Falling back to bundled executable: /Users//.vscode/extensions/charliermarsh.ruff-2024.52.0-darwin-x64/bundled/libs/bin/ruff 2024-10-29 11:26:30.687 [info] Resolved 'ruff.nativeServer: auto' to use the native server 2024-10-29 11:26:30.697 [info] Found Ruff 0.7.0 at /Users//.vscode/extensions/charliermarsh.ruff-2024.52.0-darwin-x64/bundled/libs/bin/ruff 2024-10-29 11:26:30.698 [info] Server run command: /Users//.vscode/extensions/charliermarsh.ruff-2024.52.0-darwin-x64/bundled/libs/bin/ruff server 2024-10-29 11:26:30.699 [info] Server: Start requested. 2024-10-29 11:26:30.732 [info] [Trace - 11:26:30] Sending request 'initialize - (0)'. 2024-10-29 11:26:30.748 [info] [Trace - 11:26:30] Received response 'initialize - (0)' in 17ms. 2024-10-29 11:26:30.749 [info] [Trace - 11:26:30] Sending notification 'initialized'. 2024-10-29 11:26:30.764 [info] [Trace - 11:26:30] Sending notification 'notebookDocument/didOpen'. 2024-10-29 11:26:30.773 [info] [Trace - 11:26:30] Sending request 'textDocument/codeAction - (1)'. 2024-10-29 11:26:30.774 [info] [Trace - 11:26:30] Sending request 'textDocument/codeAction - (2)'. 2024-10-29 11:26:30.775 [info] [Trace - 11:26:30] Sending request 'textDocument/codeAction - (3)'. 2024-10-29 11:26:30.779 [info] [Trace - 11:26:30] 0.000030130s WARN main ruff_server::server: No workspace(s) were provided during initialization. Using the current working directory as a default workspace... 2024-10-29 11:26:30.782 [info] [Trace - 11:26:30] 0.000545449s WARN main ruff_server::server: No workspace settings found for file:/// 2024-10-29 11:26:31.019 [info] [Trace - 11:26:31] Sending notification '$/setTrace'. 2024-10-29 11:26:31.431 [info] [Trace - 11:26:31] Sending notification '$/cancelRequest'. 2024-10-29 11:26:31.434 [info] [Trace - 11:26:31] Sending request 'textDocument/codeAction - (4)'. 2024-10-29 11:26:31.434 [info] [Trace - 11:26:31] Sending notification '$/cancelRequest'. 2024-10-29 11:26:31.435 [info] [Trace - 11:26:31] Sending request 'textDocument/codeAction - (5)'. 2024-10-29 11:26:31.435 [info] [Trace - 11:26:31] Sending notification '$/cancelRequest'. 2024-10-29 11:26:31.436 [info] [Trace - 11:26:31] Sending request 'textDocument/codeAction - (6)'. 2024-10-29 11:26:32.140 [info] [Trace - 11:26:32] Sending notification '$/cancelRequest'. 2024-10-29 11:26:32.141 [info] [Trace - 11:26:32] Sending request 'textDocument/codeAction - (7)'. 2024-10-29 11:26:32.141 [info] [Trace - 11:26:32] Sending notification '$/cancelRequest'. 2024-10-29 11:26:32.142 [info] [Trace - 11:26:32] Sending request 'textDocument/codeAction - (8)'. 2024-10-29 11:26:32.142 [info] [Trace - 11:26:32] Sending notification '$/cancelRequest'. 2024-10-29 11:26:32.143 [info] [Trace - 11:26:32] Sending request 'textDocument/codeAction - (9)'. 2024-10-29 11:26:34.480 [info] warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `Users//miniconda3/pkgs/cryptography-41.0.7-py311h48c7838_1/info/test/pyproject.toml`: - 'ignore' -> 'lint.ignore' - 'select' -> 'lint.select' - 'isort' -> 'lint.isort' 2024-10-29 11:26:40.279 [info] warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `Users//.Trash/glances/pyproject.toml`: - 'dummy-variable-rgx' -> 'lint.dummy-variable-rgx' - 'fixable' -> 'lint.fixable' - 'ignore' -> 'lint.ignore' - 'select' -> 'lint.select' - 'unfixable' -> 'lint.unfixable' - 'mccabe' -> 'lint.mccabe' 2024-10-29 11:26:41.278 [info] warning: The `ignore-init-module-imports` option is deprecated and will be removed in a future release. Ruff's handling of imports in `__init__.py` files has been improved (in preview) and unused imports will always be flagged. 2024-10-29 11:26:44.865 [info] warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `System/Volumes/Data/Users//miniconda3/pkgs/cryptography-41.0.7-py311h48c7838_1/info/test/pyproject.toml`: - 'ignore' -> 'lint.ignore' - 'select' -> 'lint.select' - 'isort' -> 'lint.isort' 2024-10-29 11:26:50.455 [info] warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `System/Volumes/Data/Users//.Trash/glances/pyproject.toml`: - 'dummy-variable-rgx' -> 'lint.dummy-variable-rgx' - 'fixable' -> 'lint.fixable' - 'ignore' -> 'lint.ignore' - 'select' -> 'lint.select' - 'unfixable' -> 'lint.unfixable' - 'mccabe' -> 'lint.mccabe' ```
  1. What's the location of this notebook or Python script? Is it in a project or is it in a random directory on your file system?

I'm not opening a project in this case, but instead a notebook that is located in /Users/<user>/Documents/folder/notebook.ipynb. However, I notice from the log that it is scanning a trashed python project to find a .toml file....why?!?!

  1. Is there a Ruff configuration (pyproject.toml / ruff.toml / .ruff.toml) that's being used for this file / notebook?

No

  1. Can you provide the settings that you've set for the Ruff extension? This would be the one starting with ruff. i.e., the ones mentioned in https://docs.astral.sh/ruff/editors/settings/.

No custom setting besides the one now used to produce the log files

"ruff.trace.server": "messages"
dhruvmanila commented 2 days ago

Thanks for providing all the information. I think this is related to https://github.com/astral-sh/ruff/issues/11366 and is fixed in 0.7.1. Can you try upgrading your Ruff version and check again?

dhruvmanila commented 2 days ago

Oh wait, you're using the bundled executable which is not yet upgraded to the latest version. I think this is an important bug fix that needs to go out in the bundled executable as well. I'll do that today.

guidocioni commented 2 days ago

Yeah I was about to ask how can I upgrade the bundled version of the server. In the meantime what would be a workaround? Install the server manually and change the settings?

dhruvmanila commented 2 days ago

Yes, you can install the ruff executable globally via pipx or uv tool install that should be then available on PATH. As you've no extension settings set, it should pickup the one from the environment i.e., PATH and that can be seen in the logs:

Here, the second line says that the extension is using the bundled executable

2024-10-29 11:26:30.516 [info] Using interpreter: /Users/<user>/miniconda3/envs/default/bin/python
2024-10-29 11:26:30.656 [info] Falling back to bundled executable: /Users/<user>/.vscode/extensions/charliermarsh.ruff-2024.52.0-darwin-x64/bundled/libs/bin/ruff
2024-10-29 11:26:30.687 [info] Resolved 'ruff.nativeServer: auto' to use the native server
2024-10-29 11:26:30.697 [info] Found Ruff 0.7.0 at /Users/<user>/.vscode/extensions/charliermarsh.ruff-2024.52.0-darwin-x64/bundled/libs/bin/ruff

While, here you can see that it uses the global ruff binary which I've installed using uv tool install:

2024-10-29 16:14:45.105 [info] Using interpreter: /Users/dhruv/playground/.venv/bin/python
2024-10-29 16:14:45.138 [info] Using the Ruff binary: /Users/dhruv/.local/bin/ruff
2024-10-29 16:14:45.495 [info] Found Ruff 0.7.1 at /Users/dhruv/.local/bin/ruff
2024-10-29 16:14:45.495 [info] Server run command: /Users/dhruv/.local/bin/ruff server
guidocioni commented 2 days ago

I've installed with conda instead, but it was picked automatically from the extension...and I can already see the speedup now! The issue does not seem to exist anymore :) Cannot believe it was so easy to fix it

dhruvmanila commented 2 days ago

Happy to hear that :)

Regardless, I'll be bumping the extension version now to provide the updated Ruff version.