I have started looking into linting BUILD files us...
# development
f
I have started looking into linting BUILD files using
flake8
. The use case is https://github.com/pantsbuild/pants/issues/19486 - I want to error on duplicate keys in a dict, e.g.
Copy code
python_sources(
    name="lib",
    overrides={
        "main.py": {"dependencies": ["//:reqs#pytest"]},
        "main.py": {"dependencies": []},
        },
)
we currently only run formatter on BUILD files, so this would be new. I've experimented in a local branch and this would require making changes to the flake8 rules. Before I proceed, are we okay with the new feature? Anything that is worth taking into account?
h
One challenge will be undefined symbols. BUILD files have a bunch of magic symbols Flake8 will be mad about since there are no imports
f
I was hoping to pick rules only selectively if that helps...
👍 1
there's an argument whether we should lint BUILD with a Python linter at all, of course...
there's value in doing this, but I don't want to fight the false positives 🙂
h
Yeah, I think you'll want to do that. But that means for example ignoring the Flake8 config the user has. So it's maybe a little weird to open source to Pants because users wouldn't be able to control the chosen lints You'd probably want it a dedicated backend for lint and build files and Flake8
f
right. So just as we have
src/python/pants/backend/python/lint/flake8/rules.py
for Python sources, I thought to have an independent function/rule in
src/python/pants/backend/build_files/lint/flake8/rules.py
that would only be used by the
pants lint src/files/BUILD
h
do you know which lints you'd use? And is it worth using Ruff instead since it's so much faster?
f
I hope Pants will route the requests accordingly to the relevant rules when run
pants lint src/files/BUILD src/files/module.py
h
will route the requests accordingly to the relevant rules when run pants lint src/files/BUILD
I thinkkkkk I fixed this but am not certain. What happens when you run
pants lint
on a BUILD file today? In the past, it would run on all targets defined in that BUILD file. But I think I changed that. (sorry, don't have Pants handy)
👀 1
f
pants lint src/python/pants/backend/python/lint/flake8/BUILD
doesn't lint Python files from the BUILD file targets
👍 1
And is it worth using Ruff instead since it's so much faster?
worth exploring, thanks for reminding about that one. I believe more and more rules are ported, so that one should be available now or in the near future
👍 1
https://beta.ruff.rs/docs/rules/#pyflakes-f yes, that one is already supported
the duplicate key error that is
h
I'd be a +1 for a Ruff-based linter for BUILD files, where Pants chooses the rules for you and it intentionally ignores config files etc. I think the only thing we'd need to consider is how to indicate if certain BUILD files should be ignored. But maybe you could ignore that. If you don't want a BUILD file reviewed, then use
build_ignore
or
pants_ignore
, or inline comments to disable Ruff for each violation The last thing I'd suggest is: where would you document this? An issue with Pants is discoverability. How do users find out about this?
💗 1
f
thanks for the input, Eric, let me explore and think about these bits
❤️ 1
p
A ruff based linter for BUILD files would be awesome.