Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faulted to apply Ruff formatting to the document #300

Closed
berceanu opened this issue Oct 16, 2023 · 14 comments · Fixed by astral-sh/ruff-lsp#276
Closed

Faulted to apply Ruff formatting to the document #300

berceanu opened this issue Oct 16, 2023 · 14 comments · Fixed by astral-sh/ruff-lsp#276
Assignees

Comments

@berceanu
Copy link

Screenshot 2023-10-16 at 17 30 30
@zanieb
Copy link
Member

zanieb commented Oct 16, 2023

Hi!

Can you please attach the contents of the file for us to test with?

@berceanu
Copy link
Author

System info:

Visual Studio Code
Version: 1.83.1
Commit: f1b07bd25dfad64b0167beb15359ae573aecd2cc
Date: 2023-10-10T23:57:32.750Z (5 days ago)
Browser: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Code/1.83.1 Chrome/114.0.5735.289 Electron/25.8.4 Safari/537.36

Ruff v2023.40.0

@berceanu
Copy link
Author

Hi!

Can you please attach the contents of the file for us to test with?

I tried various files in that repo, for instance:

https://github.com/berceanu/prepic/blob/2f1afe1931069da02494e662955b5c4ac785fd47/prepic/simulation.py#L1

@berceanu
Copy link
Author

Perhaps the unicode symbols bother ruff?

@berceanu berceanu changed the title Ruff failed to format document Faulted to apply Ruff formatting to the document Oct 16, 2023
@zanieb
Copy link
Member

zanieb commented Oct 16, 2023

Hm ruff format works fine for me with ruff 0.0.292. Can you upgrade Ruff and see if it's resolved? pip install --upgrade ruff

@berceanu
Copy link
Author

Doesn't the vscode extension ship with a version of ruff?

The extension ships with ruff==0.0.292.

@zanieb
Copy link
Member

zanieb commented Oct 16, 2023

It does, but I wasn't sure what version yours shipped with — we will use your system version if available. If it's the latest version though, then I'm pretty confused by this error. The given file also formats fine for me in VSCode. Can you open the logs for the Ruff extension?

@berceanu
Copy link
Author

Well, I don't have a locally installed version on my system. How do I open the logs?

@zanieb
Copy link
Member

zanieb commented Oct 16, 2023

The VSCode terminal has output logs for extensions
Screenshot 2023-10-16 at 10 16 09

@berceanu
Copy link
Author

2023-10-16 18:23:57.905 [info] [Trace - 18:23:57] Received response 'workspace/executeCommand - (32)' in 5ms. Request failed: KeyError: 'ruff.applyFormat' (-32602).
2023-10-16 18:23:58.877 [info] [Trace - 18:23:58] Sending request 'textDocument/hover - (33)'.
2023-10-16 18:23:58.880 [info] [Trace - 18:23:58] Received response 'textDocument/hover - (33)' in 2ms.
2023-10-16 18:23:59.295 [info] [Trace - 18:23:59] Sending request 'textDocument/hover - (34)'.
2023-10-16 18:23:59.300 [info] [Trace - 18:23:59] Received response 'textDocument/hover - (34)' in 5ms.
2023-10-16 18:24:33.572 [info] [Trace - 18:24:33] Sending request 'textDocument/codeAction - (35)'.
2023-10-16 18:24:33.578 [info] [Trace - 18:24:33] Received response 'textDocument/codeAction - (35)' in 6ms.
2023-10-16 18:24:36.499 [info] [Trace - 18:24:36] Sending request 'workspace/executeCommand - (36)'.
2023-10-16 18:24:36.501 [info] 2023-10-16 18:24:36,500 ERROR Failed to handle request 36 workspace/executeCommand ExecuteCommandParams(command='ruff.applyFormat', arguments=[{'uri': 'file:///Users/berceanu/Development/prepic/prepic/simulation.py', 'version': 1}], work_done_token=None)
Traceback (most recent call last):
  File "/Users/berceanu/.vscode/extensions/charliermarsh.ruff-2023.40.0-darwin-arm64/bundled/libs/pygls/protocol.py", line 338, in _handle_request
    handler(params, msg_id)
  File "/Users/berceanu/.vscode/extensions/charliermarsh.ruff-2023.40.0-darwin-arm64/bundled/libs/pygls/protocol.py", line 83, in decorator
    ret_val = base_func(self, *args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/berceanu/.vscode/extensions/charliermarsh.ruff-2023.40.0-darwin-arm64/bundled/libs/pygls/protocol.py", line 800, in lsp_workspace__execute_command
    cmd_handler = self.fm.commands[params.command]
                  ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'ruff.applyFormat'

2023-10-16 18:24:36.502 [info] [Trace - 18:24:36] Received response 'workspace/executeCommand - (36)' in 3ms. Request failed: KeyError: 'ruff.applyFormat' (-32602).

@charliermarsh
Copy link
Member

Do you have ruff.enableExperimentalFormatter set in your settings.json?

@berceanu
Copy link
Author

Nope, I only have:

    "extensions.experimental.affinity": {
        "asvetliakov.vscode-neovim": 1
    }

@charliermarsh
Copy link
Member

The formatter is in alpha -- you need to opt into it for now: https://github.com/astral-sh/ruff/blob/main/crates/ruff_python_formatter/README.md#vs-code. Though I'll fix this specific error in the next release to avoid confusion.

charliermarsh added a commit to astral-sh/ruff-lsp that referenced this issue Oct 16, 2023
## Summary

We always expose this on the client side, so doing so conditionally
within the server can lead to invalid command errors. This action has to
be triggered explicitly by the user, so it seems fine to expose it even
if Ruff doesn't register itself as a formatter.

Closes astral-sh/ruff-vscode#300.
@berceanu
Copy link
Author

Thanks, I confirm that setting

    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.formatOnSave": true,
    }

fixed my issue.

azurelotus0926 added a commit to azurelotus0926/ruff-lsp that referenced this issue Jun 27, 2024
## Summary

We always expose this on the client side, so doing so conditionally
within the server can lead to invalid command errors. This action has to
be triggered explicitly by the user, so it seems fine to expose it even
if Ruff doesn't register itself as a formatter.

Closes astral-sh/ruff-vscode#300.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants