Skip to content

Commit 2c9b670

Browse files
Rework lighthouse_version to reduce spurious recompilation (#8336)
#8311 Removes the `git_version` crate from `lighthouse_version` and implements git `HEAD` tracking manually. This removes the (mostly) broken dirty tracking but prevents spurious recompilation of the `lighthouse_version` crate. This also reworks the way crate versions are handled by utilizing workspace version inheritance and Cargo environment variables. This means the _only_ place where Lighthouse's version is defined is in the top level `Cargo.toml` for the workspace. All relevant binaries then inherit this version. This largely makes the `change_version.sh` script useless so I've removed it, although we could keep a version which just alters the workspace version (if we need to maintain compatibility with certain build/release tooling. ### When is a Rebuild Triggered? 1. When the build.rs file is changed. 2. When the HEAD commit changes (added, removed, rebased, etc) 3. When the branch changes (this includes changing to the current branch, and creating a detached HEAD) Note that working/staged changes will not trigger a recompile of `lighthouse_version`. Co-Authored-By: Mac L <[email protected]> Co-Authored-By: Michael Sproul <[email protected]>
1 parent c46cb0b commit 2c9b670

File tree

12 files changed

+110
-101
lines changed

12 files changed

+110
-101
lines changed

Cargo.lock

Lines changed: 3 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ resolver = "2"
9494

9595
[workspace.package]
9696
edition = "2024"
97+
version = "8.0.0-rc.2"
9798

9899
[workspace.dependencies]
99100
account_utils = { path = "common/account_utils" }

account_manager/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "account_manager"
3-
version = "0.3.5"
3+
version = { workspace = true }
44
authors = [
55
"Paul Hauner <[email protected]>",
66
"Luke Anderson <[email protected]>",

beacon_node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "beacon_node"
3-
version = "8.0.0-rc.2"
3+
version = { workspace = true }
44
authors = [
55
"Paul Hauner <[email protected]>",
66
"Age Manning <[email protected]",

boot_node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "boot_node"
3-
version = "8.0.0-rc.2"
3+
version = { workspace = true }
44
authors = ["Sigma Prime <[email protected]>"]
55
edition = { workspace = true }
66

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
[package]
22
name = "lighthouse_version"
3-
version = "0.1.0"
3+
version = { workspace = true }
44
authors = ["Sigma Prime <[email protected]>"]
55
edition = { workspace = true }
6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
8-
[dependencies]
9-
git-version = "0.3.4"
106

117
[dev-dependencies]
128
regex = { workspace = true }

common/lighthouse_version/build.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use std::env;
2+
use std::fs;
3+
use std::path::Path;
4+
use std::process::Command;
5+
6+
const CLIENT_NAME: &str = "Lighthouse";
7+
8+
fn main() {
9+
println!("cargo:rerun-if-changed=build.rs");
10+
11+
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
12+
let manifest_path = Path::new(&manifest_dir);
13+
14+
// The crate version is inherited from the workspace.
15+
let semantic_version = env::var("CARGO_PKG_VERSION").unwrap();
16+
17+
// Hardcode the .git/ path.
18+
// This assumes the `lighthouse_version` crate will never move.
19+
let git_dir = manifest_path.join("../../.git");
20+
21+
if git_dir.exists() {
22+
// HEAD either contains a commit hash directly (detached HEAD), or a reference to a branch.
23+
let head_path = git_dir.join("HEAD");
24+
if head_path.exists() {
25+
println!("cargo:rerun-if-changed={}", head_path.display());
26+
27+
if let Ok(head_content) = fs::read_to_string(&head_path) {
28+
let head_content = head_content.trim();
29+
30+
// If HEAD is a reference, also check that file.
31+
if let Some(ref_path) = head_content.strip_prefix("ref: ") {
32+
let full_ref_path = git_dir.join(ref_path);
33+
if full_ref_path.exists() {
34+
println!("cargo:rerun-if-changed={}", full_ref_path.display());
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
// Construct Lighthouse version string without commit hash.
42+
let base_version = format!("{}/v{}", CLIENT_NAME, semantic_version);
43+
44+
let commit_hash = get_git_hash(7);
45+
let commit_prefix = get_git_hash(8);
46+
47+
// If commit hash is valid, construct the full version string.
48+
let version = if !commit_hash.is_empty() && commit_hash.len() >= 7 {
49+
format!("{}-{}", base_version, commit_hash)
50+
} else {
51+
base_version
52+
};
53+
54+
println!("cargo:rustc-env=GIT_VERSION={}", version);
55+
println!("cargo:rustc-env=GIT_COMMIT_PREFIX={}", commit_prefix);
56+
println!("cargo:rustc-env=CLIENT_NAME={}", CLIENT_NAME);
57+
println!("cargo:rustc-env=SEMANTIC_VERSION={}", semantic_version);
58+
}
59+
60+
fn get_git_hash(len: usize) -> String {
61+
Command::new("git")
62+
.args(["rev-parse", &format!("--short={}", len), "HEAD"])
63+
.output()
64+
.ok()
65+
.and_then(|output| {
66+
if output.status.success() {
67+
String::from_utf8(output.stdout).ok()
68+
} else {
69+
None
70+
}
71+
})
72+
.map(|s| s.trim().to_string())
73+
.unwrap_or_else(|| {
74+
// Fallback commit prefix for execution engine reporting.
75+
if len == 8 {
76+
"00000000".to_string()
77+
} else {
78+
String::new()
79+
}
80+
})
81+
}
Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
1-
use git_version::git_version;
21
use std::env::consts;
32

43
/// Returns the current version of this build of Lighthouse.
54
///
6-
/// A plus-sign (`+`) is appended to the git commit if the tree is dirty.
75
/// Commit hash is omitted if the sources don't include git information.
86
///
97
/// ## Example
108
///
11-
/// `Lighthouse/v1.5.1-67da032+`
12-
pub const VERSION: &str = git_version!(
13-
args = [
14-
"--always",
15-
"--dirty=+",
16-
"--abbrev=7",
17-
// NOTE: using --match instead of --exclude for compatibility with old Git
18-
"--match=thiswillnevermatchlol"
19-
],
20-
prefix = "Lighthouse/v8.0.0-rc.2-",
21-
fallback = "Lighthouse/v8.0.0-rc.2"
22-
);
9+
/// `Lighthouse/v8.0.0-67da032`
10+
pub const VERSION: &str = env!("GIT_VERSION");
2311

2412
/// Returns the first eight characters of the latest commit hash for this build.
2513
///
2614
/// No indication is given if the tree is dirty. This is part of the standard
2715
/// for reporting the client version to the execution engine.
28-
pub const COMMIT_PREFIX: &str = git_version!(
29-
args = [
30-
"--always",
31-
"--abbrev=8",
32-
// NOTE: using --match instead of --exclude for compatibility with old Git
33-
"--match=thiswillnevermatchlol"
34-
],
35-
prefix = "",
36-
suffix = "",
37-
cargo_prefix = "",
38-
cargo_suffix = "",
39-
fallback = "00000000"
40-
);
16+
pub const COMMIT_PREFIX: &str = env!("GIT_COMMIT_PREFIX");
4117

4218
/// Returns `VERSION`, but with platform information appended to the end.
4319
///
4420
/// ## Example
4521
///
46-
/// `Lighthouse/v1.5.1-67da032+/x86_64-linux`
22+
/// `Lighthouse/v8.0.0-67da032/x86_64-linux`
4723
pub fn version_with_platform() -> String {
4824
format!("{}/{}-{}", VERSION, consts::ARCH, consts::OS)
4925
}
@@ -52,16 +28,16 @@ pub fn version_with_platform() -> String {
5228
///
5329
/// ## Example
5430
///
55-
/// `1.5.1`
31+
/// `8.0.0`
5632
pub fn version() -> &'static str {
57-
"8.0.0-rc.2"
33+
env!("SEMANTIC_VERSION")
5834
}
5935

6036
/// Returns the name of the current client running.
6137
///
6238
/// This will usually be "Lighthouse"
6339
pub fn client_name() -> &'static str {
64-
"Lighthouse"
40+
env!("CLIENT_NAME")
6541
}
6642

6743
#[cfg(test)]
@@ -72,7 +48,7 @@ mod test {
7248
#[test]
7349
fn version_formatting() {
7450
let re = Regex::new(
75-
r"^Lighthouse/v[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta).[0-9])?(-[[:xdigit:]]{7})?\+?$",
51+
r"^Lighthouse/v[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta)\.[0-9])?(-[[:xdigit:]]{7})?$",
7652
)
7753
.unwrap();
7854
assert!(
@@ -91,4 +67,14 @@ mod test {
9167
version()
9268
);
9369
}
70+
71+
#[test]
72+
fn client_name_is_lighthouse() {
73+
assert_eq!(client_name(), "Lighthouse");
74+
}
75+
76+
#[test]
77+
fn version_contains_semantic_version() {
78+
assert!(VERSION.contains(version()));
79+
}
9480
}

lcli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "lcli"
33
description = "Lighthouse CLI (modeled after zcli)"
4-
version = "8.0.0-rc.2"
4+
version = { workspace = true }
55
authors = ["Paul Hauner <[email protected]>"]
66
edition = { workspace = true }
77

lighthouse/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lighthouse"
3-
version = "8.0.0-rc.2"
3+
version = { workspace = true }
44
authors = ["Sigma Prime <[email protected]>"]
55
edition = { workspace = true }
66
autotests = false

0 commit comments

Comments
 (0)