vscode-ruff
vscode-ruff copied to clipboard
Files are excluded when not in the inclusion set
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.
Thanks for the report. I think I know why this might be happening.
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.
Hmm, I think there are two arguments here:
- 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-lspworks. This is probably the natural behavior of how a language server should consider an open file in the editor. - 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.
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.
I'd need to think about this but the language ID is internal to the language server protocol so the users shouldn't need to worry about it. This functionality might be more similar to how --force-exclude works.