diff --git a/crates/nu-engine/src/compile/mod.rs b/crates/nu-engine/src/compile/mod.rs index 94a767e80c..78066232f9 100644 --- a/crates/nu-engine/src/compile/mod.rs +++ b/crates/nu-engine/src/compile/mod.rs @@ -1,7 +1,7 @@ use nu_protocol::{ ast::{Block, Pipeline, PipelineRedirection, RedirectionSource}, engine::StateWorkingSet, - ir::{Instruction, IrBlock}, + ir::{Instruction, IrBlock, RedirectMode}, CompileError, IntoSpanned, RegId, Span, }; @@ -113,20 +113,24 @@ fn compile_pipeline( // element, then it's from whatever is passed in as the mode to use. let next_redirect_modes = if let Some(next_element) = iter.peek() { - // If there's a next element we always pipe out *unless* this is a single redirection - // to stderr (e>|) - let modes = redirect_modes_of_expression(working_set, &next_element.expr, span)?; - if matches!( - element.redirection, - Some(PipelineRedirection::Single { - source: RedirectionSource::Stderr, - .. - }) - ) { - modes - } else { - modes.with_pipe_out(next_element.pipe.unwrap_or(next_element.expr.span)) + let mut modes = redirect_modes_of_expression(working_set, &next_element.expr, span)?; + + // If there's a next element with no inherent redirection we always pipe out *unless* + // this is a single redirection to stderr (e>|) + if modes.out.is_none() + && !matches!( + element.redirection, + Some(PipelineRedirection::Single { + source: RedirectionSource::Stderr, + .. + }) + ) + { + let pipe_span = next_element.pipe.unwrap_or(next_element.expr.span); + modes.out = Some(RedirectMode::Pipe.into_spanned(pipe_span)); } + + modes } else { redirect_modes .take() diff --git a/crates/nu-engine/src/compile/redirect.rs b/crates/nu-engine/src/compile/redirect.rs index 5d1e7a6d20..1b401a71b6 100644 --- a/crates/nu-engine/src/compile/redirect.rs +++ b/crates/nu-engine/src/compile/redirect.rs @@ -28,13 +28,6 @@ impl RedirectModes { } } - pub(crate) fn with_pipe_out(&self, span: Span) -> Self { - RedirectModes { - out: Some(RedirectMode::Pipe.into_spanned(span)), - err: self.err.clone(), - } - } - pub(crate) fn with_capture_out(&self, span: Span) -> Self { RedirectModes { out: Some(RedirectMode::Capture.into_spanned(span)), @@ -134,7 +127,7 @@ pub(crate) fn finish_redirection( builder.push(Instruction::CloseFile { file_num }.into_spanned(span))?; } Some(Spanned { - item: RedirectMode::Pipe | RedirectMode::Capture, + item: RedirectMode::Pipe, span, }) => { builder.push(Instruction::CheckErrRedirected { src: out_reg }.into_spanned(span))?;