Skip to content

Commit 513422b

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

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -775,13 +775,24 @@ 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+
if let Some(v) = envs.get(&upper_case_key) {
782+
return v.to_owned();
783+
}
784+
}
785+
env::var_os(key)
786+
}
787+
788+
let key: &str = key.as_ref();
779789
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());
790+
791+
let val: Option<String> = match envs.get(key) {
792+
Some(v) => v.to_owned(),
793+
None => get_env_value(key, envs),
794+
}
795+
.and_then(|os_str| os_str.into_string().ok());
785796

786797
LocalFingerprint::RerunIfEnvChanged { var, val }
787798
}

tests/testsuite/build_script_env.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ 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+
println!("aaaaaa");
15+
if let Ok(foo) = std::env::var("Foo") {
16+
assert!(&foo != "bad");
17+
}
18+
}
19+
"#
20+
} else {
21+
r#"
22+
fn main() {
23+
println!("cargo:rerun-if-env-changed=FOO");
24+
if let Ok(foo) = std::env::var("FOO") {
25+
assert!(&foo != "bad");
26+
}
27+
}
28+
"#
29+
};
30+
1031
let p = project()
1132
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
1233
.file("src/main.rs", "fn main() {}")
@@ -17,17 +38,7 @@ fn rerun_if_env_changes_config() {
1738
FOO = "good"
1839
"#,
1940
)
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-
)
41+
.file("build.rs", build)
3142
.build();
3243

3344
p.cargo("check")
@@ -48,12 +59,11 @@ fn rerun_if_env_changes_config() {
4859

4960
p.cargo("check")
5061
.with_status(101)
51-
.with_stderr_data(
52-
"\
62+
.with_stderr_data(str![[r#"
5363
[COMPILING] foo v0.1.0 ([ROOT]/foo)
5464
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
55-
...",
56-
)
65+
...
66+
"#]])
5767
.run();
5868
}
5969

0 commit comments

Comments
 (0)