vscode-ruff icon indicating copy to clipboard operation
vscode-ruff copied to clipboard

formatOnSave not working on notebook

Open JordanHeintz opened this issue 1 year ago • 8 comments

Description

As far as I can tell, I have applied the necessary configuration to use Ruff to format notebooks on save, but it just will not do anything. It does work on regular python files just fine.

Environment

Windows 10 Python: 3.12.4 VSCode Ruff Extension: v2024.36.0 (includes ruff 0.5.4)

I have the following in my settings.json:

{
    "notebook.formatOnSave.enabled": true,
    "notebook.defaultFormatter": "charliermarsh.ruff",
    "notebook.codeActionsOnSave": {
        "notebook.source.fixAll": "explicit",
        "notebook.source.organizeImports": "explicit",
    },
    "[python]": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.codeActionsOnSave": {
            "source.fixAll": "explicit",
            "source.organizeImports": "explicit",
        }
    },
}

I added a ruff.toml file to my workspace with the following setting:

[tool.ruff]
extend-include = ["*.ipynb"]

IIUC, the extension should find that file automatically, but I also tried specifying a path to ruff.toml in the extension settings, with no effect.

Expected Results

I expected Ruff to format my .ipynb files on save the same it does my .py files.

Actual Results

Nothing. VSCode Output shows the extension loading, resolving ruff.nativeServer: auto to use the native server, and a request to start the server, with no indications that any errors occur.

JordanHeintz avatar Aug 05 '24 20:08 JordanHeintz

It could be that there are multiple formatters available for Python files. Can you try running the "Notebook: Format Notebook" command verify whether that works? Does VS Code provide a dialog like the following?

Screenshot 2024-08-06 at 12 16 51

If so, you'd need to configure a default formatter for Python files:

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

dhruvmanila avatar Aug 06 '24 06:08 dhruvmanila

I don't seem to have a "Notebook: Format Notebook" command in my command palette. However, that is an option in the right-click menu when I have a notebook open, and it doesn't seem to do anything.

I do already have "editor.defaultFormatter" set in the code block I posted above. Did I do something wrong there?

JordanHeintz avatar Aug 06 '24 15:08 JordanHeintz

Strange. Does formatting regular python files work as expected?

MichaReiser avatar Aug 06 '24 16:08 MichaReiser

Yes, formatting regular Python files works perfectly.

JordanHeintz avatar Aug 06 '24 17:08 JordanHeintz

I added a ruff.toml file to my workspace with the following setting:

Do you have any other config in your workspace? For instance, is there a pyproject.toml as well with [tool.ruff] section in it? If it's there, can you make sure there's only one config (either pyproject.toml or ruff.toml)?

I don't seem to have a "Notebook: Format Notebook" command in my command palette.

This command will only be available when a Jupyter Notebook is opened in the editor.

So, your config looks correct to me and it's working fine on my machine.

dhruvmanila avatar Aug 07 '24 02:08 dhruvmanila

I too am having this issue with the Ruff native server but not with ruff-lsp.

msongtw avatar Aug 07 '24 15:08 msongtw

Do you have any other config in your workspace? For instance, is there a pyproject.toml as well with [tool.ruff] section in it? If it's there, can you make sure there's only one config (either pyproject.toml or ruff.toml)?

Nope, no other configs in that workspace.

This command will only be available when a Jupyter Notebook is opened in the editor.

Ah, thanks for that tip. No, that command doesn't seem to do anything either.


Based on @msongtw's comment above, I tried forcing ruff-lsp, and it did indeed work correctly. I had to delete my ruff.toml file as ruff-lsp throws a parsing error (unknown field 'tool'), but made no other changes. Ruff-lsp works with my settings.json, native server does not.

JordanHeintz avatar Aug 07 '24 16:08 JordanHeintz

Oh ok, I see the problem. The ruff.toml file does not need the [tool.ruff] header, it's only required in pyproject.toml file. You can see that we provide examples for how a setting should be added in both files at https://docs.astral.sh/ruff/settings/#extend-include. So, if your file is ruff.toml, then it should just be:

extend-include = ["*.ipynb"]

Can you try this?

dhruvmanila avatar Aug 08 '24 06:08 dhruvmanila

Yep, that was it, thank you. I must've read through that doc a half dozen times, and I missed that detail each time, sorry! Appreciate the responsiveness!

JordanHeintz avatar Aug 08 '24 14:08 JordanHeintz