Fix Ruff import sorting when fix batches omit package markers#23298
Fix Ruff import sorting when fix batches omit package markers#23298
Conversation
7d39a1a to
66a73ab
Compare
| @dataclass(frozen=True) | ||
| class RunRuffRequest: | ||
| snapshot: Snapshot | ||
| files: tuple[str, ...] |
There was a problem hiding this comment.
Bear with me, as I haven't had my coffee - but why do we have this files? It's an alias for snapshot.files in several of these cases.
So, it's files for the files we want fixed, while snapshot is files + init.py?
There was a problem hiding this comment.
Exactly. RunRuffRequest.files is the Python files to fix while snapshot contains those files plus any __init__.py which needed to be added back.
There was a problem hiding this comment.
In terms of naming, files is consistent with the AbstractFmtRequest.files property.
|
I have a vague recollection that there was an idea to not have ruff partitions, as it would be faster to just run it over the largest set of code, than to go through the overhead to run it otherwise. However, I don't see how that would have worked in multi-py-version repos, as each has it's own python level. |
I don't have any context on the Ruff design choices. The custom partitioner here is putting everything into a single partition. The |
Problem
As observed with CI test failures in #23284, Ruff’s isort rules can classify imports differently depending on whether or not ancestor
__init__.pyfiles are present in the sandbox. Thefixgoal may split files into batches, which can leave a Ruff batch without the package marker files that were present during lint. In that case,pants lintcan report a fixable I001, whilepants fixreports "All checks passed!."Solution
This change adds a custom partitioner to the Ruff fix rules to record the necessary
__init__.pyfiles as per-partition metadata. When thefixgoal executes on a partition, it includes the__init__.pyfiles set in the metadata back into the input digest so Ruff sees the correct package behavior.Also adds a regression test covering a local package whose name shadows a third-party package.
AI Disclousre
Codex (GPT-5.5) was used to diagnose the problem observed in #23284, write a reproduction test, and write the fix with design guidance from me.