Bladeren bron

build: bake SM121_VERSION through a generated source, not option_env alone

Warm caches kept the old literal even with rerun-if-env-changed (the add-on
images 1.3.6-1.3.8 all shipped 'dev'). build.rs now writes OUT_DIR/build_version.rs
from the env and echoes the value as a cargo warning, so the CI log states what
the image got and the badge cannot go stale.
Alexander Korolev 8 uur geleden
bovenliggende
commit
60ffddaa38
2 gewijzigde bestanden met toevoegingen van 21 en 13 verwijderingen
  1. 14 6
      build.rs
  2. 7 7
      src/build_info.rs

+ 14 - 6
build.rs

@@ -1,10 +1,18 @@
-//! Rebuild the crate when the build identity changes.
+//! Build identity for the dashboard badge.
 //!
-//! `option_env!("SM121_VERSION")` is baked at compile time, but cargo does not
-//! treat an env var as an input on its own — without this line a cached build
-//! keeps the old value (the add-on's 1.3.6/1.3.7 images both shipped "dev"
-//! from a warm `target/` cache mount). `cargo:rerun-if-env-changed` makes the
-//! crate recompile whenever the version passed by the CI changes.
+//! `SM121_VERSION` is passed by the add-on image build (`--build-arg` →
+//! `ENV` in the builder stage). Two things are needed for it to survive a
+//! warm cargo cache: this script must declare the env var as an input, and the
+//! value must reach the compiler as a *generated source*, not through
+//! `option_env!` alone (a cached fingerprint can otherwise keep the old
+//! literal). The value is echoed as a cargo warning so the CI log shows what
+//! the image actually got.
 fn main() {
     println!("cargo:rerun-if-env-changed=SM121_VERSION");
+    let version = std::env::var("SM121_VERSION").unwrap_or_else(|_| "dev".into());
+    println!("cargo:warning=SM121_VERSION={version}");
+    let out = std::path::PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR"))
+        .join("build_version.rs");
+    std::fs::write(out, format!("pub const VERSION: &str = {version:?};\n"))
+        .expect("write build_version.rs");
 }

+ 7 - 7
src/build_info.rs

@@ -1,15 +1,15 @@
 //! Build-time identity of this binary.
 //!
-//! The add-on image is built with `SM121_VERSION=<add-on version>` (a Docker
-//! build arg the CI passes from `sm121/config.yaml`), so the running dashboard
-//! can show which release it is. A plain `cargo build` reports `dev`.
-//!
-//! `option_env!` rather than `env!`: a local build has no such variable and
-//! must still compile.
+//! The version comes from `SM121_VERSION` at compile time: the add-on CI passes
+//! it as a Docker build arg, the builder stage exports it, and `build.rs` writes
+//! it into `OUT_DIR/build_version.rs` (so a warm cargo cache cannot keep the old
+//! literal). A plain `cargo build` reports `dev`.
+
+include!(concat!(env!("OUT_DIR"), "/build_version.rs"));
 
 /// The version baked in at build time (`dev` for a local build).
 pub fn version() -> &'static str {
-    option_env!("SM121_VERSION").unwrap_or("dev")
+    VERSION
 }
 
 #[cfg(test)]