Hi. Is there a way to tel `pants fmt` to only do t...
# general
f
Hi. Is there a way to tel
pants fmt
to only do the checking? I'm using
ruff
and tried passing the
--check
flag with
pants fmt :: -- --check
, but without success. I see there is a
--lint-skip-fixers
, but I don't find an equivalent for
fmt
. Is there a way for me to achieve this? I'd like a CI job to fail with exit code 1 if the code is not formatted.
w
I think
pants lint
runs the fmt checker, doesn't it?
pants lint --only=ruff-format
or something?
Copy code
pantsanity % pants lint --only=ruff-format ::                               βŽ‡ main*
08:34:31.12 [INFO] Initializing scheduler...
08:34:42.74 [INFO] Scheduler initialized.
08:34:42.92 [INFO] Completed: Format with `ruff format` - ruff format made no changes.

βœ“ ruff format succeeded.
f
Ah, yes, it does ... 🀦 Thank you!
πŸ‘ 1
Hm, now
pants lint
checks what I want with
ruff
, but
pants fmt
only fixes code style things, while it skipps the import ordering. When I run
pants lint
, it reports them and says they're fixable with the
--fix
flag, while format doesn't do anything about them. I tried passing
--fix
to both
lint
and
fmt
.
w
pants fix
The differentiation comes from semantic changes vs non-semantic (e.g. whitespace and stuff) changes. I'm summarizing a LONG bikeshed here...
pants fix fmt lint check test
is my typical non-packaging workflow
f
Got it. Thanks a lot!
πŸ‘ 1
Still not over ... πŸ™‚ I have a weird discrepancy where
pants fix
and
pants fmt
say everything is fine, but
pants lint
says I have some import statements unsorted. I'm not sure where this difference could come from, as it's
ruff
in all cases.
w
What happens when you chain them as a single call?
f
fix
and
fmt
pass, saying everything is fine,
lint
fails with the same error as before
w
And there are no other enabled linters/formatters? That's where most of my issues like this happen - something else taking over
f
This is all I have in the backends:
Copy code
backend_packages = [
  "pants.backend.python",
  "pants.backend.python.typecheck.mypy",
  "pants.backend.experimental.python",
  "pants.backend.experimental.python.lint.ruff.check",
  "pants.backend.experimental.python.lint.ruff.format",
  "pants.backend.docker",
]
I presume nothing else could run other than what's in the backends. I would suspect
isort
for example, but it's not there.
w
Hmm, so they should all run off the same configuration. Also, just for clarity - are you saying that ruff formatter is failing on the lint? Or the ruff linter is failing on lints?
like
pants fix fmt lint --only=ruff-format ::
fails?
f
If I run it like that, everything passes. If not, I get:
Copy code
Completed: Lint with `ruff check` - ruff check failed (exit code 1).
w
Yeah, so a different thing is failing
Not the formatter
f
But interestingly,
pants fix
did fix the import ordering. Before I had much more errors regarding import ordering. Now
pants fix
says everything is fine, but
pants lint
still finds import ordering problems. Since it's
ruff
running behind the scenes in both cases, I'm not sure how this can happen.
w
Not sure, from when I've used it, ruff's auto-fixer can't fix every problem, just a subset of them
What happens when you first run
Copy code
ruff check --fix
f
I did
pants export
and tried running
ruff
in the exported venv. All checks are fine:
ruff format
,
ruff format --check
,
ruff check
,
ruff check --fix
.
w
This is strange, if you can create a repro with that happening, we can take a look. I've never seen something like this - maybe a bad cache or something?
f
I'll try removing all caches. Then if that doesn't help, I'll try to come up with a repro. Thanks for all the help so far!
w
πŸ‘
h
This is reminiscent of a known problem with isort and I wonder if ruff has inherited it. Are you running on
::
or are you using
--changed-since
? There is an issue with how the tools infer first-party vs third-party that requires all the imported code to be present in the sandbox, so the tool can see it, but depending on how you invoked Pants, and on what's been cached, it may not be.
The workaround for isort is to use its config to tell it explicitly about your first party packages. Maybe ruff has similar config?
f
Removing all caches didn't help. I am running on
::
. Did also
--changed-since
, but I get the same result. I'll check if there are some settings for
ruff
as you suggest, thanks.
f
I think this thread was a different topic, but it does reference the first party setting: https://pantsbuild.slack.com/archives/C046T6T9U/p1713790680146989?thread_ts=1713786750.898519&cid=C046T6T9U
I had a similar problem but it was b/c I had both ruff and yapf doing formatting and they were in disagreement about import sorting
h
The joys of multiple nonstandard tools doing overlapping things…
f
Setting
known-first-party
didn't help. Also, only ruff is executed in this case (
ruff format
and
ruff check
). So I don't think I'm experiencing the joys of multiple nonstadard tools doing overlapping things πŸ™‚ . I'll try to come up with a reproducible sample.
I solved it! The thing was, I needed to set
known-first-party
and
known-local-folder
for every package in our repo. I did it wrong at the first attempt, where I thought I just need to set
known-first-party
globally. Now I use a local
pyproject.toml
for every package, where I take common settings via:
Copy code
[tool.ruff]
extend = "../../../pyproject.toml"
And I removed the global
pyproject.toml
as a config for ruff in `pants.toml`:
Copy code
[ruff]
config = "pyproject.toml"  # <---- delete
So now
ruff
picks up the per-package
pyproject.toml
each time it goes through the folders. Thank you all for the help !
πŸŽ‰ 2