Visitar URL original
Ruff configuration from pyproject.toml is ignored · Issue #54 · astral-sh/ruff-pre-commit · GitHub
Skip to content

Ruff configuration from pyproject.toml is ignored #54

Open

Description

@beajifu

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"]

Activity

  1. charliermarsh commented on Oct 13, 2023

    @charliermarsh
    Member

    Hmm, a bit of a tough one to debug. When you switch between pyproject.toml and ruff.toml, are the files located in the same directory?

  2. beajifu commented on Oct 14, 2023

    @beajifu
    Author

    Yes they are in the same directory.

    I tried it on another laptop with a small example project and this problem did not occur. I try to find a way to reproduce the problem and give you an update.

  3. beajifu commented on Oct 15, 2023

    @beajifu
    Author

    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?

  4. klow68 commented on Oct 19, 2023

    @klow68

    same issue with v0.1.0 but committing the config doesn't change anything

    • found the issue : the configuration needs to be added in the toml and we need to select the rule to use even for line-length
    repos:
      - repo: https://github.com/astral-sh/ruff-pre-commit
        rev: v0.1.0
        hooks:
          - id: ruff
            args: [--select=C901, --select=E501]
    

    by default line-length is not checked, seems strange no ?

  5. charliermarsh commented on Oct 19, 2023

    @charliermarsh
    Member

    @klow68 - We removed E501 from the default configuration in v0.1.0 (https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md#breaking-changes), so I think that's working as intended.

  6. dhruvmanila commented on Oct 30, 2023

    @dhruvmanila
    Member

    @beajifu I'm unable to reproduce with the steps mentioned although I'm using F401 as an example rule.

    Gist: https://gist.github.com/dhruvmanila/8c796087330cb22699f3b20e929fde45

    .
    ├── .pre-commit-config.yaml
    ├── pyproject.toml
    └── test.py
    

    Steps

    1. Install pre-commit and hooks
    2. Comment out the ignore in config file
    $ pre-commit run
    ruff.....................................................................Failed
    - hook id: ruff
    - exit code: 1
    
    test.py:1:8: F401 [*] `os` imported but unused
    Found 1 error.
    [*] 1 potentially fixable with the --fix option.
    1. Uncomment the ignore in config file
    $ pre-commit run
    ruff.....................................................................Passed
  7. rpop0 commented on Nov 21, 2023

    @rpop0

    My configuration in pyproject.toml also seems to be having an issue.

    Running ruff without any config: 3 errors

    Running ruff with 3 select = ["ALL"] in pyproject.toml: 389 errors

    Running ruff through pre-commit: 89 errors.

    It seems like running ruff through pre-commit is using more rules than default but ignoring the rules in pyproject.toml

  8. rytilahti commented on Nov 21, 2023

    @rytilahti

    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.

  9. andylinzie commented on Jan 12, 2024

    @andylinzie

    This still seems to be an issue; using v0.1.7

  10. charliermarsh commented on Jan 12, 2024

    @charliermarsh
    Member

    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!

  11. andylinzie commented on Jan 12, 2024

    @andylinzie

    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.

  12. rafalkrupinski commented on Jan 26, 2024

    @rafalkrupinski
    [tool.ruff]
    select = ["E"]
    
    # Never enforce `E501` (line length violations).
    ignore = ["E501"]

    don't these go in tool.ruff.lint?

  13. andylinzie commented on Jan 27, 2024

    @andylinzie

    @rafalkrupinski I double checked docs, and you're right. I have been busy with other tasks, but I saved this thread for later. I'll retest, and repost.

  14. charliermarsh commented on Jan 27, 2024

    @charliermarsh
    Member

    @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.)

  15. andylinzie commented on Jan 27, 2024

    @andylinzie

    I realized the same thing just now; I'll retest

  16. 15 remaining items

  17. mazzma12 commented on May 7, 2024

    @mazzma12

    I have the same problem that ruff-pre-commit does not load ruff config from pyproject.toml.

    I resolved it by adding the config in the args.

      - repo: https://github.com/astral-sh/ruff-pre-commit
        # Ruff version.
        rev: v0.4.3
        hooks:
          # Run the linter.
          - id: ruff
            args: ["--select", "E,W,F,I,C,B,UP", "--ignore", "E203,B008,C901"]
          # Run the formatter.
          - id: ruff-format

    Same here, but more explicitly for the --exclude config which is not accounted for

  18. artefactop commented on Jun 23, 2024

    @artefactop

    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
  19. NIvo172 commented on Jul 12, 2024

    @NIvo172

    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
  20. ezeparziale commented on Jul 14, 2024

    @ezeparziale

    I upgraded from 0.4.5 to 0.5.1, and Ruff detects pyproject.toml automatically.

    # See https://pre-commit.com for more information
    default_language_version:
      python: python3.12
    repos:
      - repo: https://github.com/astral-sh/ruff-pre-commit
        # Ruff version.
        rev: v0.5.1
        hooks:
          # Run the linter.
          - id: ruff
            args: [--fix]
          # Run the formatter.
          - id: ruff-format
  21. Blumenkind111 commented on Jul 31, 2024

    @Blumenkind111

    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.

  22. NIvo172 commented on Aug 1, 2024

    @NIvo172

    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

  23. exhuma commented on Oct 10, 2024

    @exhuma

    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.

  24. tazr commented on Oct 22, 2024

    @tazr

    In case that might help someone. In my case I noticed that the pyproject.toml was read (syntax errors there were found by ruff) but that ignore directives were... ignored. I found that was because I was using a --select directive in the .pre-commit-config.yaml file. For some reason this seems to reset the options put in the pyproject.toml. (This might be a feature so as to enforce some checks but I am unaware of a documentation supporting that.)

    In order to be able to have ignore rules defined in one or multiple pyproject.toml I had to move select directives from the .pre-commit-config.yaml to those files.

  25. denes44 commented on Nov 9, 2024

    @denes44

    @tazr For that, I believe you should use --extend-select instead of --select

  26. MatthijsdeJ commented on Apr 15, 2025

    @MatthijsdeJ

    I'm still having this issue. This bug was also present in pydocstring (now deprecated). Is there a relation?

  27. cdeil commented on Apr 30, 2025

    @cdeil

    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?

  28. cdeil commented on Apr 30, 2025

    @cdeil

    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?

  29. MatthijsdeJ commented on May 1, 2025

    @MatthijsdeJ

    The solution for me was git adding the updated .pre-commit-config.yaml. Someone told me that a cached (outdated) version of the file might be used before adding it. This solved the issue for me.

  30. craigulliott commented on Aug 26, 2025

    @craigulliott

    I ran into this same problem on a new project. Turns out that I just needed to git add (stage) all the relevant files. The hooks don't seem to be aware of files that are unstaged.

    I was getting ruff check...............................................................Passed, but after staging all my files it worked as expected.

    git add .
    git commit -m 'build: testing hooks'
    # commit failed due to my configured rules in .ruff.toml
    

    I tied staging just my pyproject.toml file, and that didn't help.

    Next, I broke my config out into a separate .ruff.toml file, but that didn't help (note, i never tried staging that file).

    Finally I just tried git add . and then things worked as expected. So I'm not sure if it was staging the .ruff.toml file, or some other files. This was a brand new project.

    Hope this helps someone!

  31. samcorky commented on Dec 14, 2025

    @samcorky

    I am having a similar issue, where the pre-commit ignores the

    [tool.ruff]
    fix = true

    configuration in my pyproject.toml file. Using the CLI, it works fine, but pre-commit seems to ignore it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions