https://pantsbuild.org/ logo
f

fresh-cat-90827

12/23/2021, 5:46 PM
I have noticed there is interest in programmatically modifying
BUILD
files (for instance, to add some boilerplate code across the monorepo or individual projects, such as disabling a formatter globally and then enabling it on the a project basis). I’ve explored this path and would love to share with the community. To learn more, see the thread.
I had to add boilerplate code to a
BUILD
file for all
python_library
and
python_tests
targets. I am not considering a manual approach which would only work for a toy project. So we go for a programmatic update of the
BUILD
files. One path to take was to use an AST / CST (abstract or concrete syntax tree) based approach (since
BUILD
files are valid Python files) and then just add extra arguments to a function call. This is fairly easy given the low complexity of the
BUILD
files in my case. A few libraries to consider are: • https://github.com/python-rope/ropehttps://github.com/PyCQA/redbaronhttps://github.com/berkerpeksag/astorhttps://github.com/Instagram/LibCST However, this may mean some work and also not everyone wants to get into the AST business 😄 Another approach was to use an OOTB tool to edit
BUILD
files which I did. Because
BUILD
files in Pants share semantics with the
BUILD
files used in Bazel, I shamelessly used buildozer (part of Bazel build tools) to update my
BUILD
files. Keep in mind it will reformat the
BUILD
file with the Bazel style which may be not what you want.
Copy code
$ buildozer 'set skip_yapf True' 'set skip_mypy True' 'set skip_flake8 True' ///...:%python_tests
$ buildozer 'set skip_yapf True' 'set skip_mypy True' 'set skip_flake8 True' ///...:%python_library
Running these have modified all libraries and tests targets to have those arguments. E.g.
Copy code
python_library(sources=["*/**/*.py", "!tests/"])
was turned into
Copy code
python_library(
    skip_flake8 = True,
    skip_mypy = True,
    skip_yapf = True,
    sources = [
        "*/**/*.py",
        "!tests/",
    ],
)
However, I run Python formatter on the
BUILD
files anyway afterwards, so that wasn’t a problem for me:
Copy code
python_library(
    skip_flake8=True,
    skip_mypy=True,
    skip_yapf=True,
    sources=[
        "*/**/*.py",
        "!tests/",
    ],
)
There are easy ways to have fine-grained control over what targets you’d like to run the editing command on, but you may need to consult the Bazel docs as they may differ from the ones used in Pants, e.g. editing only a single project files:
Copy code
$ buildozer 'set skip_yapf False' 'set skip_mypy False' projectdir/subdir:%python_library
There may be plans to have some functionality similar to what
buildozer
provides as core of Pants, but until then it may be a sensible approach for some of us. I think it may be worth experimenting with.
👀 1
📣 1
c

curved-television-6568

12/23/2021, 5:49 PM
Keep in mind it will reformat the 
BUILD
 file with the Bazel style which may be not what you want.
This is easily remedied in a follow up step with:
Copy code
./pants update-build-files
❤️ 1
h

hundreds-father-404

12/23/2021, 5:50 PM
Thanks for sharing! We currently have the
update-build-files
goal in Pants 2.8+, which gives a generic plugin hook for modifying BUILD files. Right now, it integrates Black and also has some deprecation fixers, like renaming
python_library
to
python_sources
It could be expanded to do other things like what you're talking about
❤️ 1
h

happy-kitchen-89482

12/23/2021, 5:53 PM
This is neat! I'm impressed it works
🎯 1
b

busy-vase-39202

01/04/2022, 6:03 PM
This could be a good blog post, pretty much verbatim. Any interest?
f

fresh-cat-90827

01/04/2022, 6:24 PM
Sure, happy to write 🙂 as long as you guys are happy to tell people that Bazel tools are okay to use 😉 my understanding is that you folks would be interested in having people moving from Bazel to Pants 🙂
b

busy-vase-39202

01/04/2022, 6:25 PM
You're essentially providing a window into one aspect of that movement. 😉
🙌 1
h

hundreds-father-404

01/04/2022, 6:27 PM
Imo, it's also definitely okay to use Bazel tools and Bazel itself too! Ultimately our goal is for people to have an excellent build experience, however they do that. It's part of why we're trying to put so much effort into incremental adoption, so you can use Pants in addition to other tools if that makes sense for your team: https://www.pantsbuild.org/docs/existing-repositories We hope to keep improving Pants such that you don't need to mix multiple tools, but I think it's important you still can it if makes sense for you
🙌 1
1
b

busy-vase-39202

01/04/2022, 6:30 PM
Yep. If a Bazel user can make the transition from Pants easier by using tools that they already are comfortable with, that's a win for them and for Pants. Thank you Bazel team 😉
f

fresh-cat-90827

01/04/2022, 6:35 PM
okay then 🙂 I’ll DM Carina to discuss the details
🚀 1
❤️ 2