Skip to content
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

Support for disabling specific rules from auto-fixing on save #260

Open
foucdeg opened this issue Jul 28, 2023 · 17 comments
Open

Support for disabling specific rules from auto-fixing on save #260

foucdeg opened this issue Jul 28, 2023 · 17 comments
Labels
code-actions Related to code actions configuration Related to settings and configuration enhancement New feature or request

Comments

@foucdeg
Copy link

foucdeg commented Jul 28, 2023

When coding, it is annoying that our temporary imports and variables that we haven't yet used are removed every time we save the file.

We'd like the Ruff extension to auto-fix on save in general (and we have that activated using editor.codeActionsOnSave), but not F401 (unused imports) and F841 (unused variables).

We do want the rule to autofix in other contexts (pre-commit hook, CI). Is there a way to do that?

@phillies
Copy link

phillies commented Aug 4, 2023

I totally agree. Especially once #247 is fixed the automatic fix of F401 and F841 might lead to confusion and annoyance. I guess we would need 2 configuration sets, one for creating the warnings/highlights and one for fixing.

I would propose maybe something like this

    "ruff.args": [
        "--line-length=97",
        "--select=C,E,F,W,B,E501,I001",
        "--extend-ignore=E203"
    ],
    "ruff.fixAll.args": [
        "--line-length=97",
        "--select=C,E,F,W,B,E501,I001",
        "--extend-ignore=E203,F841,F401"
    ],

@francesco086
Copy link

I'm also interested as F401 drives me nut. I had to disable it altogether for now.

@albertotb
Copy link

This feature would be awesome, I have to keep re-typing imports whenever I comment out parts of the code while developing.

@charliermarsh
Copy link
Member

Totally hear you. I should note that you can disable the fix for F401 entirely via:

    "ruff.args": [
        "--unfixable=F401"
    ],

This'll prevent Ruff from removing unused imports on save. But it'll also prevent Ruff from removing unused imports when you run "Fix all" and such, so it's not a perfect solution.

@aneeshusa
Copy link

Just discussed this with Charlie live at PyCon - we currently have "ruff.lint.args": ["--ignore=F401"] in VSCode workspace settings to set the default for our org, but not ruff.toml so this is still enforced in CI. We would like to be able to "opt-in" to removing unused imports in VSCode, some ideas we discussed:

  • VSCode user settings don't work as workspace settings take precedence
  • Having an explicit VSCode action for this - either as part of Organize Imports or a standalone "Remove unused imports" action
  • Making F401 only run when saving a file, but not while just editing (not sure if this is too aggressive)

@T-256
Copy link

T-256 commented May 24, 2024

@aneeshusa Does #457 solve it? you can set new ruff.configurationPreference in your VSCode settings.

@Cito
Copy link

Cito commented Jul 2, 2024

Note that in newer versions of ruff you need to set

    "ruff.lint.args": [
        "--unfixable=F401"
    ],

@amaurygelin
Copy link

amaurygelin commented Jul 18, 2024

Note that in newer versions of ruff you need to set

    "ruff.lint.args": [
        "--unfixable=F401"
    ],

Hello, it does not work for me, unused imports are still deleted.
Here is my settings.json:

"[python]": {
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll": "explicit",
    "source.organizeImports": "explicit"
  },
  "editor.defaultFormatter": "charliermarsh.ruff"
},
"ruff.nativeServer": true,
"ruff.lint.args": [
  "--unfixable=F401"
],

@dhruvmanila
Copy link
Member

@amaurygelin The ruff.lint.args setting isn't used by the native server (you've set ruff.nativeServer to true). For the native server, there are multiple options based on your use-case:

  1. You can use a local configuration file (pyproject.toml) which will be picked up by Ruff
  2. Use a global configuration file and define it in ruff.configuration

@T-256
Copy link

T-256 commented Jul 23, 2024

We do want the rule to autofix in other contexts (pre-commit hook, CI). Is there a way to do that?

2. Use a global configuration file and define it in ruff.configuration

Could we make it to accept json values?
It'd be easier for user than creating files manually and pass cross-platform aware paths.

"ruff.nativeServer": true,
"ruff.configuration": {
   "unfixable": ["F401", "F801"]
}

we can either drop string value support for ruff.configuration in favor of {"extend": "path/to/ruff.toml"} value, Or we can separate this setting.

I believe this setting also helps to:

@dhruvmanila
Copy link
Member

Could we make it to accept json values?
It'd be easier for user than creating files manually and pass cross-platform aware paths.

We considered this for the configuration proposal but decided against it for now because:

  1. We want to provide auto-completion, which requires including the entire schema in package.json.
  2. Compatibility with different Ruff versions is a challenge. Shipping a schema tied to a specific extension version could limit flexibility since we allow users to use a specific Ruff version with ruff.path.

There's an option to allow any value to pass through to Ruff, leaving validation to the executable. However, this might result in a less optimal user experience due to the lack of auto-completion and delayed feedback on settings correctness.

@dhruvmanila
Copy link
Member

I'm interested in supporting this and have created an issue with more details at astral-sh/ruff#12709. If anyone wants to work on this, feel free to ping me / comment on the Ruff issue.

@T-256
Copy link

T-256 commented Feb 27, 2025

Does #702 solve this issue?

@T-256
Copy link

T-256 commented Feb 27, 2025

There's an option to allow any value to pass through to Ruff, leaving validation to the executable.

Do you mean Ruff Extension can send lint diagnostics for settings.json? Cool, it's better than nothing
It can get diagnostics by validating passed value to ruff.configuaretion via ruff config.

However, this might result in a less optimal user experience due to the lack of auto-completion and delayed feedback on settings correctness.

It needs investigation to check is there any other extensions that implemented auto-completion for settings.json. I think it makes sense at least provided a way from VSCode to let extension set auto-completion values dynamically during runtime instead of specifying statically in package.json.

@dhruvmanila
Copy link
Member

Does #702 solve this issue?

That's a good point. I think it can partially resolve it i.e., a user can configure Ruff in an editor context to mark certain rules as unfixable which will then be considered when applying code actions on save. But, it'll also mean that those won't show up in the lightbulb menu nor as a "Quick fix" in the diagnostic popup.

Consider this example config:

{
  "ruff.configuration": {
    "lint": {
      "unfixable": ["F541"]
    }
  }
}

So, I think it's still useful to provide that feature so that the user still have the option to fix it if they want to.

Do you mean Ruff Extension can send lint diagnostics for settings.json? Cool, it's better than nothing
It can get diagnostics by validating passed value to ruff.configuaretion via ruff config.

No, I meant what we've implemented in astral-sh/ruff#16296 which is that ruff.configuration will accept an object that represents the Ruff config which will then be passed onto the chosen ruff executable. The validation is performed on the server side which will notify the user if it fails.

It needs investigation to check is there any other extensions that implemented auto-completion for settings.json. I think it makes sense at least provided a way from VSCode to let extension set auto-completion values dynamically during runtime instead of specifying statically in package.json.

Yeah, I don't know if that's even possible because the extension would need to provide suggestion based on the selected ruff executable which is might not even be known while the user is editing settings.json.

@T-256
Copy link

T-256 commented Mar 1, 2025

But, it'll also mean that those won't show up in the lightbulb menu nor as a "Quick fix" in the diagnostic popup.

Based on proposed solution above, We could have separate configuration for editor's CodeActions (e.g. source.organizeImports action) or ruff's execute commands (e.g. ruff.executeOrganizeImports command). So new settings perhaps would either:

  • onActionsConfiguration.{action} e.g. onActionsConfiguration.source.organizeImports
  • onExecuteConfiguration.{cmd} e.g. onExecuteConfiguration.organizeImports

I prefer latter because I think we can also expand it for Format Document and I don't think it making sense for former one since document formatting isn't a CodeAction!

"editor.codeActionsOnSave": {
    "source.organizeImports": "explicit"
},
"ruff.onExecuteConfiguration.organizeImports": {
    "extend-ignore": ["E203", "F841", "F401"]
}

Note that E203, F841, F401 still will show via QuickFix bulb through editor but when file save triggered by editor and the source.organizeImports pushed, ruff server will consider additional settings from onExecuteConfiguration setting.

Relevant issues:

@dhruvmanila dhruvmanila changed the title Disable specific rules from auto-fixing on save? Support for disabling specific rules from auto-fixing on save Mar 3, 2025
@dhruvmanila dhruvmanila added enhancement New feature or request configuration Related to settings and configuration code-actions Related to code actions labels Mar 3, 2025
@dhruvmanila
Copy link
Member

@T-256 Thank you for providing other options! I think any design discussions should be moved to astral-sh/ruff#12709 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-actions Related to code actions configuration Related to settings and configuration enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants