Open reinout opened 5 months ago
Can I take this?
I think we may need to do some research (or have some discussion) around the desired behavior here.
Like, I think it's reasonable that if you're in a project that has an exclude for a subdirectory, and that subdirectory has its own configuration file, and you run ruff check subdirectory/some_file.py
, that we check that file rather than ignoring it. I believe this was an intentional change we made long ago to not walk past the project root when looking for excludes.
Most of the confusion here, I think, comes from the pre-commit workflow wherein you're passing those excluded files to Ruff directly. It's possible that we should respect parent excludes when you run with --force-exclude
, but it'd be a change in semantics.
The confusion indeed comes from pre-commit's passing of excluded files.
Ruff nicely respects parent excludes with --force-exclude
, except for a pyproject.toml
file. Which confuses me :-)
Is there a usecase for an excluded directory to be partially "un-excluded" by a config file in the excluded directory? I can't really think of one.
In case the current functionality is the desired behaviour, a comment in the ruff pre-commit documentation might be a good idea. OTOH, this issue will probably turn up in search engines, including the --config
tip.
In my opinion, it is pretty much a corner case so leaving it be is okay if there's no quick solution.
👍 I think what's happening is... We look at the pyproject.toml
in the subdirectory, because if it were valid and contained Ruff configuration, then we'd ignore the exclusion from the parent, and instead defer to the subdirectory's configuration to figure out the relevant exclusions. But then we can't parse it, and so the error shows up. It's definitely an edge case... But we should probably silence that error _if the pyproject.toml
is excluded by the parent.
I think it would make sense at least to silence that error if that dir is excluded. I cannot figure any usecase to parse it if excluded?🤔
@charliermarsh what's the conclusion? Should we some guard around the corner or leave it as it is?
@charliermarsh, is there any update to this particular case? I see that #7959 was closed orienting for the use of globset rules and this was also suggested by you in #9381.
Using the solution mentioned in both issues works fine for excluding the whole directory, but what about specific files inside that directory? For instance, this configuration will still parse the subdirectory pyproject.toml
, and in my case ruff
will fail because said file contains Jinja syntax:
extend-exclude = ["[{][{] cookiecutter.repo_name [}][}]/pyproject.toml"]
Ruff
0.1.13
I've excluded a "cookiecutter" directory because those files have
{{ project_name }}
-like variables in them that aren't valid python or toml. This works fine with ruff itself. When run from within a pre-commit (ruff-pre-commit, alsov0.1.13
), filenames are passed explicitly to ruff.pyproject.toml
in the excluded directory is not excluded, presumably because ruff allows multiple configuration files in various directories.I've worked around it by telling ruff to only use my top-level pyproject.toml by passing
--config pyproject.toml
. That quieted things down:Expectation: ruff excludes directories that it is told to exclude, including pyproject.toml files, even when those are "accidentally" passed on the command line (by a pre-commit hook in this case).
To reproduce it, create a subdir in an existing pyproject.toml-using project:
Ignore the "bugger" directory in your top level
pyproject.toml
:Note that ruff-pre-commit automatically uses
--force-exclude
to force the excludes to be used even when a file is passed on the commandline. I'm using that in the examples below, too, of course.Running ruff itself now works fine, but passing either the .py or the .toml from the excluded subdir causes ruff to try and read the
pyproject.toml
file, resulting in the "TOML parse error":If you remove the pyproject.toml file, everything works just fine (the warning is fine):
If we re-instate the pyproject.toml file in the excluded subdir it fails again. But passing
--config pyproject.toml
prevents ruff from trying to read the offending .toml file, making it work fine again:Conclusion: I think ruff should not look for config files in excluded dirs.