mammoth-dawn-85816
09/22/2025, 8:20 AMgit_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.)mammoth-dawn-85816
09/22/2025, 8:21 AMpython_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?gorgeous-winter-99296
09/22/2025, 12:00 PMgorgeous-winter-99296
09/22/2025, 12:02 PMmammoth-dawn-85816
09/22/2025, 12:06 PMhappy-kitchen-89482
09/22/2025, 12:39 PMhappy-kitchen-89482
09/22/2025, 12:41 PMmammoth-dawn-85816
09/22/2025, 1:07 PMvcs_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.
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?elegant-florist-94385
09/22/2025, 8:12 PMelegant-florist-94385
09/22/2025, 8:13 PMPurePath 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)mammoth-dawn-85816
09/29/2025, 6:26 AMvcs_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!