astral-sh / ruff-vscode

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

notebook.source.organizeImports setting does not seem to work when imports are below markdown cell #593

Open bolliger32 opened 2 months ago

bolliger32 commented 2 months ago

It took me a little while, but I finally figured out the use of notebook.source.fixAll and notebook.source.organizeImports settings (instead of their source.* counterparts) to prevent vscode from turning my import cells in jupyter notebooks into gibberish when using ruff as the formatter. However, it appears that when a notebook starts with a markdown cell (say, to title the notebook) and then has a python cell with imports after the markdown, notebook.source.organizeImports doesn't work. Here are my relevant vscode settings:

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

And here's the relevant part of my pyproject.toml file in the workspace open in vscode:

[tool.ruff.lint]
unfixable = ["F401"]
select = ["E4", "E7", "E9", "F", "NPY", "I"]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402"]

[tool.ruff.format]
docstring-code-format = true

I'm using ruff 0.6.2.

Side Note: If I use source.organizeImports instead of notebook.source.organizeImports, it "works" in the sense that something happens, but the effect is to introduce gibberish. Here's an example of the "gibberish" that occurs when using source.* instead of notebook.source.*. The first screenshot is from "pre-save", the second is after saving once, and the third after saving a second time

image image image
dhruvmanila commented 2 months ago

Huh, that is indeed the case. I wonder if it's a bug in VS Code, let me check if the editor is sending the request in the first place.

dhruvmanila commented 2 months ago

Hmm, VS Code isn't sending the code action request when a markdown cell is present. This is a bit worrying and could be related to the notebook selector filter here: https://github.com/astral-sh/ruff/blob/3e9c7adeeee639043686a1717a1683f6620efd2a/crates/ruff_server/src/server.rs#L335-L344

dhruvmanila commented 2 months ago

Let me look at this later today. Thanks for raising this issue!

bolliger32 commented 2 months ago

Thanks for taking a look so quickly!

dhruvmanila commented 2 months ago

This might be related to https://github.com/microsoft/vscode/issues/216653

Riezebos commented 1 month ago

@bolliger32 Thanks for mentioning "notebook.source.organizeImports" as a replacement for "source.organizeImports"! I had no idea this existed, and since a few weeks I've had a bunch of import cells in notebooks turn to gibberish as well. It was quite annoying as the Undo action would also not consistently restore the original version.

matt-dies-tenet3 commented 6 days ago

I commented on the potentially related issue as well, but I'm experiencing the same issue; manually saving kicks off the importing, but doesn't actually organize the imports and instead creates gibberish.

Important to note though, our setup uses the following, unlike the original poster:

"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": {
    "source.organizeImports": "explicit"
},

Unfortunately, the VSCode issue doesn't seemed to have gained any traction quite yet, but fingers crossed!