Hi folks! I’d like to be able to include the curre...
# general
m
Hi folks! I’d like to be able to include the current Git SHA in my build (ideally in the form of a
git_sha.py
file in a given package with the contents
git_sha = "a1b2c3d4…the whole lot"
). I have
adhoc_tool
and
shell_command
set up, but pants isn’t building the
git_sha.py
file, nor is it returning and errors, so I’m stuck! Details in thread🧵 (I know that
vcs_version
is intended for this kind of thing, but (a) I also can’t seem to get that to work and (b) the output format doesn’t contain the entire SHA.)
✅ 1
✅ 1
This is the contents of my `my_package/src/my_package/BUILD`:
Copy code
python_sources(
  dependencies=[":git_sha"]
)

adhoc_tool(
    name="git_sha",
    runnable=":generate_version",
    output_files=["git_sha.py"],
)

shell_command(
  name="generate_version",
  command="echo 'git_sha = \"$(git rev-parse HEAD)\"' > git_sha.py",
  tools=["git"],
  output_files=["git_sha.py"],
)
With a
poetry_requirements()
in the
my_package/BUILD
, and this package imported by a lambda in another part of this monorepo. (I have
pants.backend.shell
and
pants.backend.experimental.adhoc
in my pants.toml) When I run
pants package ::
from the root of my monorepo, I get my built lambda produced without the
git_sha.py
file present. What am I missing?
g
https://www.pantsbuild.org/dev/reference/targets/experimental_wrap_as_python_sources might be necessary to ensure it gets picked up as a codegenned target.
👀 1
m
Thank Tom; I’ll look through and see how I get on!
h
It’s a shame if vcs_version doesn’t work for this, since that is what it’s supposed to do
I guess that tries to extract semvers from tags, rather than just give you the git sha
m
@happy-kitchen-89482 yeah; it did seem like exactly the right thing! I can probably get away with the first 7 digits of the sha, as extracted from what
vcs_version
produces, though I initially couldn’t get vcs_version working either! It looks like
experimental_wrap_as_python_sources
was the missing piece of the puzzle — thanks @gorgeous-winter-99296! (though should it always be needed alongside
vcs_version
?) I’m still a little confused as to why the following
BUILD
in
my_package/src/my_package/
writes
git_sha.py
to the root of my lambda’s zip, rather than along side the
my_package
source files.
Copy code
python_sources(
  dependencies=[":build_git_sha"],
)

experimental_wrap_as_python_sources(
  name="build_git_sha",
  inputs=[":git_sha"],
)

vcs_version(
  name="git_sha",
  generate_to="git_sha.py",
  template='git_sha = "{version}".split("+g")[1].split(".")[0]',
)
Naturally I can
generate_to="my_package/git_sha.py"
but it will cause me some serious confusion if I ever rename my package (I don’t like defining things in two places where I can help it 😅) Any idea what I’m doing wrong?
e
You should be able to use https://www.pantsbuild.org/dev/reference/build-file-symbols/build_file_dir to get the path, so you don't need to define it twice
🙇‍♂️ 1
It's a
PurePath
so you can use
.parts[-1]
to get the name of your current directory. (It's still technically leaning on the directory structure rather than actually the package name, but it ought to work fairly well I think)
m
Thanks @elegant-florist-94385! I ended up doing:
Copy code
vcs_version(
    name="git_sha",
    generate_to=f"{build_file_dir().name}/git_sha.py",
    # This is a hack to extract the short sha from what pants easily provides
    template='git_sha = "{version}".split("+g")[1].split(".d")[0]',
)
Which works perfectly 😊 Cheers!
🔥 1