ruff-pre-commit: Ruff configuration from pyproject.toml is ignored
Hi,
I’m trying to use the pre-commit hook for Ruff, but it ignores my settings from the pyproject.toml. I want Ruff to ignore the line-length but if I commit a file with lines that exceed this limit it is complaining about it. If I use a ruff.toml to configure Ruff it works as expected.
I use version 0.0.292.
My settings in .pre-commit-config.yaml:
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: end-of-file-fixer
- id: check-added-large-files
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.292
hooks:
- id: ruff
Ruff configuration part in my pyproject.toml (same is used in the ruff.toml)
[tool.ruff]
select = ["E"]
# Never enforce `E501` (line length violations).
ignore = ["E501"]
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 53
- Comments: 44 (6 by maintainers)
I had the same problem and I fixed it with this config, passing the config file to the args explicitly.
@Quatters Hi, thanks for providing the repository for the problem you’re facing. But, I don’t think it’s a bug with Ruff or pre-commit, I’ve described in detail about what’s happening here: https://github.com/astral-sh/ruff/issues/9696#issuecomment-1916480913.
One solution would be to use
pass_filenames: falsein the pre-commit config so that the file discovery is done by Ruff taking into account the includes and excludes configured by the user in theirpyproject.toml.This solves your problem.
That said, maybe Ruff’s pre-commit hook should do that by default?
I could reproduce the problem and find a solution. If I use a project with already added code and a pyproject.toml to the git repo and then
pre-committo the virtual environment,.pre-commit-config.yaml,pre-commit installpyproject.tomlWhen I first commit the
.pre-commit-config.yamland thepyproject.tomland after that commit the file with the long line then ruff ignores it as expected. Is this behavior expected?I’m still having this issue. This bug was also present in pydocstring (now deprecated). Is there a relation?
It works now for me too, but one thing I stumbled upon was when updating some ruff rules or exceptions and not commiting them with the python files (for whatever reason), the hook uses the last commited version of pyproject.toml, which does not have the new rule (or exclusion) and thus produces other results when calling ruff check from console.
If you think your config is ignored. Add
--verboseto the pre-commit hook.Then you can run
pre-commit run --verbose --all-filesThe output for me is:
I’m currently also running into this.
My issue was that the
excludesetting to ignore some folders was under the[tool.ruff.lint]section. Following the comment below made me think that was the correct location:Moving it back to
[tool.ruff]fixed it.@Blumenkind111 This is expected behavior of pre-commit. Pre-Commit Issue #818
Wouldn’t that make ruff to check all files every time? I think pre-commit users expect it to only check changed files, even if it is fast.
Even if pre-commit passes filenames to
ruff check, I still don’t see why it wouldn’t reed the configuration…Would really appreciate if you could share some more information – we weren’t able to reproduce it above and it doesn’t seem to be a common issue, so we need some help. What behavior are you seeing? What commands are you running? How is your project structured? Have you tried clearing the cache, as above? Thank you in advance!
OK, so adding
pass_filenames: falsein.pre-commit-config.yamlfixed the issue and the commit worked. I had missed that suggestion in a comment above.Suggestion: at https://docs.astral.sh/ruff/integrations/#pre-commit please also document how to work with the
repo: localoption. I would prefer to use the existing ruff I already have installed withuv add --dev ruffand then installed and locked to given version by uv. I think the recommendedrepo: https://github.com/astral-sh/ruff-pre-commitway will create yet another venv and install and will make it harder to maintain consistent version local & CI because it doesn’t useuv.lock?I’m running into this issue as well.
In
pyproject.tomlI specify that thealembicfolder should be exluded:This works as it should when invoking ruff 0.11.5 directly.
But when trying to commit with this
pre-commit4.2.0 config:the
pyproject.tomlsettingextend-excludeis ignored and ruff starts to mess with thealembicfolder:Can one of the ruff maintainers reproduce?
@tazr For that, I believe you should use
--extend-selectinstead of--select@rpop0 How are you reproducing this bug? Could you post a minimal example repository for us or maybe zip a folder and attach here?
This happens to me and I have a single pyproject.toml file
@dhruvmanila thank you for the quick response. Yes, providing
pass_filenames: falsehelped in my case.I’m not sure about doing that by default (will there be any side effects/breaking changes?). Anyway, I think information about
pass_filenameswould be useful in README.@charliermarsh just created repo in which pre-commit hook ignores
pyproject.tomlconfig unlike the ruff itself. Hope this would help. https://github.com/Quatters/ruff-pre-commit-bug-demo@caerulescens - Both are accepted right now – putting
ignoreunder[tool.ruff]and[tool.ruff.lint]are interchangeable. (We’re moving towards the latter, but they both work as-is.)It seems I have more than one project reliably producing this bug (even after clearing caches), and I constructed a new project as a minimal example for @charliermarsh to reproduce the bug reliably. The new project I made ended up reading the
pyproject.tomlcorrectly, and I checked by causingN999: Invalid module nameand then ignoring the rule:ignore = ["N999"]on a separate run. The bugged and new project that I compared both useruff-pre-commitv0.1.7and were invoked the same way. I don’t think it’s related to the version, but if I spend some more time I can isolate the issue.Chiming in just in case it may help someone. In my case, clearing the precommit cache (
$HOME/.cache/pre-commit/) fixed a similarly sounding issue some time ago. Alas, I didn’t keep the cache so I couldn’t debug it further.