diff --git a/crates/nu-cli/src/evaluate/scope.rs b/crates/nu-cli/src/evaluate/scope.rs index 1d8a91788e..28d73affc7 100644 --- a/crates/nu-cli/src/evaluate/scope.rs +++ b/crates/nu-cli/src/evaluate/scope.rs @@ -89,7 +89,9 @@ impl Scope { for frame in self.frames.lock().iter().rev() { for v in frame.vars.iter() { - output.insert(v.0.clone(), v.1.clone()); + if !output.contains_key(v.0) { + output.insert(v.0.clone(), v.1.clone()); + } } } @@ -102,7 +104,9 @@ impl Scope { for frame in self.frames.lock().iter().rev() { for v in frame.env.iter() { - output.insert(v.0.clone(), v.1.clone()); + if !output.contains_key(v.0) { + output.insert(v.0.clone(), v.1.clone()); + } } } diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index c65a0f8909..bfc4d366bc 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -455,6 +455,28 @@ fn set_env_doesnt_leak() { assert!(actual.err.contains("did you mean")); } +#[test] +fn proper_shadow_set_env_aliases() { + let actual = nu!( + cwd: ".", + r#" + set-env DEBUG = true; echo $nu.env.DEBUG | autoview; do { set-env DEBUG = false; echo $nu.env.DEBUG } | autoview; echo $nu.env.DEBUG + "# + ); + assert_eq!(actual.out, "truefalsetrue"); +} + +#[test] +fn proper_shadow_set_aliases() { + let actual = nu!( + cwd: ".", + r#" + set DEBUG = false; echo $DEBUG | autoview; do { set DEBUG = true; echo $DEBUG } | autoview; echo $DEBUG + "# + ); + assert_eq!(actual.out, "falsetruefalse"); +} + #[cfg(feature = "which")] #[test] fn argument_invocation_reports_errors() {