Skip to content

Commit f4ce3f6

Browse files
committed
Avoid rerun-if-env-changed on vars set by cargo for build scripts
1 parent 019603a commit f4ce3f6

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2854,11 +2854,25 @@ impl Build {
28542854
}
28552855

28562856
fn getenv(&self, v: &str) -> Option<String> {
2857+
// Returns true for environment variables cargo sets for build scripts:
2858+
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
2859+
//
2860+
// This handles more of the vars than we actually use (it tries to check
2861+
// complete-ish set), just to avoid needing maintenance if/when new
2862+
// calls to `getenv`/`getenv_unwrap` are added.
2863+
fn provided_by_cargo(envvar: &str) -> bool {
2864+
match envvar {
2865+
v if v.starts_with("CARGO") || v.starts_with("RUSTC") => true,
2866+
"HOST" | "TARGET" | "RUSTDOC" | "OUT_DIR" | "OPT_LEVEL" | "DEBUG" | "PROFILE"
2867+
| "NUM_JOBS" | "RUSTFLAGS" => true,
2868+
_ => false,
2869+
}
2870+
}
28572871
let mut cache = self.env_cache.lock().unwrap();
28582872
if let Some(val) = cache.get(v) {
28592873
return val.clone();
28602874
}
2861-
if self.emit_rerun_if_env_changed {
2875+
if self.emit_rerun_if_env_changed && !provided_by_cargo(v) {
28622876
self.print(&format!("cargo:rerun-if-env-changed={}", v));
28632877
}
28642878
let r = env::var(v).ok();

0 commit comments

Comments
 (0)