File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments