Skip to content

Commit 08bbb58

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

File tree

2 files changed

+80
-16
lines changed

2 files changed

+80
-16
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_envs_case_insensitive(key: &str, envs: &BTreeMap<String, Option<OsString>>) -> Option<OsString> {
779+
let upper_case_key: String = key.to_uppercase();
780+
for (k, v) in envs {
781+
if k.to_uppercase().eq(&upper_case_key) {
782+
return v.to_owned();
783+
}
784+
}
785+
None
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> = if cfg!(windows) {
792+
get_envs_case_insensitive(key, envs)
793+
} else {
794+
envs.get(key).and_then(|v| v.to_owned())
795+
}
796+
.xor(env::var_os(key))
797+
.and_then(|os_str| os_str.into_string().ok());
785798

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

tests/testsuite/build_script_env.rs

+61-10
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ fn rerun_if_env_changes_config() {
2020
.file(
2121
"build.rs",
2222
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-
"#,
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+
"#,
3030
)
3131
.build();
3232

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

4949
p.cargo("check")
5050
.with_status(101)
51-
.with_stderr_data(
52-
"\
51+
.with_stderr_data(str![[r#"
5352
[COMPILING] foo v0.1.0 ([ROOT]/foo)
5453
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
55-
...",
54+
...
55+
"#]])
56+
.run();
57+
}
58+
59+
#[cfg(windows)]
60+
#[cargo_test]
61+
fn rerun_if_env_changes_config_in_windows() {
62+
let p = project()
63+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
64+
.file("src/main.rs", "fn main() {}")
65+
.file(
66+
".cargo/config.toml",
67+
r#"
68+
[env]
69+
Foo = "good"
70+
"#,
5671
)
72+
.file(
73+
"build.rs",
74+
r#"
75+
fn main() {
76+
println!("cargo:rerun-if-env-changed=foo");
77+
if let Ok(foo) = std::env::var("FOo") {
78+
assert!(&foo != "bad");
79+
}
80+
}
81+
"#,
82+
)
83+
.build();
84+
85+
p.cargo("check")
86+
.with_stderr_data(str![[r#"
87+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
88+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
89+
90+
"#]])
91+
.run();
92+
93+
p.change_file(
94+
".cargo/config.toml",
95+
r#"
96+
[env]
97+
FoO = "bad"
98+
"#,
99+
);
100+
101+
p.cargo("check")
102+
.with_status(101)
103+
.with_stderr_data(str![[r#"
104+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
105+
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
106+
...
107+
"#]])
57108
.run();
58109
}
59110

0 commit comments

Comments
 (0)