astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
946 stars 45 forks source link

Ruff takes too long to save in VS Code #428

Closed tsantor closed 3 months ago

tsantor commented 3 months ago

While I love ruff, I had to go back to Microsoft's own extensions for black, pylint and isort. Once I disabled those and tried to go full on ruff each time I save a python file it takes at least 5-6 seconds to save with:

Saving 'filename.py': Getting code actions from ''Ruff', ' (configure).
charliermarsh commented 3 months ago

Hmm, I've never heard of this happening before. Would love to figure it out. Is there any other logging you could share when you open the pane?

Screenshot 2024-03-16 at 10 17 24 AM

Screenshot 2024-03-16 at 10 17 28 AM

dhruvmanila commented 3 months ago

If you're not seeing the "Ruff" button on the toolbar, you can do so like this:

Screenshot 2024-03-19 at 15 02 09

Or, click on the "Open logs" link in the "Ruff" section.

tsantor commented 3 months ago

Well the ruff logs pointed out the issue. It was looking for ruff which was not in the virtualenv. Unlike Microsoft's extensions for black, pylint, and isort, ruff does not ship with the package itself. Installed ruff problem solved!

I was using it as a pre-commit hook so that was ultimately taking care of it, but I also wanted ruff to run on file save which now I have. Now I can say goodbye to black, pylint, and isort.

dhruvmanila commented 3 months ago

Well the ruff logs pointed out the issue. It was looking for ruff which was not in the virtualenv. Unlike Microsoft's extensions for black, pylint, and isort, ruff does not ship with the package itself. Installed ruff problem solved!

@tsantor Can you share any relevant logs? Because we do ship with the bundled executable as part of the extension.

tsantor commented 3 months ago

Sure, here you go:

2024-03-20 01:14:25.461 [info]   Message: subprocess.CalledProcessError: Command '['/Users/tsantor/.pyenv/shims/ruff', '--version']' returned non-zero exit status 127.
  Code: -32603 
[object Object]
2024-03-20 01:14:25.492 [info] Interpreter executable (/Users/tsantor/.pyenv/versions/3.9.11/envs/xqrcode_env/bin/ruff) not found
2024-03-20 01:14:25.492 [info] Using environment executable: /Users/tsantor/.pyenv/shims/ruff
2024-03-20 01:14:25.692 [info] pyenv: ruff: command not found
...
2024-03-20 01:14:30.356 [info] 2024-03-20 01:14:30,356 ERROR Exception occurred in notification: "subprocess.CalledProcessError: Command '['/Users/tsantor/.pyenv/shims/ruff', '--version']' returned non-zero exit status 127."
Traceback (most recent call last):
  File "/Users/tsantor/.vscode/extensions/charliermarsh.ruff-2024.16.0-darwin-arm64/bundled/libs/pygls/protocol/json_rpc.py", line 159, in _execute_notification_callback
    raise future.exception()
  File "/Users/tsantor/.vscode/extensions/charliermarsh.ruff-2024.16.0-darwin-arm64/bundled/libs/ruff_lsp/server.py", line 480, in did_save
    diagnostics = await _lint_document_impl(document, settings)
  File "/Users/tsantor/.vscode/extensions/charliermarsh.ruff-2024.16.0-darwin-arm64/bundled/libs/ruff_lsp/server.py", line 605, in _lint_document_impl
    result = await _run_check_on_document(document, settings)
  File "/Users/tsantor/.vscode/extensions/charliermarsh.ruff-2024.16.0-darwin-arm64/bundled/libs/ruff_lsp/server.py", line 1843, in _run_check_on_document
    executable = _find_ruff_binary(settings, VERSION_REQUIREMENT_LINTER)
  File "/Users/tsantor/.vscode/extensions/charliermarsh.ruff-2024.16.0-darwin-arm64/bundled/libs/ruff_lsp/server.py", line 1750, in _find_ruff_binary
    version = _executable_version(path)
  File "/Users/tsantor/.vscode/extensions/charliermarsh.ruff-2024.16.0-darwin-arm64/bundled/libs/ruff_lsp/server.py", line 1825, in _executable_version
    version = utils.version(executable)
  File "/Users/tsantor/.vscode/extensions/charliermarsh.ruff-2024.16.0-darwin-arm64/bundled/libs/ruff_lsp/utils.py", line 96, in version
    output = subprocess.check_output([executable, "--version"]).decode().strip()
  File "/Users/tsantor/.pyenv/versions/3.9.11/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/Users/tsantor/.pyenv/versions/3.9.11/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/Users/tsantor/.pyenv/shims/ruff', '--version']' returned non-zero exit status 127.

2024-03-20 01:14:30.359 [info] 2024-03-20 01:14:30,359 WARNING Cancel notification for unknown message id "1426"

As soon as I manually install ruff into my environment it goes away.

dhruvmanila commented 3 months ago

Thanks for providing the logs! I think it's working as intended. So, what's happening is:

  1. Check for Ruff executable in the current interpreter, doesn't exists

    Interpreter executable (/Users/tsantor/.pyenv/versions/3.9.11/envs/xqrcode_env/bin/ruff) not found

  2. Check for Ruff executable from the environment, exists:

    Using environment executable: /Users/tsantor/.pyenv/shims/ruff

  3. We want to get the version of that Ruff executable and here's where the failure happens

The way pyenv shims work is that they're just a small script to pass the arguments to an actual command. Here, your project is configured with a specific environment "xqrcode_env". This means that pyenv will look for a ruff executable in the bin directory of that environment but it doesn't find it as evident by the logs:

2024-03-20 01:14:25.692 [info] pyenv: ruff: command not found

This means that the Ruff server fails to start because the command failed. The reason the shim exists in the first place is because you might have installed Ruff in the global environment (pyenv global).

This is where we check for the Ruff version: https://github.com/astral-sh/ruff-lsp/blob/187d7790be0783b9ac41ce025a724cf389bf575c/ruff_lsp/utils.py#L94-L98

Hope this is helpful :)

mbmccoy commented 2 months ago

Related, and ruff does seem to be a common thread: https://github.com/microsoft/vscode/issues/209789 https://github.com/microsoft/vscode/issues/204648

dhruvmanila commented 2 months ago

@mbmccoy Do you think it's an issue with Ruff's VS Code extension? If so, can you open a new issue with any relevant details and we can discuss there.