Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kaddkaka opened this issue Nov 20, 2024 · 4 comments
Open
Labels
question Asking for support or clarification

Comments

@kaddkaka
Copy link

kaddkaka commented Nov 20, 2024

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)
@MichaReiser MichaReiser added the question Asking for support or clarification label Nov 21, 2024
@MichaReiser
Copy link
Member

Thanks for your feedback on your ruff experience. I can see how this can be surprising. The behavior is intentional (not speaking about the linked issue) but probably hard to find. It is explained in the documentation of the src setting.

You can explicitly set the src setting to exclude the lib folder.

@kaddkaka
Copy link
Author

So if I don't want this automatic behavior, what's my option?

[tool.ruff]
src = []

?

@kaddkaka
Copy link
Author

And what about

ruff check -v should list:

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

Propose in separate issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

2 participants