Skip to content

Commit

Permalink
Make it case-insensitive in Windows environment and add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
heisen-li committed Jul 1, 2024
1 parent 962e404 commit d19af49
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 16 deletions.
28 changes: 22 additions & 6 deletions src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,29 @@ impl LocalFingerprint {
key: K,
envs: &BTreeMap<String, Option<OsString>>,
) -> LocalFingerprint {
let key = key.as_ref();
fn get_envs_case_insensitive(
key: &str,
envs: &BTreeMap<String, Option<OsString>>,
) -> Option<OsString> {
let upper_case_key: String = key.to_uppercase();
for (k, v) in envs {
if k.to_uppercase().eq(&upper_case_key) {
return v.to_owned();
}
}
None
}

let key: &str = key.as_ref();
let var = key.to_owned();
let val = envs
.get(key)
.map(|v| v.to_owned())
.or_else(|| Some(env::var_os(key)))
.and_then(|os_str| os_str?.into_string().ok());

let val: Option<String> = if cfg!(windows) {
get_envs_case_insensitive(key, envs)
} else {
envs.get(key).and_then(|v| v.to_owned())
}
.xor(env::var_os(key))
.and_then(|os_str| os_str.into_string().ok());

LocalFingerprint::RerunIfEnvChanged { var, val }
}
Expand Down
71 changes: 61 additions & 10 deletions tests/testsuite/build_script_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ fn rerun_if_env_changes_config() {
.file(
"build.rs",
r#"
fn main() {
println!("cargo:rerun-if-env-changed=FOO");
if let Ok(foo) = std::env::var("FOO") {
assert!(&foo != "bad");
}
}
"#,
fn main() {
println!("cargo:rerun-if-env-changed=FOO");
if let Ok(foo) = std::env::var("FOO") {
assert!(&foo != "bad");
}
}
"#,
)
.build();

Expand All @@ -48,12 +48,63 @@ fn rerun_if_env_changes_config() {

p.cargo("check")
.with_status(101)
.with_stderr_data(
"\
.with_stderr_data(str![[r#"
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
...",
...
"#]])
.run();
}

#[cfg(windows)]
#[cargo_test]
fn rerun_if_env_changes_config_in_windows() {
let p = project()
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config.toml",
r#"
[env]
Foo = "good"
"#,
)
.file(
"build.rs",
r#"
fn main() {
println!("cargo:rerun-if-env-changed=foo");
if let Ok(foo) = std::env::var("FOo") {
assert!(&foo != "bad");
}
}
"#,
)
.build();

p.cargo("check")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();

p.change_file(
".cargo/config.toml",
r#"
[env]
FoO = "bad"
"#,
);

p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
...
"#]])
.run();
}

Expand Down

0 comments on commit d19af49

Please sign in to comment.