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
This commit is contained in:
Access 2022-10-23 02:24:58 +08:00 committed by GitHub
parent 8224ec49bc
commit c9fb381d69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View file

@ -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,

View file

@ -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);