Include VCS information via cargo build-time environment variables

There is a popular (~2M downloads) crate called shadow-rs that adds useful build-time information to Rust binaries. This includes the git branch, tag and commit that the binary was built from.

In order to source this information it uses git2-rs, which in turn uses libgit2. This has historically been a bit annoying for packagers, who much prefer to avoid statically linking libgit2 (for example, homebrew uses the LIBGIT2_NO_VENDOR env var to force this).

It's a shame to have to add a large dependency like libgit2 in order to get build-time commit information. While there are other ways to do this, it seems that Cargo already uses libgit2 and is therefore able to fetch VCS information.

I think it would be a good idea to provide a CARGO_VCS_GIT_COMMIT and a CARGO_VCS_GIT_COMMIT_DATE build time environment variable, to surface this useful information without requiring libgit2?

2 Likes
1 Like

This indeed would be great, but I think the wider point is: why do I need to use a third party dependency for this? Can’t Cargo just provide this useful information for me?

As one example of a potential drawback, providing this information would mean a build from the .crate file is not the same as a build from the git repository. If we were going to do this in cargo, I would want to make sure we could supply the same information in either case so that builds can be reproducible.

1 Like

There is also GitHub - rust-secure-code/cargo-auditable: Make production Rust binaries auditable to embed all dependency versions for consumption by tools like cargo audit. It doesn't embed the commit hash though.

1 Like

Here's the information I gather for ghostflow-director binaries (via build.rs).

See this issue:

I noted this in the above issue. export-subst may be useful here.