mirror of
https://github.com/nushell/nushell.git
synced 2025-07-07 18:07:02 +02:00
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).
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
use miette::Diagnostic;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
use std::{io, num::NonZeroI32};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
ast::Operator, engine::StateWorkingSet, format_error, LabeledError, ParseError, Span, Spanned,
|
||||
Value,
|
||||
ast::Operator, engine::StateWorkingSet, format_shell_error, record, LabeledError, ParseError,
|
||||
Span, Spanned, Value,
|
||||
};
|
||||
|
||||
/// The fundamental error type for the evaluation engine. These cases represent different kinds of errors
|
||||
@ -639,6 +639,49 @@ pub enum ShellError {
|
||||
span: Span,
|
||||
},
|
||||
|
||||
/// An external command exited with a non-zero exit code.
|
||||
///
|
||||
/// ## Resolution
|
||||
///
|
||||
/// Check the external command's error message.
|
||||
#[error("External command had a non-zero exit code")]
|
||||
#[diagnostic(code(nu::shell::non_zero_exit_code))]
|
||||
NonZeroExitCode {
|
||||
exit_code: NonZeroI32,
|
||||
#[label("exited with code {exit_code}")]
|
||||
span: Span,
|
||||
},
|
||||
|
||||
#[cfg(unix)]
|
||||
/// An external command exited due to a signal.
|
||||
///
|
||||
/// ## Resolution
|
||||
///
|
||||
/// Check why the signal was sent or triggered.
|
||||
#[error("External command was terminated by a signal")]
|
||||
#[diagnostic(code(nu::shell::terminated_by_signal))]
|
||||
TerminatedBySignal {
|
||||
signal_name: String,
|
||||
signal: i32,
|
||||
#[label("terminated by {signal_name} ({signal})")]
|
||||
span: Span,
|
||||
},
|
||||
|
||||
#[cfg(unix)]
|
||||
/// An external command core dumped.
|
||||
///
|
||||
/// ## Resolution
|
||||
///
|
||||
/// Check why the core dumped was triggered.
|
||||
#[error("External command core dumped")]
|
||||
#[diagnostic(code(nu::shell::core_dumped))]
|
||||
CoreDumped {
|
||||
signal_name: String,
|
||||
signal: i32,
|
||||
#[label("core dumped with {signal_name} ({signal})")]
|
||||
span: Span,
|
||||
},
|
||||
|
||||
/// An unsupported body input was used for the respective application body type in 'http' command
|
||||
///
|
||||
/// ## Resolution
|
||||
@ -885,21 +928,6 @@ pub enum ShellError {
|
||||
span: Span,
|
||||
},
|
||||
|
||||
#[cfg(unix)]
|
||||
/// An I/O operation failed.
|
||||
///
|
||||
/// ## Resolution
|
||||
///
|
||||
/// This is a generic error. Refer to the specific error message for further details.
|
||||
#[error("program coredump error")]
|
||||
#[diagnostic(code(nu::shell::coredump_error))]
|
||||
CoredumpErrorSpanned {
|
||||
msg: String,
|
||||
signal: i32,
|
||||
#[label("{msg}")]
|
||||
span: Span,
|
||||
},
|
||||
|
||||
/// Tried to `cd` to a path that isn't a directory.
|
||||
///
|
||||
/// ## Resolution
|
||||
@ -1285,6 +1313,16 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
|
||||
span: Span,
|
||||
},
|
||||
|
||||
#[error("Deprecated: {old_command}")]
|
||||
#[diagnostic(help("for more info see {url}"))]
|
||||
Deprecated {
|
||||
old_command: String,
|
||||
new_suggestion: String,
|
||||
#[label("`{old_command}` is deprecated and will be removed in a future release. Please {new_suggestion} instead.")]
|
||||
span: Span,
|
||||
url: String,
|
||||
},
|
||||
|
||||
/// Invalid glob pattern
|
||||
///
|
||||
/// ## Resolution
|
||||
@ -1404,10 +1442,41 @@ On Windows, this would be %USERPROFILE%\AppData\Roaming"#
|
||||
},
|
||||
}
|
||||
|
||||
// TODO: Implement as From trait
|
||||
impl ShellError {
|
||||
pub fn external_exit_code(&self) -> Option<Spanned<i32>> {
|
||||
let (item, span) = match *self {
|
||||
Self::NonZeroExitCode { exit_code, span } => (exit_code.into(), span),
|
||||
#[cfg(unix)]
|
||||
Self::TerminatedBySignal { signal, span, .. }
|
||||
| Self::CoreDumped { signal, span, .. } => (-signal, span),
|
||||
_ => return None,
|
||||
};
|
||||
Some(Spanned { item, span })
|
||||
}
|
||||
|
||||
pub fn exit_code(&self) -> i32 {
|
||||
self.external_exit_code().map(|e| e.item).unwrap_or(1)
|
||||
}
|
||||
|
||||
pub fn into_value(self, span: Span) -> Value {
|
||||
let exit_code = self.external_exit_code();
|
||||
|
||||
let mut record = record! {
|
||||
"msg" => Value::string(self.to_string(), span),
|
||||
"debug" => Value::string(format!("{self:?}"), span),
|
||||
"raw" => Value::error(self, span),
|
||||
};
|
||||
|
||||
if let Some(code) = exit_code {
|
||||
record.push("exit_code", Value::int(code.item.into(), code.span));
|
||||
}
|
||||
|
||||
Value::record(record, span)
|
||||
}
|
||||
|
||||
// TODO: Implement as From trait
|
||||
pub fn wrap(self, working_set: &StateWorkingSet, span: Span) -> ParseError {
|
||||
let msg = format_error(working_set, &self);
|
||||
let msg = format_shell_error(working_set, &self);
|
||||
ParseError::LabeledError(
|
||||
msg,
|
||||
"Encountered error during parse-time evaluation".into(),
|
||||
|
Reference in New Issue
Block a user