Hello! Wanted to check if anyone knows how to solv...
# general
b
Hello! Wanted to check if anyone knows how to solve this issue. I have pants 2.20 configured with ruff When I run
pants fix ::
, it sorts my imports like this:
Copy code
import aioboto3
import uvicorn
from botocore.config import Config
from fastapi import FastAPI
from fastapi.middleware import Middleware
from fastapi.middleware.cors import CORSMiddleware

from backend.config import get_settings
from backend.routers import router
But when I either run my pre-commit, or directly run
pants fix src/python/backend/main.py
, it resorts my imports to:
Copy code
import aioboto3
import uvicorn
from backend.config import get_settings
from backend.routers import router
from botocore.config import Config
from fastapi import FastAPI
from fastapi.middleware import Middleware
from fastapi.middleware.cors import CORSMiddleware
So I keep getting constant reformats and my CI fails because it expects the first sorting, but pre commit overrides it to the second. (I know I could skip the pre commit, but I wanted to fix this wrong sorting that it is doing) Any ideas ?
1
b
A few questions: 1. Is your pre-commit configuration something outside pants? If so, is it using the same version of ruff as you're using in your pants configuration? 2. Assuming it's not a versioning mismatch, the symptom sounds somewhat like https://github.com/pantsbuild/pants/issues/18410, where the contents of Pants' sandbox influence the import sorting. A workaround there is to set
known-first-party
explicitly.
b
Does sound a lot like #2 given that it happens also when running pants fix :: vs pants fix src/...py I'll give that solution a try later. Thanks!
👍 1
Forgot to reply here, but leaving the note for anyone that looks this up in the future. First of all thanks Huon, #2 was exatcly the fix. I went to my pyproject.toml and set:
Copy code
[tool.ruff.lint.isort]
known-first-party = ["backend"]
Now
pants fix ::
and
pants fix src/python/backend/main.py
are having the same effect
🎉 1
b
great; thanks for closing the loop, and sorry for the trouble!