astral-sh / ruff-vscode

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

Files are incorrectly formatted when not included in ruff configuration #623

Open noahw3 opened 1 day ago

noahw3 commented 1 day ago

I have ruff configured to only run against a certain directory and ignore all others. I've done this via setting the include value in pyproject.toml:

[tool.ruff]
include = ["directory_to_include/**.py"]

This works correctly when I run ruff format from the command line - only files in that directory are changed. However, when I save a file in VSCode that should be excluded, formatting is applied.

VSCode settings.json:

{
  "[python]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "charliermarsh.ruff",
  }
}

I swear I had this working a couple weeks ago, so I'm not sure if some sort of regression was introduced recently or if I somehow didn't test it correctly previously. If I explicitly add other directories to the exclude configuration it works, but that's obviously not as flexible as being able to only include a single directory. VSCode extension version is v2024.50.0, ruff version is 0.6.6.

dhruvmanila commented 16 hours ago

Thanks for the report. I think I know why this might be happening.

dhruvmanila commented 16 hours ago

This was introduced in https://github.com/astral-sh/ruff/pull/13326 as the server will always include files that has the Python language id even though it's excluded by way of not including it. Sorry for this, it's a mistake on my end.

dhruvmanila commented 16 hours ago

Hmm, I think there are two arguments here:

  1. This is working as expected and it was a bug before https://github.com/astral-sh/ruff/pull/13326. The reason being that the opened file is always considered as being in the inclusion set and is similar to how ruff-lsp works. This is probably the natural behavior of how a language server should consider an open file in the editor.
  2. This is a bug and the fix would be to only check for the language id when the file is without an extension. But, this would mean that we'll start ignoring files that aren't in the inclusion set, and is opened in an editor.

The behavior as mentioned in (1) is also how other extensions like black work.

noahw3 commented 12 hours ago

Would it make sense to add a configuration parameter of some sort to choose whether to include all python language id files or to strictly follow the configured include set? I'm fine with the current behavior being the default if that more closely matches the expectations for VSCode extensions, but I'd still like the ability to somehow only use the strict inclusion set.