From 2ffe30ecf0bf12ddc7cf89e30445a5a9ac67df89 Mon Sep 17 00:00:00 2001 From: Sophia June Turner <547158+sophiajt@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:42:55 +1300 Subject: [PATCH] Respect non-zero exit code in subexpressions and blocks (#8984) # Description This PR changes the way we handled non-zero exit codes to be and early exit between `foo; bar`. If `foo` in the example has a non-zero exit code, `bar` wouldn't be run. This also affects subexpressions. --- crates/nu-engine/src/eval.rs | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 307a08eb73..075b7388e8 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -723,7 +723,13 @@ pub fn eval_block( let mut v: Vec<_> = exit_code.collect(); if let Some(v) = v.pop() { + let break_loop = !matches!(v.as_i64(), Ok(0)); + stack.add_env_var("LAST_EXIT_CODE".into(), v); + if break_loop { + input = PipelineData::empty(); + break; + } } } } @@ -741,31 +747,9 @@ pub fn eval_subexpression( engine_state: &EngineState, stack: &mut Stack, block: &Block, - mut input: PipelineData, + input: PipelineData, ) -> Result { - for pipeline in block.pipelines.iter() { - let mut stderr_writer_jobs = vec![]; - for expr in pipeline.elements.iter() { - input = eval_element_with_input( - engine_state, - stack, - expr, - input, - true, - false, - &mut stderr_writer_jobs, - )? - .0 - } - // `eval_element_with_input` may creates some threads - // to write stderr message to a file, here we need to wait and make sure that it's - // finished. - for h in stderr_writer_jobs { - let _ = h.join(); - } - } - - Ok(input) + eval_block(engine_state, stack, block, input, true, false) } pub fn eval_variable(