Closed alexdauenhauer closed 8 months ago
Thanks @alexdauenhauer -- I'll take a look, I too would expect that to work.
(I just moved this to the extension repo.)
@charliermarsh ah ok great, I didn't know about this repo. One note, after further review I think maybe it was only the import sorting that wasn't working. I realized in my settings that I had that set to off for ruff. I disabled isort, and turned it on for ruff, but it still didn't organize the imports, but I did get some problems flagged so maybe it is partially running? not sure entirely
Do you have the import-sorting rules enabled? (Add --extend-select=I001
to your args
, maybe?)
when I add that to my settings, it appears to catch the error on autosave, but not fix it
Hi, sorry to hijack this thread for my question, but I was wondering about the following:
In the extension settings there is an option to "auto-fix" either onType
or onSave
. Changing this will add for example "ruff.run": "onSave",
to the settings.json. However neither of these does anything for me.
Ruff works however, and does the auto-fix with these settings:
{
"[python]": {
"editor.defaultFormatter": null,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
}
},
}
To me this is somewhat confusing tbh. My setup is rather simple with the pylance, black and ruff extensions installed and the following ruff.toml in the project root:
target-version = "py38"
select = [
"E", "W", # pycodestyle
"F", # pyflakes
"I", # isort
"N", # pep8-naming
]
[per-file-ignores]
"__init__.py" = ["E", "F", "I", "N"]
Is this working as intented?
@TheRealRolandDeschain - ruff.run
only impacts when Ruff is re-run, not when Ruff attempts to autofix. Does that help clarify?
@TheRealRolandDeschain -
ruff.run
only impacts when Ruff is re-run, not when Ruff attempts to autofix. Does that help clarify?
It does, thank you.
Parts of this may be actionable in the Ruff VSCode extension, but there are also upstream limitations to format on autosave. In short, you have to configure it just so, and you can't format on autosave of notebooks yet.
Sorry in advance for how much detail is needed to make sense of this presently. Anything surrounding the configuration of these things is in flux right now, as the API hasn't solidified (or only just) for extension developers and users alike, so there are still some quirks/surprises.
editor.codeActionsOnSave
set to "always"
, NOT true
(Code Actions on Save and Auto Save)?"always"
setting is not yet supported for Jupyter notebooks. Setting true
or "explicit"
in your notebook.codeActionsOnSave
will not trigger on autosave (Finalized notebook Code Action API)."always"
becomes an option for notebook code actions, and if Ruff still isn't formatting on autosave, then the ball is in Ruff's court.If you're running down leads on this yourself, beware that code actions used to run on window/focus change at some point, but this feature was removed (considered unintentional and surprising) before partial reimplementation in the form described above.
Since the https://github.com/astral-sh/ruff-lsp/pull/351 fix for https://github.com/astral-sh/ruff-lsp/issues/320, you may also use the notebook.source.fixAll
and notebook.source.organizeImports
code actions in your notebook.codeActionsOnSave
setting instead of source.fixAll
and source.organizeImports
actions, which changes the amount of context received by the language server to perform the intended actions. In my case, I still need to use source.organizeImports
instead of notebook.source.organizeImports
for some reason that I haven't nailed down yet.
hi @blakeNaccarato thanks for this. Yes this has been working now since the two PRs you mentioned were merged. I forgot about this issue so I'll close it now. Thanks
Great, thanks! Heads up, the Ruff VSCode extension readme still recommends the (just) deprecated true
setting rather than "always"
or "explicit"
, and if users use true
or "explicit"
, they won't get code actions on autosave, even for plain Python files. This should probably be mentioned in the readme, with the PR pointing to the existing https://github.com/astral-sh/ruff-vscode/issues/313. By how verbose my issue comments are, you would think I would have just opened a PR for that by now, but I haven't gotten around to it yet 😅.
If anyone is struggling on this and stumbled onto this issue in hopes of a quick fix, I got it to work with the following and its doing exactly what I want (formatting and sorting imports whenever i click save)
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
}
},
"ruff.lint.args": [
"--config=pyproject.toml"
],
"ruff.organizeImports": true,
"ruff.fixAll": true,
"editor.formatOnSave": true,
If anyone is struggling on this and stumbled onto this issue in hopes of a quick fix, I got it to work with the following and its doing exactly what I want (formatting and sorting imports whenever i click save)
"[python]": { "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.fixAll": "explicit", } }, "ruff.lint.args": [ "--config=pyproject.toml" ], "ruff.organizeImports": true, "ruff.fixAll": true, "editor.formatOnSave": true,
THANK YOU, But I had to add this in order for it to work! (Maybe because I have subdirectories in svelte?)
"editor.defaultFormatter": "charliermarsh.ruff"
Since I'm running Python and JS projects, I need to override the default formatter explicitly for VSCode away from Prettier.
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
}
},
"ruff.fixAll": true,
"ruff.organizeImports": true,
Currently with the
"files.autoSave": "onFocusChange"
setting, when I switch tabs or windows, the file I was on is saved. This autosave triggers the black formatter, but does not trigger ruff code actions. I have to manually hitcmd + s
to trigger ruff. Am I missing a setting or misunderstanding how this should work? What I would like is for the autosave to also trigger ruff.relevant settings (I think this would be all of them)