Repository navigation
Exclude applied to root, when used with pre-commit #9023
Description
Activity
Can you try it with the official pre-commit hook as mentioned in the documentation?
So, your config would be:
repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.1.6 hooks: - id: ruff types: [python]
Also, can you provide the output for the command
python -m pre_commit run --all-files --verbose?- addedquestionAn issue asking for support or clarificationAn issue asking for support or clarification
on Dec 6, 2023 Can you try it with the official pre-commit hook as mentioned in the documentation?
Same result with the official pre-commit hook. Files are not checked and the violation is not detected.
Also, can you provide the output for the command python -m pre_commit run --all-files --verbose?
> python -m pre_commit run --all-files --verbose ruff.....................................................................Passed - hook id: ruff - duration: 0.01s warning: No Python files found under the given path(s)I'm pretty sure this is the same problem that I brought up here #6191
Are the files correctly checked if you add a
ruff.tomlto the root of thebuilddirectory?Are the files correctly checked if you add a
ruff.tomlto the root of thebuilddirectory?No. Same result if a
ruff.tomlexists in the root.The files are checked if ruff is called directly:
> python -m ruff check . test.py:3:8: F401 [*] `ruff` imported but unused Found 1 error. [*] 1 fixable with the `--fix` option.With the pre-commit hook, they are not checked:
> python -m pre_commit run --all-files --verbose ruff.....................................................................Passed - hook id: ruff - duration: 0s warning: No Python files found under the given path(s)Directory structure:
build |- .git |- .pre-commit-config.yaml |- test.py |- ruff.tomlOh sorry, I missed that this is specifically when using
--all-files(at least, I assume?). That I can at least explain. Presumedly, pre-commit is passing the root directory to Ruff, likeruff check .(from the directory containingbuild). In that case, when we start to iterate over files in that root directory, we use Ruff's default settings, and see thebuilddirectory, which is excluded by default, and skip it. (If we pass paths to files withinbuilddirectly, Ruff will correctly use the settings withinbuild.)This is confusing in this case, but changing the behavior would also be confusing. Imagine that instead of
build, your directory was.venv. It would be strange if we linted files within.venvin such a case, right?If pre-commit provides the files in relative paths, would the problem be resolved?
Oh sorry, I missed that this is specifically when using
--all-files(at least, I assume?). That I can at least explain. Presumedly, pre-commit is passing the root directory to Ruff, likeruff check .(from the directory containingbuild). In that case, when we start to iterate over files in that root directory, we use Ruff's default settings, and see thebuilddirectory, which is excluded by default, and skip it. (If we pass paths to files withinbuilddirectly, Ruff will correctly use the settings withinbuild.)I think there is a misunderstanding between us.
There is no directory namedbuildin the project. The root directory is namedbuild.
If we are using strictly relative paths, no path will containbuild.The current behavior is not consistent between pre-commit and directly using Ruff.
Ruff is working as expected when called directly:> cd /home/user/build > ruff . test.py:3:8: F401 [*] `ruff` imported but unused Found 1 error. [*] 1 fixable with the `--fix` option. > ruff /home/user/build/test.py test.py:3:8: F401 [*] `ruff` imported but unused Found 1 error. [*] 1 fixable with the `--fix` option.Running it with pre-commit has a different behavior:
> python -m pre_commit run --all-files --verbose ruff.....................................................................Passed - hook id: ruff - duration: 0.05s warning: No Python files found under the given path(s)
If we check the documentation here. https://docs.astral.sh/ruff/settings/#exclude
Single-path patterns, like .mypy_cache (to exclude any directory named .mypy_cache in the tree), foo.py (to exclude any file named foo.py), or foo_.py (to exclude any file matching foo_.py ).
Ruff will exclude any directory named
buildin the tree.
I expect it to check only the tree starting from root (thebuilddirectory in our case).
Therefore the tree would look like this:./ ├─ .git/ ├─ .pre-commit-config.yaml ├─ test.py ├─ ruff.tomlFYI: this issue is still not resolved.
Removingbuildfrom the default exclude list, simply hides the issues, but does not solve it.
Files are not checked with Ruff running in pre-commit, if the git repository is inside a build directory.
Steps to reproduce
The setup is very basic.
Create a directory
build.Initialize git inside this directory.
Create a Python file, which is violating Ruff rules.
Install pre-commit hook (with the pre-commit Python package).
Run pre-commit on all files -> the Python file will not be checked.
Project structure
Executed commands:
Files
test.py
.pre-commit-config.yaml
Versions
pre-commit==3.5.0
ruff==0.1.7