From a86e6ce89b889c8fb010af49cd88cdc647c823cc Mon Sep 17 00:00:00 2001 From: Reilly Wood <26268125+rgwood@users.noreply.github.com> Date: Mon, 4 Apr 2022 04:11:27 -0700 Subject: [PATCH] Set LAST_EXIT_CODE on parse error (#5084) --- crates/nu-cli/src/util.rs | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/crates/nu-cli/src/util.rs b/crates/nu-cli/src/util.rs index da1baf2082..bc227643b5 100644 --- a/crates/nu-cli/src/util.rs +++ b/crates/nu-cli/src/util.rs @@ -259,6 +259,7 @@ pub fn eval_source( &[], ); if let Some(err) = err { + set_last_exit_code(stack, 1); report_error(&working_set, &err); return false; } @@ -286,22 +287,10 @@ pub fn eval_source( if let Some(exit_code) = exit_code.take().and_then(|it| it.last()) { stack.add_env_var("LAST_EXIT_CODE".to_string(), exit_code); } else { - stack.add_env_var( - "LAST_EXIT_CODE".to_string(), - Value::Int { - val: 0, - span: Span { start: 0, end: 0 }, - }, - ); + set_last_exit_code(stack, 0); } } else { - stack.add_env_var( - "LAST_EXIT_CODE".to_string(), - Value::Int { - val: 0, - span: Span { start: 0, end: 0 }, - }, - ); + set_last_exit_code(stack, 0); } if let Err(err) = print_pipeline_data(pipeline_data, engine_state, stack) { @@ -319,13 +308,7 @@ pub fn eval_source( } } Err(err) => { - stack.add_env_var( - "LAST_EXIT_CODE".to_string(), - Value::Int { - val: 1, - span: Span { start: 0, end: 0 }, - }, - ); + set_last_exit_code(stack, 1); let working_set = StateWorkingSet::new(engine_state); @@ -338,6 +321,16 @@ pub fn eval_source( true } +fn set_last_exit_code(stack: &mut Stack, exit_code: i64) { + stack.add_env_var( + "LAST_EXIT_CODE".to_string(), + Value::Int { + val: exit_code, + span: Span { start: 0, end: 0 }, + }, + ); +} + fn seems_like_number(bytes: &[u8]) -> bool { if bytes.is_empty() { false