Skip to content

Commit ac05a85

Browse files
committed
Don't allow config env to modify vars set by cargo
1 parent 81537ee commit ac05a85

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,12 @@ impl<'cfg> Compilation<'cfg> {
348348
if self.config.cli_unstable().configurable_env {
349349
// Apply any environment variables from the config
350350
for (key, value) in self.config.env_config()?.iter() {
351-
if value.is_force() || cmd.get_env(key).is_none() {
351+
// never override a value that has already been set by cargo
352+
if cmd.get_envs().contains_key(key) {
353+
continue;
354+
}
355+
356+
if value.is_force() || env::var_os(key).is_none() {
352357
cmd.env(key, value.resolve(self.config));
353358
}
354359
}

tests/testsuite/cargo_env_config.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,31 @@ fn env_relative() {
131131
.masquerade_as_nightly_cargo()
132132
.run();
133133
}
134+
135+
#[cargo_test]
136+
fn env_no_override() {
137+
let p = project()
138+
.file("Cargo.toml", &basic_bin_manifest("unchanged"))
139+
.file(
140+
"src/main.rs",
141+
r#"
142+
use std::env;
143+
fn main() {
144+
println!( "CARGO_PKG_NAME:{}", env!("CARGO_PKG_NAME") );
145+
}
146+
"#,
147+
)
148+
.file(
149+
".cargo/config",
150+
r#"
151+
[env]
152+
CARGO_PKG_NAME = { value = "from-config", force = true }
153+
"#,
154+
)
155+
.build();
156+
157+
p.cargo("run -Zconfigurable-env")
158+
.masquerade_as_nightly_cargo()
159+
.with_stdout_contains("CARGO_PKG_NAME:unchanged")
160+
.run();
161+
}

0 commit comments

Comments
 (0)