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.
This commit is contained in:
Sophia June Turner 2023-12-05 19:42:55 +13:00 committed by GitHub
parent f8dc3421b0
commit 2ffe30ecf0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<PipelineData, ShellError> {
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(