Skip to content

bug/surprise: adding an empty ruff.toml affects how import rules behaves #14497

Open
@kaddkaka

Description

Adding an ruff.toml to your project file structure affects how ruff finds and categorizes imports, and therefore how rule I001 behaves.

version: ruff 0.7.4

Detailed description

Without ruff.toml

Project structure:

$ tree
.
├── proj
│   ├── lib
│   │   └── bar.py
│   └── src
│       └── foo.py
└── pyproject.toml

File content:

File: proj/lib/bar.py


File: proj/src/foo.py
import banana
import lib.bar
import itertools

File: pyproject.toml
[tool.ruff]

Here lib.bar is identified as 'lib.bar' as Known(ThirdParty)

$ ruff check --select I -v --no-cache proj/src/foo.py
[2024-11-20][20:25:48][ruff::resolve][DEBUG] Using configuration file (via parent) at: /home/david/projects/bugreport_ruff_toml_dir/pyproject.toml
[2024-11-20][20:25:48][ruff::commands::check][DEBUG] Identified files to lint in: 4.268338ms
[2024-11-20][20:25:48][ruff::diagnostics][DEBUG] Checking: /home/david/projects/bugreport_ruff_toml_dir/proj/src/foo.py
[2024-11-20][20:25:48][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'banana' as Known(ThirdParty) (NoMatch)
[2024-11-20][20:25:48][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'lib.bar' as Known(ThirdParty) (NoMatch)
[2024-11-20][20:25:48][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'itertools' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-11-20][20:25:48][ruff::commands::check][DEBUG] Checked 1 files in: 1.303531ms
proj/src/foo.py:1:1: I001 [*] Import block is un-sorted or un-formatted
  |
1 | / import banana
2 | | import lib.bar
3 | | import itertools
  |
  = help: Organize imports

Found 1 error.
[*] 1 fixable with the `--fix` option.
$ ruff check --select I --fix --no-cache proj/src/foo.py ; git diff
Found 1 error (1 fixed, 0 remaining).
diff --git a/proj/src/foo.py b/proj/src/foo.py
index 11710ea..ad0a354 100644
--- a/proj/src/foo.py
+++ b/proj/src/foo.py
@@ -1,3 +1,4 @@
+import itertools
+
 import banana
 import lib.bar
-import itertools

With an empty ruff.toml

$ touch proj/ruff.toml
$ tree
.
├── proj
│   ├── lib
│   │   └── bar.py
│   ├── ruff.toml
│   └── src
│       └── foo.py
└── pyproject.toml

Here the lib.bar import is identified as 'lib.bar' as Known(FirstParty)

$ ruff check --select I -v --no-cache proj/src/foo.py
[2024-11-20][20:28:25][ruff::resolve][DEBUG] Using configuration file (via parent) at: /home/david/projects/bugreport_ruff_toml_dir/pyproject.toml
[2024-11-20][20:28:25][ruff_workspace::pyproject][DEBUG] `project.requires_python` in `pyproject.toml` will not be used to set `target_version` when using `ruff.toml`.
[2024-11-20][20:28:25][ruff::commands::check][DEBUG] Identified files to lint in: 5.248828ms
[2024-11-20][20:28:25][ruff::diagnostics][DEBUG] Checking: /home/david/projects/bugreport_ruff_toml_dir/proj/src/foo.py
[2024-11-20][20:28:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'banana' as Known(ThirdParty) (NoMatch)
[2024-11-20][20:28:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'lib.bar' as Known(FirstParty) (SourceMatch("/home/david/projects/bugreport_ruff_toml_dir/proj"))
[2024-11-20][20:28:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'itertools' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-11-20][20:28:25][ruff::commands::check][DEBUG] Checked 1 files in: 1.36171ms
proj/src/foo.py:1:1: I001 [*] Import block is un-sorted or un-formatted
  |
1 | / import banana
2 | | import lib.bar
3 | | import itertools
  |
  = help: Organize imports

Found 1 error.
[*] 1 fixable with the `--fix` option.
$ ruff check --select I --fix --no-cache proj/src/foo.py ; git diff
Found 1 error (1 fixed, 0 remaining).
diff --git a/proj/src/foo.py b/proj/src/foo.py
index 11710ea..9b3f53b 100644
--- a/proj/src/foo.py
+++ b/proj/src/foo.py
@@ -1,3 +1,5 @@
+import itertools
+
 import banana
+
 import lib.bar
-import itertools

Additional wishes

This issue would have been easier to debug if ruff check -v also listed:

  • which ruff.toml has been visitted
  • which pyproject.toml has been visitted (even though ignore due to missing ruff section)

Activity

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

Metadata

Assignees

No one assigned

    Labels

    questionAsking for support or clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions