bulky-alarm-94027
05/16/2022, 9:47 AMgit describe
curved-television-6568
05/16/2022, 11:02 AMexperimental_shell_command
for how to run some arbitrary commands during a build.
https://www.pantsbuild.org/docs/run-shell-commandsbitter-ability-32190
05/16/2022, 11:33 AMexperimental_shell_command
or any other Pants-native target will be run in a sandbox outside of your repo (for hermeticity).
What that means is you essentially aren't able to run git
commands.bitter-ability-32190
05/16/2022, 11:35 AMbitter-ability-32190
05/16/2022, 11:36 AMcurved-television-6568
05/16/2022, 11:37 AMGIT_DIR
), but not the prettiest. Another option is to run the git describe
prior to the ./pants …
and inject the value as an env var for the shell command, if it’s not dynamic in nature.curved-television-6568
05/16/2022, 11:37 AMbulky-alarm-94027
05/16/2022, 1:06 PMexperimental_shell_command
but it seemed that i would have to end up using sed
or awk
which felt counterintuitive for a build system (e.g. ant
has a replace
task, or in gradle by creating a function that does it). I did not know that i would not have access to git which also makes it awkward.
I also looked a plugins and this seemed to be the best option, but it also felt like overkill.bitter-ability-32190
05/16/2022, 1:16 PMbulky-alarm-94027
05/16/2022, 1:16 PMbitter-ability-32190
05/16/2022, 2:22 PMexperimental_shell_command
to run that.
Then it's a matter of:
• Getting the git command to work
• Ensuring the cache "does the right thing"
So here's a gist you can add easily as a plugin to get the absolute path to the repo root. Note it is an abs path so remote caching is right out.
And here's a gist for a plugin which will code generate a file containing the current git SHA. When used a dependency it will invalidate the relevant rules when git SHA changes. YMMV it's a hack.
In your case, you might be able to just twist the latter code to generate the metadata.json
for you (it already has the build root, see line 47)bitter-ability-32190
05/16/2022, 2:23 PMbulky-alarm-94027
05/16/2022, 2:23 PMhappy-kitchen-89482
05/16/2022, 2:45 PMsetuptools_scm
specifically (and that calls out to git). It sounds like you want a more generic version of that?happy-kitchen-89482
05/16/2022, 2:46 PMexperimental_git_command
or somethingbitter-ability-32190
05/16/2022, 2:47 PMhappy-kitchen-89482
05/16/2022, 2:48 PMexperimental_shell_command
by setting GIT_DIR and GIT_WORK_TREE ?bitter-ability-32190
05/16/2022, 2:48 PMhappy-kitchen-89482
05/16/2022, 2:50 PM{buildroot}
the same way we support {chroot}
happy-kitchen-89482
05/16/2022, 2:50 PMGIT_DIR={buildroot}/.git
seems reasonablebitter-ability-32190
05/16/2022, 2:51 PMhappy-kitchen-89482
05/16/2022, 2:51 PMhappy-kitchen-89482
05/16/2022, 2:51 PMbitter-ability-32190
05/16/2022, 2:51 PMgitstate
target)happy-kitchen-89482
05/16/2022, 2:51 PMhappy-kitchen-89482
05/16/2022, 2:52 PMhappy-kitchen-89482
05/16/2022, 2:52 PMexperimental_shell_command
can revert all your work in your real buildroothappy-kitchen-89482
05/16/2022, 2:52 PMbitter-ability-32190
05/16/2022, 2:55 PMcurved-television-6568
05/16/2022, 3:03 PM.git
dir into the sandbox as a bare clone (no worktree)
From there you can do all kinds of git-fu in read only mode.bulky-alarm-94027
05/16/2022, 3:13 PMbitter-ability-32190
05/16/2022, 3:15 PMexperimental_shell_command
which is hermetic (doesn't get invalidated). Then the dep graph is sound although the metadata is stale.
Then for our docker images (or really anything we want to package) I explicitly tell pants to ignore that experimental_shell_command
and use an identical one which depends on that gitstate
target I provided earlier. So then it's cached behavior depends on commits, but we arne't building docker images every PR, so it's OK