Skip to content

Commit cbe3643

Browse files
committed
Make it case-insensitive in Windows environment and add examples
1 parent 962e404 commit cbe3643

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

src/cargo/core/compiler/fingerprint/mod.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -775,13 +775,26 @@ impl LocalFingerprint {
775775
key: K,
776776
envs: &BTreeMap<String, Option<OsString>>,
777777
) -> LocalFingerprint {
778-
let key = key.as_ref();
778+
fn get_env_value(key: &str, envs: &BTreeMap<String, Option<OsString>>) -> Option<OsString> {
779+
if cfg!(windows) {
780+
let upper_case_key: String = key.to_uppercase();
781+
for (k, v) in envs {
782+
if k.to_uppercase().eq(&upper_case_key) {
783+
return v.to_owned();
784+
}
785+
}
786+
}
787+
env::var_os(key)
788+
}
789+
790+
let key: &str = key.as_ref();
779791
let var = key.to_owned();
780-
let val = envs
781-
.get(key)
782-
.map(|v| v.to_owned())
783-
.or_else(|| Some(env::var_os(key)))
784-
.and_then(|os_str| os_str?.into_string().ok());
792+
793+
let val: Option<String> = match envs.get(key) {
794+
Some(v) => v.to_owned(),
795+
None => get_env_value(key, envs),
796+
}
797+
.and_then(|os_str| os_str.into_string().ok());
785798

786799
LocalFingerprint::RerunIfEnvChanged { var, val }
787800
}

tests/testsuite/build_script_env.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ use cargo_test_support::str;
77

88
#[cargo_test]
99
fn rerun_if_env_changes_config() {
10+
let build = if cfg!(windows) {
11+
r#"
12+
fn main() {
13+
println!("cargo:rerun-if-env-changed=fOO");
14+
if let Ok(foo) = std::env::var("Foo") {
15+
assert!(&foo != "bad");
16+
}
17+
}
18+
"#
19+
} else {
20+
r#"
21+
fn main() {
22+
println!("cargo:rerun-if-env-changed=FOO");
23+
if let Ok(foo) = std::env::var("FOO") {
24+
assert!(&foo != "bad");
25+
}
26+
}
27+
"#
28+
};
29+
1030
let p = project()
1131
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
1232
.file("src/main.rs", "fn main() {}")
@@ -17,17 +37,7 @@ fn rerun_if_env_changes_config() {
1737
FOO = "good"
1838
"#,
1939
)
20-
.file(
21-
"build.rs",
22-
r#"
23-
fn main() {
24-
println!("cargo:rerun-if-env-changed=FOO");
25-
if let Ok(foo) = std::env::var("FOO") {
26-
assert!(&foo != "bad");
27-
}
28-
}
29-
"#,
30-
)
40+
.file("build.rs", build)
3141
.build();
3242

3343
p.cargo("check")
@@ -48,12 +58,11 @@ fn rerun_if_env_changes_config() {
4858

4959
p.cargo("check")
5060
.with_status(101)
51-
.with_stderr_data(
52-
"\
61+
.with_stderr_data(str![[r#"
5362
[COMPILING] foo v0.1.0 ([ROOT]/foo)
5463
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
55-
...",
56-
)
64+
...
65+
"#]])
5766
.run();
5867
}
5968

0 commit comments

Comments
 (0)