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)

Most upvoted comments

I had the same problem and I fixed it with this config, passing the config file to the args explicitly.

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
  # Ruff version.
  rev: v0.4.5
  hooks:
    # Run the linter.
    - id: ruff
      args: [ --fix, --config=pyproject.toml ]
    # Run the formatter.
    - id: ruff-format

@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: false in 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 their pyproject.toml.

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.1.15
    hooks:
      - id: ruff
        pass_filenames: false

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

  • add pre-commit to the virtual environment,
  • create a .pre-commit-config.yaml,
  • install the git hooks with pre-commit install
  • add the ruff configuration (ignore line length) to the pyproject.toml
  • try to only commit the python file that contains too long lines -> ruff complains about too long lines

When I first commit the .pre-commit-config.yaml and the pyproject.toml and 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 --verbose to the pre-commit hook.

- repo: https://github.com/astral-sh/ruff-pre-commit
  # Ruff: Formatter and Linter.
  rev: v0.5.1
  hooks:
    - id: ruff-format
      args: [--verbose, ]
    - id: ruff
      args: [--verbose, --fix,]

Then you can run pre-commit run --verbose --all-files

The output for me is:

ruff-format..............................................................Passed
- hook id: ruff-format
- duration: 0.08s

[ruff::resolve][DEBUG] Using configuration file (via parent) at: SOMEPATH/pyproject_template\pyproject.toml

I’m currently also running into this.

My issue was that the exclude setting to ignore some folders was under the [tool.ruff.lint] section. Following the comment below made me think that was the correct location:

@caerulescens - Both are accepted right now – putting ignore under [tool.ruff] and [tool.ruff.lint] are interchangeable. (We’re moving towards the latter, but they both work as-is.)

Moving it back to [tool.ruff] fixed it.

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.

@Blumenkind111 This is expected behavior of pre-commit. Pre-Commit Issue #818

One solution would be to use pass_filenames: false in 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 their pyproject.toml. That said, maybe Ruff’s pre-commit hook should do that by default?

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: false in .pre-commit-config.yaml fixed 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: local option. I would prefer to use the existing ruff I already have installed with uv add --dev ruff and then installed and locked to given version by uv. I think the recommended repo: https://github.com/astral-sh/ruff-pre-commit way will create yet another venv and install and will make it harder to maintain consistent version local & CI because it doesn’t use uv.lock?

I’m running into this issue as well.

In pyproject.toml I specify that the alembic folder should be exluded:

[tool.ruff]
line-length = 120
extend-exclude = ["alembic"]

[tool.ruff.lint]
select=["E4", "E7", "E9", "F", "INP001", "I"]

[tool.ruff.format]
quote-style = "preserve"

This works as it should when invoking ruff 0.11.5 directly.

But when trying to commit with this pre-commit 4.2.0 config:

repos:
  - repo: local
    hooks:
      - id: ruff-check
        name: ruff-check
        entry: uv run ruff check --fix
        language: system
        types: [python]
        stages: [pre-commit]
      - id: ruff-format
        name: ruff-format
        entry: uv run ruff format
        language: system
        types: [python]
        stages: [pre-commit]

the pyproject.toml setting extend-exclude is ignored and ruff starts to mess with the alembic folder:

(backend-fastapi) (base) cdeil@Kryvoff-MBP backend_fastapi_5 % git status
On branch alembic-update-autogenerate
Your branch is up to date with 'origin/alembic-update-autogenerate'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   alembic/versions/2024_04_29_1554-822ce1de1694_add_transport_task.py
        modified:   alembic/versions/2024_05_07_1151-c59bcfee52df_add_default_address_to_groups.py
        modified:   alembic/versions/2024_05_08_1313-92fe12293446_move_container_to_transaction_list.py
        modified:   alembic/versions/2024_06_23_1305-a7f232338b89_introduce_virtual_containers.py

(backend-fastapi) (base) cdeil@Kryvoff-MBP backend_fastapi_5 % git commit -m 'Fix declarative_base imports in alembic'         
ruff-check...............................................................Failed
- hook id: ruff-check
- exit code: 1
- files were modified by this hook

alembic/versions/2024_04_29_1554-822ce1de1694_add_transport_task.py:1:1: INP001 File `alembic/versions/2024_04_29_1554-822ce1de1694_add_transport_task.py` is part of an implicit namespace package. Add an `__init__.py`.
alembic/versions/2024_05_07_1151-c59bcfee52df_add_default_address_to_groups.py:1:1: INP001 File `alembic/versions/2024_05_07_1151-c59bcfee52df_add_default_address_to_groups.py` is part of an implicit namespace package. Add an `__init__.py`.
alembic/versions/2024_05_08_1313-92fe12293446_move_container_to_transaction_list.py:1:1: INP001 File `alembic/versions/2024_05_08_1313-92fe12293446_move_container_to_transaction_list.py` is part of an implicit namespace package. Add an `__init__.py`.
alembic/versions/2024_06_23_1305-a7f232338b89_introduce_virtual_containers.py:1:1: INP001 File `alembic/versions/2024_06_23_1305-a7f232338b89_introduce_virtual_containers.py` is part of an implicit namespace package. Add an `__init__.py`.
Found 5 errors (1 fixed, 4 remaining).

ruff-format..............................................................Failed
- hook id: ruff-format
- files were modified by this hook

2 files reformatted, 2 files left unchanged

Can one of the ruff maintainers reproduce?

@tazr For that, I believe you should use --extend-select instead 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?

@charliermarsh I think I solved it! The problem occurs when there’s another pyproject.toml within the project. The problem within the larger project was solved when I removed the second pyproject.toml within {{ cookiecutter.repository_name }}, which is used by cookiecutter. It seems that ruff prioritized [tool.ruff] in {{ cookiecutter.repository_name }}/pyproject.toml over the top-level pyproject.toml. This explains why this issue could be difficult to reproduce; it requires having multiple configurations for ruff present. I had the same D rules ignored in {{ cookiecutter.repository_name }}/pyproject.toml as the top-level pyproject.toml, which was misleading me during the debugging process.

This issue can be closed as ruff does read the pyproject.toml correctly. Maybe it would be helpful to note this behavior in the documentation if it’s not already there? I think it could be useful to raise a warning when multiple configurations are found, and indicate that it could lead to unexpected behavior.

This happens to me and I have a single pyproject.toml file

@dhruvmanila thank you for the quick response. Yes, providing pass_filenames: false helped in my case.

That said, maybe Ruff’s pre-commit hook should do that by default?

I’m not sure about doing that by default (will there be any side effects/breaking changes?). Anyway, I think information about pass_filenames would be useful in README.

@charliermarsh just created repo in which pre-commit hook ignores pyproject.toml config unlike the ruff itself. Hope this would help. https://github.com/Quatters/ruff-pre-commit-bug-demo

@caerulescens - Both are accepted right now – putting ignore under [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.toml correctly, and I checked by causing N999: Invalid module name and then ignoring the rule: ignore = ["N999"] on a separate run. The bugged and new project that I compared both use ruff-pre-commit v0.1.7 and 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.