From c9fb381d69b0b23a7095c4d97ddf6ee476b65bb1 Mon Sep 17 00:00:00 2001 From: Access Date: Sun, 23 Oct 2022 02:24:58 +0800 Subject: [PATCH] feat: coredump (#6791) fix issue in https://github.com/nushell/nushell/issues/5903 return Error to pipeline_data, if match error, then return error directly --- crates/nu-command/src/system/run_external.rs | 24 ++++++++++++++++++++ crates/nu-protocol/src/pipeline_data.rs | 9 +++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index 114803bf8..f704e6e7d 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -342,6 +342,8 @@ impl ExternalCommand { } } + #[cfg(unix)] + let commandname = self.name.item.clone(); let redirect_stdout = self.redirect_stdout; let redirect_stderr = self.redirect_stderr; let span = self.name.span; @@ -378,6 +380,28 @@ impl ExternalCommand { span, )), Ok(x) => { + #[cfg(unix)] + { + use nu_ansi_term::{Color, Style}; + use std::os::unix::process::ExitStatusExt; + if x.core_dumped() { + let style = Style::new().bold().on(Color::Red); + println!( + "{}", + style.paint(format!( + "nushell: oops, process '{commandname}' core dumped" + )) + ); + let _ = exit_code_tx.send(Value::Error { + error: ShellError::ExternalCommand( + "core dumped".to_string(), + format!("Child process '{commandname}' core dumped"), + head, + ), + }); + return Ok(()); + } + } if let Some(code) = x.code() { let _ = exit_code_tx.send(Value::Int { val: code as i64, diff --git a/crates/nu-protocol/src/pipeline_data.rs b/crates/nu-protocol/src/pipeline_data.rs index 32665fe13..7afa5e5a6 100644 --- a/crates/nu-protocol/src/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline_data.rs @@ -445,9 +445,12 @@ impl PipelineData { // Make sure everything has finished if let Some(exit_code) = exit_code { let mut exit_codes: Vec<_> = exit_code.into_iter().collect(); - if let Some(Value::Int { val, .. }) = exit_codes.pop() { - return Ok(val); - } + return match exit_codes.pop() { + #[cfg(unix)] + Some(Value::Error { error }) => Err(error), + Some(Value::Int { val, .. }) => Ok(val), + _ => Ok(0), + }; } return Ok(0);