From ac05a8526ff409c594548068eea532c0e5ddffac Mon Sep 17 00:00:00 2001 From: Martin Donlon Date: Sat, 12 Jun 2021 08:48:23 -0700 Subject: [PATCH] Don't allow config env to modify vars set by cargo --- src/cargo/core/compiler/compilation.rs | 7 ++++++- tests/testsuite/cargo_env_config.rs | 28 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index e0b9282af63..3a6733a85ff 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -348,7 +348,12 @@ impl<'cfg> Compilation<'cfg> { if self.config.cli_unstable().configurable_env { // Apply any environment variables from the config for (key, value) in self.config.env_config()?.iter() { - if value.is_force() || cmd.get_env(key).is_none() { + // never override a value that has already been set by cargo + if cmd.get_envs().contains_key(key) { + continue; + } + + if value.is_force() || env::var_os(key).is_none() { cmd.env(key, value.resolve(self.config)); } } diff --git a/tests/testsuite/cargo_env_config.rs b/tests/testsuite/cargo_env_config.rs index 83ebdbb18f6..c50d952d1c0 100644 --- a/tests/testsuite/cargo_env_config.rs +++ b/tests/testsuite/cargo_env_config.rs @@ -131,3 +131,31 @@ fn env_relative() { .masquerade_as_nightly_cargo() .run(); } + +#[cargo_test] +fn env_no_override() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("unchanged")) + .file( + "src/main.rs", + r#" + use std::env; + fn main() { + println!( "CARGO_PKG_NAME:{}", env!("CARGO_PKG_NAME") ); + } + "#, + ) + .file( + ".cargo/config", + r#" + [env] + CARGO_PKG_NAME = { value = "from-config", force = true } + "#, + ) + .build(); + + p.cargo("run -Zconfigurable-env") + .masquerade_as_nightly_cargo() + .with_stdout_contains("CARGO_PKG_NAME:unchanged") + .run(); +}