Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions crates/fe/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::process::Command;

fn main() {
println!("cargo:rerun-if-changed=.git/HEAD");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Recompute git hash when HEAD commit changes

cargo:rerun-if-changed=.git/HEAD does not reliably invalidate this build script for new commits, so FE_GIT_HASH can go stale and fe --version can report an old hash. In this crate (crates/fe), .git/HEAD is not the repo’s HEAD file, and even if the path were corrected, HEAD’s contents usually stay unchanged while advancing commits on the same branch; as a result, rebuilding after a new commit (without touching source files) may keep the previous hash.

Useful? React with 👍 / 👎.

println!("cargo:rerun-if-env-changed=FE_GIT_HASH");

if let Ok(override_hash) = std::env::var("FE_GIT_HASH")
&& !override_hash.trim().is_empty()
{
println!("cargo:rustc-env=FE_GIT_HASH={}", override_hash.trim());
return;
}

let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output();

let Some(hash) = output
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.filter(|s| !s.is_empty())
else {
return;
};

println!("cargo:rustc-env=FE_GIT_HASH={hash}");
}
13 changes: 12 additions & 1 deletion crates/fe/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod tree;
mod workspace_ingot;

use std::fs;
use std::sync::OnceLock;

use build::build;
use camino::Utf8PathBuf;
Expand Down Expand Up @@ -52,8 +53,18 @@ pub enum BuildEmit {
Abi,
}

fn cli_version() -> &'static str {
static VERSION: OnceLock<String> = OnceLock::new();
VERSION
.get_or_init(|| match option_env!("FE_GIT_HASH") {
Some(hash) if !hash.is_empty() => format!("{} ({hash})", env!("CARGO_PKG_VERSION")),
_ => env!("CARGO_PKG_VERSION").to_string(),
})
.as_str()
}

#[derive(Debug, Clone, Parser)]
#[command(version, about, long_about = None)]
#[command(version = cli_version(), about, long_about = None)]
pub struct Options {
/// Control colored output (auto, always, never).
#[arg(long, global = true, value_enum, default_value = "auto")]
Expand Down
Loading