I think I’ve run into a bug with `pants fmt` and ...
# general
d
I think I’ve run into a bug with
pants fmt
and isort, I’m seeing different behaviours invoking the tool manually vs invoking it via pants. I think it might be similar to what’s described in https://pantsbuild.slack.com/archives/C046T6T9U/p1686656639591789 Running
Copy code
pants fmt --only=isort main.py
Changes my file
Copy code
from flask import Flask, request
from lib.x.x import square
from marshmallow import Schema
In comparison to running
Copy code
dist/export/python/virtualenvs/tools/isort/bin/isort main.py
Changes it to
Copy code
from flask import Flask, request
from marshmallow import Schema

from lib.x.x import square
The latter of which looks more sensible I have a reproducible (hopefully) example repo here: https://github.com/Jackevansevo/pants-isort-weird
2
h
isort is the bane of my existence.
The issue typically is that isort behavior depends on what else is available in the sandbox
although in this case you're running on the entire repo, so that shouldn't be relevant
Oh, wait, no you're not
you're running on just
main.py
What happens if you run
pants lint ::
?
d
Interestingly if I
rm -rf lib
I get the same result for both tools. To answer your Q
Copy code
pants lint ::
Gives me
Copy code
✓ black succeeded.
✕ isort failed.
If the file was previously sorted with
isort main.py
But passes if I’ve first done
Copy code
pants fmt --only=isort main.py
So pants fmt / lint seem to agree here. Both seem to want to always change
main.py
Copy code
pants fmt --only=isort main.py
pants pants --only=isort ::
h
Yeah, so this is classic "the presence of lib affects what isort considers first party vs third party"
The solution is to make your first party top-level package names explicit in the isort config
1
d
nice, it looks like adding
Copy code
known_first_party = ['lib']
Indeed fixes the issue Many thanks for diving into this and helping out Benjy, much appreciated.
h
Sorry for the trouble. Unfortunately isort's expectation of knowing the entire universe is not very compatible with pants's sandboxing model.