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 Jun 24, 2024
1 parent 962e404 commit 513422b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
23 changes: 17 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,24 @@ impl LocalFingerprint {
key: K,
envs: &BTreeMap<String, Option<OsString>>,
) -> LocalFingerprint {
let key = key.as_ref();
fn get_env_value(key: &str, envs: &BTreeMap<String, Option<OsString>>) -> Option<OsString> {
if cfg!(windows) {
let upper_case_key: String = key.to_uppercase();
if let Some(v) = envs.get(&upper_case_key) {
return v.to_owned();
}
}
env::var_os(key)
}

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> = match envs.get(key) {
Some(v) => v.to_owned(),
None => get_env_value(key, envs),
}
.and_then(|os_str| os_str.into_string().ok());

LocalFingerprint::RerunIfEnvChanged { var, val }
}
Expand Down
40 changes: 25 additions & 15 deletions tests/testsuite/build_script_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ use cargo_test_support::str;

#[cargo_test]
fn rerun_if_env_changes_config() {
let build = if cfg!(windows) {
r#"
fn main() {
println!("cargo:rerun-if-env-changed=fOO");
println!("aaaaaa");
if let Ok(foo) = std::env::var("Foo") {
assert!(&foo != "bad");
}
}
"#
} else {
r#"
fn main() {
println!("cargo:rerun-if-env-changed=FOO");
if let Ok(foo) = std::env::var("FOO") {
assert!(&foo != "bad");
}
}
"#
};

let p = project()
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
.file("src/main.rs", "fn main() {}")
Expand All @@ -17,17 +38,7 @@ fn rerun_if_env_changes_config() {
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");
}
}
"#,
)
.file("build.rs", build)
.build();

p.cargo("check")
Expand All @@ -48,12 +59,11 @@ 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();
}

Expand Down

0 comments on commit 513422b

Please sign in to comment.