nushell/crates/nu-engine/src/eval_helpers.rs
Ian Manske 3d008e2c4e
Error on non-zero exit statuses (#13515)
# Description
This PR makes it so that non-zero exit codes and termination by signal
are treated as a normal `ShellError`. Currently, these are silent
errors. That is, if an external command fails, then it's code block is
aborted, but the parent block can sometimes continue execution. E.g.,
see #8569 and this example:
```nushell
[1 2] | each { ^false }
```

Before this would give:
```
╭───┬──╮
│ 0 │  │
│ 1 │  │
╰───┴──╯
```

Now, this shows an error:
```
Error: nu:🐚:eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[entry #1:1:2]
 1 │ [1 2] | each { ^false }
   ·  ┬
   ·  ╰── source value
   ╰────

Error: nu:🐚:non_zero_exit_code

  × External command had a non-zero exit code
   ╭─[entry #1:1:17]
 1 │ [1 2] | each { ^false }
   ·                 ──┬──
   ·                   ╰── exited with code 1
   ╰────
```

This PR fixes #12874, fixes #5960, fixes #10856, and fixes #5347. This
PR also partially addresses #10633 and #10624 (only the last command of
a pipeline is currently checked). It looks like #8569 is already fixed,
but this PR will make sure it is definitely fixed (fixes #8569).

# User-Facing Changes
- Non-zero exit codes and termination by signal now cause an error to be
thrown.
- The error record value passed to a `catch` block may now have an
`exit_code` column containing the integer exit code if the error was due
to an external command.
- Adds new config values, `display_errors.exit_code` and
`display_errors.termination_signal`, which determine whether an error
message should be printed in the respective error cases. For
non-interactive sessions, these are set to `true`, and for interactive
sessions `display_errors.exit_code` is false (via the default config).

# Tests
Added a few tests.

# After Submitting
- Update docs and book.
- Future work:
- Error if other external commands besides the last in a pipeline exit
with a non-zero exit code. Then, deprecate `do -c` since this will be
the default behavior everywhere.
- Add a better mechanism for exit codes and deprecate
`$env.LAST_EXIT_CODE` (it's buggy).
2024-09-07 06:44:26 +00:00

94 lines
3.5 KiB
Rust

use crate::{
eval_block, eval_block_with_early_return, eval_expression, eval_expression_with_input,
eval_ir_block, eval_subexpression,
};
use nu_protocol::{
ast::{Block, Expression},
debugger::{WithDebug, WithoutDebug},
engine::{EngineState, Stack},
PipelineData, ShellError, Value,
};
/// Type of eval_block() function
pub type EvalBlockFn =
fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
/// Type of eval_ir_block() function
pub type EvalIrBlockFn =
fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
/// Type of eval_block_with_early_return() function
pub type EvalBlockWithEarlyReturnFn =
fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
/// Type of eval_expression() function
pub type EvalExpressionFn = fn(&EngineState, &mut Stack, &Expression) -> Result<Value, ShellError>;
/// Type of eval_expression_with_input() function
pub type EvalExpressionWithInputFn =
fn(&EngineState, &mut Stack, &Expression, PipelineData) -> Result<PipelineData, ShellError>;
/// Type of eval_subexpression() function
pub type EvalSubexpressionFn =
fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
/// Helper function to fetch `eval_block()` with the correct type parameter based on whether
/// engine_state is configured with or without a debugger.
pub fn get_eval_block(engine_state: &EngineState) -> EvalBlockFn {
if engine_state.is_debugging() {
eval_block::<WithDebug>
} else {
eval_block::<WithoutDebug>
}
}
/// Helper function to fetch `eval_ir_block()` with the correct type parameter based on whether
/// engine_state is configured with or without a debugger.
pub fn get_eval_ir_block(engine_state: &EngineState) -> EvalIrBlockFn {
if engine_state.is_debugging() {
eval_ir_block::<WithDebug>
} else {
eval_ir_block::<WithoutDebug>
}
}
/// Helper function to fetch `eval_block_with_early_return()` with the correct type parameter based
/// on whether engine_state is configured with or without a debugger.
pub fn get_eval_block_with_early_return(engine_state: &EngineState) -> EvalBlockWithEarlyReturnFn {
if engine_state.is_debugging() {
eval_block_with_early_return::<WithDebug>
} else {
eval_block_with_early_return::<WithoutDebug>
}
}
/// Helper function to fetch `eval_expression()` with the correct type parameter based on whether
/// engine_state is configured with or without a debugger.
pub fn get_eval_expression(engine_state: &EngineState) -> EvalExpressionFn {
if engine_state.is_debugging() {
eval_expression::<WithDebug>
} else {
eval_expression::<WithoutDebug>
}
}
/// Helper function to fetch `eval_expression_with_input()` with the correct type parameter based
/// on whether engine_state is configured with or without a debugger.
pub fn get_eval_expression_with_input(engine_state: &EngineState) -> EvalExpressionWithInputFn {
if engine_state.is_debugging() {
eval_expression_with_input::<WithDebug>
} else {
eval_expression_with_input::<WithoutDebug>
}
}
/// Helper function to fetch `eval_subexpression()` with the correct type parameter based on whether
/// engine_state is configured with or without a debugger.
pub fn get_eval_subexpression(engine_state: &EngineState) -> EvalSubexpressionFn {
if engine_state.is_debugging() {
eval_subexpression::<WithDebug>
} else {
eval_subexpression::<WithoutDebug>
}
}