diff --git a/crates/nu-engine/src/compile/expression.rs b/crates/nu-engine/src/compile/expression.rs index 948eb5c238..80fc4fb3fb 100644 --- a/crates/nu-engine/src/compile/expression.rs +++ b/crates/nu-engine/src/compile/expression.rs @@ -444,7 +444,15 @@ pub(crate) fn compile_expression( working_set, builder, &full_cell_path.head, - RedirectModes::capture_out(expr.span), + // Only capture the output if there is a tail. This was a bit of a headscratcher + // as the parser emits a FullCellPath with no tail for subexpressions in + // general, which shouldn't be captured any differently than they otherwise + // would be. + if !full_cell_path.tail.is_empty() { + RedirectModes::capture_out(expr.span) + } else { + redirect_modes + }, in_reg, out_reg, )?; diff --git a/tests/shell/pipeline/commands/external.rs b/tests/shell/pipeline/commands/external.rs index a6efe2b6c9..ff851a3293 100644 --- a/tests/shell/pipeline/commands/external.rs +++ b/tests/shell/pipeline/commands/external.rs @@ -209,6 +209,12 @@ fn run_glob_if_pass_variable_to_external() { }) } +#[test] +fn subexpression_does_not_implicitly_capture() { + let actual = nu!("(nu --testbin cococo); null"); + assert_eq!(actual.out, "cococo"); +} + mod it_evaluation { use super::nu; use nu_test_support::fs::Stub::{EmptyFile, FileWithContent, FileWithContentToBeTrimmed};