https://pantsbuild.org/ logo
#general
Title
# general
f

fresh-cat-90827

07/13/2021, 5:48 PM
Hopefully a simple one — what do you think would be the cleanest option to bring in versions values to
BUILD
files so that different targets such as
pex_binary
could use a value from an arbitrary file stored in a Python package within the monorepo?
I like how it’s used in Pants here https://github.com/pantsbuild/pants/blob/dcbedd422fcb3c52b85bfeae4f4fbde031ad69a3/src/python/pants/version.py#L17. But the help suggests that
Macros cannot import other modules, just like BUILD files cannot have import statements.
So I wonder how to bring in version into the
BUILD
file, if that’s possible at all. The use case would be to bring the version value in the
pex_binary
target:
Copy code
pex_binary(
  name=f'project-linux-{version}-cp3.6'
)
The reason I don’t want to define
version
in the
BUILD
file is because there are clients external to Pants who would be interested in obtaining the version value and it’s much easier to do this by reading a standalone file containing the version only (like here https://github.com/pantsbuild/pants/blob/dcbedd422fcb3c52b85bfeae4f4fbde031ad69a3/src/python/pants/VERSION) than reading a variable defined in the
BUILD
file.
👍 1
e

enough-analyst-54434

07/13/2021, 9:54 PM
This may suffice for your needs if they are simple:
Copy code
$ cat <<EOF > foo.py
version = "1.2.3"
EOF
$ cat <<EOF > BUILD.example
pex_binary(            
  name=f'project-linux-{version}-cp3.6'
)                                      
EOF
$ ./pants --build-file-prelude-globs=foo.py list //:
//:build_root
//:gitignore
//:pants_toml
//:project-linux-1.2.3-cp3.6
//:scripts
The hint is here: https://www.pantsbuild.org/docs/macros#how-to-add-a-macro . Now that's in the context of macros, but generally all symbols at the top-level in prelude-globs will be available without import.
2 Views