From 265ab3b5141f9be9064e0657ca999602a42226c1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 23 Oct 2024 14:44:49 -0700 Subject: [PATCH] Use $CARGO_PKG_VERSION from buildscript exec-time instead of build-time --- build.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 9518c8d..4fc36f7 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,7 @@ +use std::env; +use std::ffi::OsString; +use std::process; + fn main() { println!("cargo:rerun-if-changed=build.rs"); @@ -5,5 +9,13 @@ fn main() { println!("cargo:rustc-check-cfg=cfg(prettyplease_debug)"); println!("cargo:rustc-check-cfg=cfg(prettyplease_debug_indent)"); - println!(concat!("cargo:VERSION=", env!("CARGO_PKG_VERSION"))); + let pkg_version = cargo_env_var("CARGO_PKG_VERSION"); + println!("cargo:VERSION={}", pkg_version.to_str().unwrap()); +} + +fn cargo_env_var(key: &str) -> OsString { + env::var_os(key).unwrap_or_else(|| { + eprintln!("Environment variable ${key} is not set during execution of build script"); + process::exit(1); + }) }