I am trying to use the new ruff linter instead of ...
# general
b
I am trying to use the new ruff linter instead of isort with poetry, and I am running into a configuration issue. My pyproject.toml at the root of the repo has (among other things) this section:
Copy code
per-file-ignores = { '__init__.py'= ["F401"]}

[tool.ruff.isort]
known-third-party = ["scipy"]
It is clear that per-file-ignores line is used by ruff when invoked through pants. For some reason it however completely ignores the known-third-party specification. If I however run ruff directly with ("ruff --config pyproject.toml --select="I") from the root it seems to use the known-third-party specification. What could be going wrong here, or is this a bug?
Ah. that's it. The config discovery is probably dumb. You use `[tool.ruff.isort]`(the sub-table) and config discovery looks for
[tool.ruff]
exactly: https://github.com/pantsbuild/pants/blob/c9def21de8a4f95d6929af227b605b3f1adfa9d2/src/python/pants/backend/python/lint/ruff/subsystem.py#L108
Presumably that will work @brave-hair-402 and if it does you might file an issue about making config discovery smarter for toml.
b
Would I not simply use this option by adding
Copy code
[ruff]
config="pyproject.toml"
to pants.toml?
e
That should do it.
If it doesn't work, try breaking out a
ruff.toml
file quick. That should get auto discovered without the need for magic byte sequence checking based on file name alone.
b
Seems like neither option actually works. If I have either a pyproject.toml or ruff.toml file in the root of the repository, and invoke ruff directly (with the --config flag) from one of my source roots I get a different behavior than if it is invoked through pants 😞
e
Surprising: https://github.com/pantsbuild/pants/blob/c9def21de8a4f95d6929af227b605b3f1adfa9d2/src/python/pants/backend/python/lint/ruff/rules_integration_test.py#L126-L156 Looks tested even. Filing a bug with good detail on your setup or an open source reproduction is probably in order.
b
I did set up a minimal example here: https://github.com/siggi84/pants_ruff_bug
I think it shows that if I call ruff directly scipy is handled as a third party import, but if called with pants it is handled as a first party import.
@enough-analyst-54434, If I change from ruff to isort, I see exactly the same issue. Suggesting that this is not really a ruff issue.
e
It is a ruff issue FWICT. Thanks for the repo! (For isort there is a classic isort / black push-pull that you need to ensure you either have new enough of both of those tools or configure them to cooperate). So, in your repo I installed the same version of ruff Pants uses in a local venv and find:
Copy code
~/dev/siggi84/pants_ruff_bug (master) $ ~/bin/ruff.venv/bin/ruff --config ruff.toml --fix src/python/hello_ruff.py src/python/hello_user.py
~/dev/siggi84/pants_ruff_bug (master) $ cd src/python/
~/dev/siggi84/pants_ruff_bug/src/python (master) $ ~/bin/ruff.venv/bin/ruff --config ../../ruff.toml --fix hello_ruff.py hello_user.py
Found 1 error(s) (1 fixed, 0 remaining).
Guess which way Pants calls ruff.
So, it seems there are some restrictions on the types of file paths you hand ruff. CD'd into the sys.path root, it works. Called from higher up, it does not.
@brave-hair-402 I know 0 ruff, but this would seem maybe enough for you to dig more. Perhaps there are ruff config options that aren't present on the CLI. I looked at the CLI and so no knobs for affecting this. I did not read ruff docs.
I did use
--no-local-cache --keep-sandboxes=always
to confirm what Pants was doing and how it was doing it.
So I read: https://beta.ruff.rs/docs/settings/#src
Copy code
$ git diff
diff --git a/ruff.toml b/ruff.toml
index d041e28..a8117a3 100644
--- a/ruff.toml
+++ b/ruff.toml
@@ -5,3 +5,5 @@ ignore = []
 # Allow autofix for all enabled rules (when `--fix`) is provided.
 fixable = ["A", "B", "C", "D", "E", "F", "I"]
 unfixable = []
+
+src = ["src/python"]
That fixes.
💪 1
It does not appear to be the case that you can pass --src on the CLI. If you could, Pants should probably be doing that and passing the list of your configured source roots.
@brave-hair-402 re-winding though, my initial guess at a bug is a bug still. The
pyproject.toml
auto-discovery is broken for cases like your original use case.
b
Yes, I will look a bit more carefully at that part in a few hours. But adding the same src entries to the ruff config as are already in the pants.toml files is not optimal.
e
Agreed. If you don't mind filing a few bugs, it would be great to have these reports coming from you and your example repo.
b
@enough-analyst-54434 I have created two bug reports: • https://github.com/pantsbuild/pants/issues/18410 • https://github.com/pantsbuild/pants/issues/18411 Does this not cover what we discussed above?
e
Yes. Thank you very much.
b
If I am leaving something out, or you think more information is needed, please let me know.
e
Nope, I had a look and the needed info seems to be all there.
b
Great. Thank you for your help.
b
Thank you for reporting!