Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,15 @@ dependencies = [
name = "build_info"
version = "0.0.0"
dependencies = [
"build_rs_git_info",
"inspect",
"vergen",
]

[[package]]
name = "build_rs_git_info"
version = "0.0.0"
dependencies = [
"anyhow",
]

[[package]]
Expand Down Expand Up @@ -7575,7 +7582,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "743bd48c283afc0388f9b8827b976905fb217ad9e647fae3a379a9283c4def2c"
dependencies = [
"deranged",
"itoa",
"libc",
"num-conv",
"num_threads",
Expand Down Expand Up @@ -8352,6 +8358,7 @@ name = "underhill_crash"
version = "0.0.0"
dependencies = [
"anyhow",
"build_rs_git_info",
"fs-err",
"futures",
"get_protocol",
Expand All @@ -8362,7 +8369,6 @@ dependencies = [
"tracing",
"tracing-subscriber",
"underhill_confidentiality",
"vergen",
"vmbus_async",
"vmbus_user_channel",
"zerocopy",
Expand Down Expand Up @@ -8400,13 +8406,13 @@ name = "underhill_init"
version = "0.0.0"
dependencies = [
"anyhow",
"build_rs_git_info",
"fs-err",
"kmsg_defs",
"libc",
"log",
"nix 0.30.1",
"underhill_confidentiality",
"vergen",
"walkdir",
]

Expand Down Expand Up @@ -8590,18 +8596,6 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"

[[package]]
name = "vergen"
version = "8.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2990d9ea5967266ea0ccf413a4aa5c42a93dbcfda9cb49a97de6931726b12566"
dependencies = [
"anyhow",
"cfg-if",
"rustversion",
"time",
]

[[package]]
name = "version_check"
version = "0.9.5"
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pipette_protocol = { path = "petri/pipette_protocol" }

# support crates
address_filter = { path = "support/address_filter" }
build_rs_git_info = { path = "support/build_rs_git_info" }
arc_cyclic_builder = { path = "support/arc_cyclic_builder" }
atomic_ringbuf = { path = "support/atomic_ringbuf" }
cache_topology = { path = "support/cache_topology" }
Expand Down Expand Up @@ -553,7 +554,6 @@ typed-path = "0.11"
uefi = "0.35.0"
unicycle = "0.10.2"
urlencoding = "2.1.3"
vergen = "8.2"
vfio-bindings = "0.4.0"
walkdir = "2.3"
wchar = "0.11"
Expand Down
2 changes: 1 addition & 1 deletion openhcl/build_info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ rust-version.workspace = true
inspect.workspace = true

[build-dependencies]
vergen = { workspace = true, features = ["git", "gitcl"] }
build_rs_git_info.workspace = true

[lints]
workspace = true
2 changes: 1 addition & 1 deletion openhcl/build_info/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#![expect(missing_docs)]

fn main() {
vergen::EmitBuilder::builder().all_git().emit().unwrap();
build_rs_git_info::emit_git_info().unwrap();
}
4 changes: 2 additions & 2 deletions openhcl/build_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ impl BuildInfo {
// structure to be closer to PODs.
Self {
crate_name: env!("CARGO_PKG_NAME"),
revision: if let Some(r) = option_env!("VERGEN_GIT_SHA") {
revision: if let Some(r) = option_env!("BUILD_GIT_SHA") {
r
} else {
""
},
branch: if let Some(b) = option_env!("VERGEN_GIT_BRANCH") {
branch: if let Some(b) = option_env!("BUILD_GIT_BRANCH") {
b
} else {
""
Expand Down
2 changes: 1 addition & 1 deletion openhcl/underhill_crash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tracing-subscriber.workspace = true
zerocopy.workspace = true

[build-dependencies]
vergen = { workspace = true, features = ["git", "gitcl"] }
build_rs_git_info.workspace = true

[lints]
workspace = true
2 changes: 1 addition & 1 deletion openhcl/underhill_crash/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#![expect(missing_docs)]

fn main() {
vergen::EmitBuilder::builder().all_git().emit().unwrap();
build_rs_git_info::emit_git_info().unwrap();
}
2 changes: 1 addition & 1 deletion openhcl/underhill_crash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub fn main() -> ! {

let os_version = OsVersionInfo::new();

let crate_revision = option_env!("VERGEN_GIT_SHA").unwrap_or("UNKNOWN_REVISION");
let crate_revision = option_env!("BUILD_GIT_SHA").unwrap_or("UNKNOWN_REVISION");
let openhcl_version = option_env!("OPENHCL_VERSION").unwrap_or("UNKNOWN_VERSION");

let os_version_major = os_version.major();
Expand Down
2 changes: 1 addition & 1 deletion openhcl/underhill_init/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ nix = { workspace = true, features = ["ioctl"] }
walkdir.workspace = true

[build-dependencies]
vergen = { workspace = true, features = ["git", "gitcl"] }
build_rs_git_info.workspace = true

[lints]
workspace = true
2 changes: 1 addition & 1 deletion openhcl/underhill_init/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#![expect(missing_docs)]

fn main() {
vergen::EmitBuilder::builder().all_git().emit().unwrap();
build_rs_git_info::emit_git_info().unwrap();
}
4 changes: 2 additions & 2 deletions openhcl/underhill_init/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ fn do_main() -> anyhow::Result<()> {
log::info!(
"Initial process: crate_name={}, crate_revision={}, crate_branch={}",
env!("CARGO_PKG_NAME"),
option_env!("VERGEN_GIT_SHA").unwrap_or("UNKNOWN_REVISION"),
option_env!("VERGEN_GIT_BRANCH").unwrap_or("UNKNOWN_BRANCH"),
option_env!("BUILD_GIT_SHA").unwrap_or("UNKNOWN_REVISION"),
option_env!("BUILD_GIT_BRANCH").unwrap_or("UNKNOWN_BRANCH"),
);

let stat_files = [
Expand Down
13 changes: 13 additions & 0 deletions support/build_rs_git_info/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

[package]
name = "build_rs_git_info"
edition.workspace = true
rust-version.workspace = true

[dependencies]
anyhow.workspace = true

[lints]
workspace = true
52 changes: 52 additions & 0 deletions support/build_rs_git_info/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Build-script helper that emits `BUILD_GIT_SHA` and `BUILD_GIT_BRANCH`
//! cargo environment variables by invoking the `git` CLI.

use std::process::Command;

fn git_output(args: &[&str]) -> anyhow::Result<String> {
let output = Command::new("git").args(args).output()?;

if !output.status.success() {
anyhow::bail!(
"git {:?} failed with code {:?}: {}",
args,
output.status.code(),
String::from_utf8_lossy(&output.stderr)
);
}

let output = String::from_utf8(output.stdout).unwrap().trim().to_owned();
Ok(output)
}

fn git_path(args: &[&str]) -> anyhow::Result<std::path::PathBuf> {
let output = git_output(args)?;
Ok(std::path::absolute(&output)?)
}

/// Emit git information as `cargo:rustc-env` variables so they are available via
/// `env!()` / `option_env!()` in the consuming crate.
pub fn emit_git_info() -> anyhow::Result<()> {
// Always rerun when HEAD changes (e.g. branch switch).
let head_path = git_path(&["rev-parse", "--git-path", "HEAD"])?;
println!("cargo:rerun-if-changed={}", head_path.display());

// If HEAD is a symbolic ref (i.e. points at a branch), also watch the
// branch ref file so we rebuild when new commits land on that branch.
if let Ok(head_ref) = git_output(&["symbolic-ref", "HEAD"]) {
// e.g. refs/heads/main → .git/refs/heads/main (or the worktree equivalent)
let ref_path = git_path(&["rev-parse", "--git-path", &head_ref])?;
println!("cargo:rerun-if-changed={}", ref_path.display());
}

Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cargo:rerun-if-changed only watches HEAD and (if symbolic) the loose ref file (e.g. .git/refs/heads/main). If refs are packed (common after git gc / git pack-refs), that ref file may not exist and updates like git commit --amend won’t trigger a rebuild, leaving BUILD_GIT_SHA stale. Consider also watching packed-refs (via git rev-parse --git-path packed-refs) unconditionally, or otherwise ensuring ref updates are tracked even when refs are packed.

Suggested change
// Also watch the packed-refs file so ref updates are tracked even when
// branch refs are packed and the loose ref files no longer exist.
let packed_refs_path = git_path(&["rev-parse", "--git-path", "packed-refs"])?;
println!("cargo:rerun-if-changed={}", packed_refs_path.display());

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chatted with @smalis-msft offline. He experimented, and found that this is incorrect. This code will trigger a rebuild if that file doesn't exist.

let sha = git_output(&["rev-parse", "HEAD"])?;
let branch = git_output(&["rev-parse", "--abbrev-ref", "HEAD"])?;

println!("cargo:rustc-env=BUILD_GIT_SHA={sha}");
println!("cargo:rustc-env=BUILD_GIT_BRANCH={branch}");

Ok(())
}