alright, another question. I'm trying Go now :slig...
# general
d
alright, another question. I'm trying Go now 🙂 our packaging process embeds the Git commit hash in the Go binary by defining a "commit" variable in the main package, and passing "-X main.commit=..." to
go build
's
-ldflags
. Right now this is done with a Python script that invokes
git
to get the current commit hash. with Pants, I have a
go_binary
target, and I set the
ldflags
using
linker_flags
. Is there some way of achieving the behavior we have? options I've thought of: • pass the commit hash through an environment variable, like
GIT_COMMIT_HASH=$( git rev-parse HEAD ) pants package my/binary
, but this kind of manual steps sort of defeats the purpose of pants • invoke pants through a wrapper script to pass that environment variable
a
I've not looked at the Go backend. But you might be able to write a small plugin to provide it.
but fwiw, your approach will break Pants' caching on builds.
d
hmm yeah that makes sense... but i'm not sure we can avoid having to embed the commit hash
i'm trying to figure out how this kind of requirement fits into the pants conceptual model. Is it fair to say that this kind of packaging process isn't something that
pants package
is designed for?
like maybe it should be handled by a separate script that invokes
pants
a
I don't think it's wrong, it's just different. There's the
vcs_version
target that does something similar
d
oh interesting, i'll take a look at that
a
see "Generating version tags from git"
d
https://www.pantsbuild.org/2.19/docs/using-pants/generating-version-tags-from-git wow, that's exactly what I needed! amazing, thank you
qq: I added a vcs_version target and I'd like to check its output, Is there some way to run it and see the generated file? Something like
bazel build :my_target
?
hacked this by adding a
run_shell_command
that just does
cat {chroot}/output.txt
and has the vcs_version target as an executable dependency
ah,
pants export-codegen <target>
c
You can do something like
Copy code
$ cat .pants.bootstrap 
export LOCALDEV_COMMIT_SHA=$(git rev-parse HEAD)
export LOCALDEV_COMMIT_SHORT_SHA=$(git rev-parse --short HEAD)
export LOCALDEV_COMMIT_BRANCH=$(git symbolic-ref --short -q HEAD)
to automatically populate some env vars https://github.com/pantsbuild/pants/issues/19981 is the feature request for a more general mechanism
d
how do I get Pants to read a bootstrap file like this?
c
You place a file called
pants.bootstrap
and the root of your repository
d
oh that's awesome, thank you