astral-sh / ruff-vscode

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

[VS Code extension] Ruff doesn't run on autosave #247

Closed alexdauenhauer closed 8 months ago

alexdauenhauer commented 1 year ago

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 hit cmd + 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)

"[python]": {
    "editor.codeActionsOnSave": {
        "source.fixAll": true,
        "source.organizeImports": true
    },
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.rulers": [
        100
    ]
},
"editor.formatOnSave": true,
"files.autoSave": "onFocusChange",
"isort.args": [
    "--force-single-line",
    "--lines-between-types=1",
    "--force-alphabetical-sort-within-sections",
    "--line-length=100"
],
"ruff.args": [
    "--line-length=100",
    "--isolated",
    "--extend-select=D403"
],
"ruff.organizeImports": false,
charliermarsh commented 1 year ago

Thanks @alexdauenhauer -- I'll take a look, I too would expect that to work.

charliermarsh commented 1 year ago

(I just moved this to the extension repo.)

alexdauenhauer commented 1 year ago

@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

charliermarsh commented 1 year ago

Do you have the import-sorting rules enabled? (Add --extend-select=I001 to your args, maybe?)

alexdauenhauer commented 1 year ago

when I add that to my settings, it appears to catch the error on autosave, but not fix it

image
johanneszellinger commented 1 year ago

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?

charliermarsh commented 1 year ago

@TheRealRolandDeschain - ruff.run only impacts when Ruff is re-run, not when Ruff attempts to autofix. Does that help clarify?

johanneszellinger commented 1 year ago

@TheRealRolandDeschain - ruff.run only impacts when Ruff is re-run, not when Ruff attempts to autofix. Does that help clarify?

It does, thank you.

blakeNaccarato commented 8 months ago

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.

More detail...

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.

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.

See a snippet of settings which works for me, currently... #### Partial contents of `.vscode/settings.json` or user `settings.json`: ```JSON { // ... "files.autoSave": "onFocusChange", // "files.autoSave": "afterDelay", // "files.autoSaveDelay": 100000, "[python]": { "editor.defaultFormatter": "charliermarsh.ruff" }, "[ipynb]": { "editor.defaultFormatter": "charliermarsh.ruff" }, "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.fixAll": "always", "source.organizeImports": "always" }, "notebook.formatOnCellExecution": true, "notebook.formatOnSave.enabled": true, "notebook.codeActionsOnSave": { "notebook.source.fixAll": "explicit", // I currently substitute `source.organizeImports` below for some confusing edge-case on my machine(s) "notebook.source.organizeImports": "explicit" }, // ... } ```
alexdauenhauer commented 8 months ago

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

blakeNaccarato commented 8 months ago

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 😅.

Ben-Epstein commented 6 months ago

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,
Bewinxed commented 5 months ago

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"

leerob commented 2 months ago

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,