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

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

Open
noahw3 opened this issue Sep 30, 2024 · 5 comments
Open
Labels
needs-decision Undecided if this should be done

Comments

@noahw3
Copy link

noahw3 commented Sep 30, 2024

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
Copy link
Member

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

@dhruvmanila dhruvmanila added the bug Something isn't working label Oct 1, 2024
@dhruvmanila
Copy link
Member

This was introduced in astral-sh/ruff#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
Copy link
Member

Hmm, I think there are two arguments here:

  1. This is working as expected and it was a bug before Add support for extensionless Python files for server ruff#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.

@dhruvmanila dhruvmanila removed the bug Something isn't working label Oct 1, 2024
@noahw3
Copy link
Author

noahw3 commented Oct 1, 2024

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.

@dhruvmanila
Copy link
Member

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.

@dhruvmanila dhruvmanila added the needs-decision Undecided if this should be done label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Undecided if this should be done
Projects
None yet
Development

No branches or pull requests

2 participants