-
Notifications
You must be signed in to change notification settings - Fork 46
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
Broken "Fix All" VSCode code action for Notebooks #320
Comments
In case anyone else sees this the following configuration works
|
I think this is a problem. |
Do you have an idea why it is happening? Is it because the LSP only sees a single cell and doesn't know about the entire document or is this also an issue when running ruff (without the LSP)? |
Yes, sorry I discussed this on Discord. To give context, this is happening because the "notebook.codeActionsOnSave" sends the code action request for each cell. Now, this is a source level code action which means that our server invokes the Ruff command, collects the diagnostics and applies any necessary fixes. But, this means that it doesn't have context from other cells. So, if first cell has This is only for the builtin command |
Hi @dhruvmanila, I know you already responded to my comment astral-sh/ruff-vscode#256 (comment) over in astral-sh/ruff-vscode#256, but I'll mention it again here in case it helps somehow. I see you've discussed notebook/cell tradeoffs over in #264 (comment), so I hope I'm not doubling-down on irrelevant information here, I promise I won't bring it up again 😅 The "notebook.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit",
}, And that seems to operate on a per-cell basis. If the Caution These settings are Possible future
|
Experiencing the same 🙏 |
Interesting, @dhruvmanila can we use these |
Adding the notebook prefix giving "notebook.source.fixAll.ruff" solved the import deletion problem for me. |
Caution @Karl60 your fix may be a side-effect of rearranging your If Ruff doesn't register that fixer, and have it hooked up to code that actually handles that case, then using that setting will have no effect. Obscuring things further, you also won't be warned by VSCode about this, as it does not validate entries in I know it's a bit of a game of telephone here, I've edited/strengthened my disclaimer in #320 (comment) that Another clarification (not to your point, but generally), I hope that clears things up! I hope my tone isn't appearing too pedantic here, I'm just trying to be hyper-explicit to avoid the proliferation of a setting that would enable a proposed Ruff VSCode API that doesn't exist yet. |
Thanks @blakeNaccarato, it's really helpful! @dhruvmanila is the best person to implement this and he's taking some well-earned vacation. I may be able to get to it before he gets back, but otherwise it'll be tackled as soon as he can :) |
@karthiknadig - do you know if the |
Actually, maybe I'm misunderstanding, since I only see that in the VS Code API reference and not the LSP spec. |
Code actions can be custom, the "notebook" namespace is one of the custom ones. This should work with the current |
Okay, I believe I know how to fix this. We need to register handlers for the |
## Summary VS Code added support for `notebook.*`-scoped code actions (https://code.visualstudio.com/api/references/vscode-api#CodeActionKind), which are intended to run over the entire document, unlike the actions that omit the `notebook.` namespace, which instead only operate over individual cells. The latter are problematic for actions that need global context, e.g., our unused imports rules. When the `notebook.*` actions are triggered, VS Code sends down the first cell (see: microsoft/vscode#193120) as the URI. This PR adds handlers for the actions and logic to use the notebook, rather than the cell. Closes #320. ## Test Plan Used the following `settings.json`: ```json { "[python]": { "editor.defaultFormatter": "charliermarsh.ruff", "editor.codeActionsOnSave": { "source.organizeImports.ruff": true } }, "notebook.codeActionsOnSave": { "notebook.source.fixAll": true } } ``` Verified that imports used across cells were not removed (whereas `"source.fixAll": true` _did_ cause imports to be removed).
This fixes the on-save actions but unfortunately running "Fix all" from the command palette will still have the same problem, since that runs |
@Yoyokrazy might have more details on the command pallet for |
Preface: I'm putting this here since it's still a fresh fix and all the relevant parties are already subscribed, but if you want this raised in a separate issue I'll do that as well. What a quick turnaround! I took this for a hacky test run, and However, it looks like In other words, the following "notebook.codeActionsOnSave": {
"notebook.source.fixAll": "explicit",
"source.organizeImports": "explicit"
}, While this only fixes errors, but does not organize imports: Caution
However, if the former setup works, and the latter doesn't, then maybe it's just the case that guidance should be for
I think this means There are a few situations where users have resorted to passing a list of options to the Here's more detail in my relevant `settings.json` snippet from that repo{
//* ruff
"ruff.importStrategy": "fromEnvironment", // Tried with and without this set
"ruff.format.args": [], // These explicitly clear out possible user settings overrides
"ruff.lint.args": [],
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
},
"[ipynb]": {
"editor.defaultFormatter": "charliermarsh.ruff"
},
//* Formatting
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file",
"editor.formatOnType": false,
"editor.codeActionsOnSave": {
"source.fixAll": "always",
"source.organizeImports": "always"
},
//* Notebook
//? https://github.com/microsoft/vscode/issues/195223#issuecomment-1800137313
"notebook.insertFinalNewline": false,
//? Code actions on notebook save partially work now
//? Need to raise an issue about `notebook.source.organizeImports` not behaving
//? https://github.com/astral-sh/ruff-lsp/issues/320
"notebook.formatOnCellExecution": true,
"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": {
"notebook.source.fixAll": "explicit",
"notebook.source.organizeImports": "explicit"
},
} |
Hmm, I do see the correct behavior on my end from |
That's good news. Okay I'll play with it and if I manage to nail down a minimal/reproducible situation, I'll raise a separate issue about it specifically whenever I have some actionable information. If I manage to fix my setup in the process, then it's a win-win. Thanks for the release, it definitely helps to get the code out there in the sunlight! The on save behavior is already more stable now even with my half-measure configuration. |
Definitely, and thanks for all your help! If it's not working for you, there's probably still more to investigate... (Did you try using |
Actually yes, I did try It seems that tool interactions (between e.g. Pylance, Sourcery, Ruff), especially when it comes to code actions, contribute to most of the uncertainty across dev environments. I'll probably start from a no frills Ruff dev environment and gradually incorporate my tools until something breaks. In any case, I won't ping this issue again about it. Just joined the Ruff Discord, so I'll chat there with any half-baked ideas 😅. |
To reproduce:
Python version: 3.11.1
Ruff version: 0.1.5
Ruff-vscode version: v2023.46.0
VScode settings.json:
Any notebook will do. E.g. in cell 1:
In cell 2:
Saving this notebook will cause
from pathlib import Path
to be deleted from the first cell. Commenting out"source.fixAll": true,
in the notebook.codeActionsOnSave bit of settings.json stops this but obviously also turns off Ruff's functionality.It's possible I've done something wrong or this is expected behaviour but just flagging it in case it's useful. Really amazing how much you've been shipping lately!
The text was updated successfully, but these errors were encountered: