From e911ff4d67e5342f325d825745f6f5c01504d6e6 Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Thu, 17 Oct 2024 20:05:58 -0700 Subject: [PATCH] Fix return setting last exit code (#14120) # Description Fixes #14113 and #14112. # Tests + Formatting Added a test. --- crates/nu-cli/src/util.rs | 2 +- crates/nu-command/tests/commands/return_.rs | 6 ++++++ crates/nu-protocol/src/engine/stack.rs | 4 ++-- crates/nu-protocol/src/errors/shell_error.rs | 7 +++++-- src/run.rs | 4 ++-- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/nu-cli/src/util.rs b/crates/nu-cli/src/util.rs index 038d0d0312..9848bb6051 100644 --- a/crates/nu-cli/src/util.rs +++ b/crates/nu-cli/src/util.rs @@ -221,7 +221,7 @@ pub fn eval_source( report_shell_error(engine_state, &err); let code = err.exit_code(); stack.set_last_error(&err); - code + code.unwrap_or(0) } }; diff --git a/crates/nu-command/tests/commands/return_.rs b/crates/nu-command/tests/commands/return_.rs index 708103d184..7e9b9ab739 100644 --- a/crates/nu-command/tests/commands/return_.rs +++ b/crates/nu-command/tests/commands/return_.rs @@ -32,3 +32,9 @@ fn return_works_in_script_with_def_main() { ); assert!(actual.err.is_empty()); } + +#[test] +fn return_does_not_set_last_exit_code() { + let actual = nu!("hide-env LAST_EXIT_CODE; do --env { return 42 }; $env.LAST_EXIT_CODE?"); + assert!(matches!(actual.out.as_str(), "")); +} diff --git a/crates/nu-protocol/src/engine/stack.rs b/crates/nu-protocol/src/engine/stack.rs index f84fae5148..3102e3a84e 100644 --- a/crates/nu-protocol/src/engine/stack.rs +++ b/crates/nu-protocol/src/engine/stack.rs @@ -287,8 +287,8 @@ impl Stack { pub fn set_last_error(&mut self, error: &ShellError) { if let Some(code) = error.external_exit_code() { self.set_last_exit_code(code.item, code.span); - } else { - self.set_last_exit_code(1, Span::unknown()); + } else if let Some(code) = error.exit_code() { + self.set_last_exit_code(code, Span::unknown()); } } diff --git a/crates/nu-protocol/src/errors/shell_error.rs b/crates/nu-protocol/src/errors/shell_error.rs index 0f609061d6..0061d63602 100644 --- a/crates/nu-protocol/src/errors/shell_error.rs +++ b/crates/nu-protocol/src/errors/shell_error.rs @@ -1470,8 +1470,11 @@ impl ShellError { Some(Spanned { item, span }) } - pub fn exit_code(&self) -> i32 { - self.external_exit_code().map(|e| e.item).unwrap_or(1) + pub fn exit_code(&self) -> Option { + match self { + Self::Return { .. } | Self::Break { .. } | Self::Continue { .. } => None, + _ => self.external_exit_code().map(|e| e.item).or(Some(1)), + } } pub fn into_value(self, span: Span) -> Value { diff --git a/src/run.rs b/src/run.rs index 84b5f7df4a..14c737c613 100644 --- a/src/run.rs +++ b/src/run.rs @@ -104,7 +104,7 @@ pub(crate) fn run_commands( if let Err(err) = result { report_shell_error(engine_state, &err); - std::process::exit(err.exit_code()); + std::process::exit(err.exit_code().unwrap_or(0)); } } @@ -178,7 +178,7 @@ pub(crate) fn run_file( if let Err(err) = result { report_shell_error(engine_state, &err); - std::process::exit(err.exit_code()); + std::process::exit(err.exit_code().unwrap_or(0)); } }