From 41946caa7ff62708fa5fe183725b0784a0b7a26d Mon Sep 17 00:00:00 2001 From: PiergiorgioZagaria Date: Sun, 7 Aug 2022 08:52:43 +0200 Subject: [PATCH 1/2] Fix process spawning error handling --- helix-term/src/commands.rs | 12 ++++++++---- helix-view/src/document.rs | 23 ++++++++++++----------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 32f2800408af..c48e069dadbb 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -875,8 +875,8 @@ fn goto_window(cx: &mut Context, align: Align) { let last_line = view.last_line(doc); let line = match align { - Align::Top => (view.offset.row + scrolloff + count), - Align::Center => (view.offset.row + ((last_line - view.offset.row) / 2)), + Align::Top => view.offset.row + scrolloff + count, + Align::Center => view.offset.row + ((last_line - view.offset.row) / 2), Align::Bottom => last_line.saturating_sub(scrolloff + count), } .max(view.offset.row + scrolloff) @@ -4578,8 +4578,12 @@ fn shell_impl( } let output = process.wait_with_output()?; - if !output.stderr.is_empty() { - log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr)); + if !output.status.success() { + if !output.stderr.is_empty() { + let err = String::from_utf8_lossy(&output.stderr); + bail!("Shell error: {}", err.to_string()); + } + bail!("Shell command failed"); } let str = std::str::from_utf8(&output.stdout) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index c96f222de856..1f381d5437d8 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -440,14 +440,14 @@ impl Document { .await .map_err(|_| FormatterError::WaitForOutputFailed)?; - if !output.stderr.is_empty() { - return Err(FormatterError::Stderr( - String::from_utf8_lossy(&output.stderr).to_string(), - )); - } - if !output.status.success() { - return Err(FormatterError::NonZeroExitStatus); + if !output.stderr.is_empty() { + return Err(FormatterError::NonZeroExitStatus(Some( + String::from_utf8_lossy(&output.stderr).to_string(), + ))); + } + + return Err(FormatterError::NonZeroExitStatus(None)); } let str = String::from_utf8(output.stdout) @@ -1092,10 +1092,9 @@ pub enum FormatterError { }, BrokenStdin, WaitForOutputFailed, - Stderr(String), InvalidUtf8Output, DiskReloadError(String), - NonZeroExitStatus, + NonZeroExitStatus(Option), } impl std::error::Error for FormatterError {} @@ -1108,10 +1107,12 @@ impl Display for FormatterError { } Self::BrokenStdin => write!(f, "Could not write to formatter stdin"), Self::WaitForOutputFailed => write!(f, "Waiting for formatter output failed"), - Self::Stderr(output) => write!(f, "Formatter error: {}", output), Self::InvalidUtf8Output => write!(f, "Invalid UTF-8 formatter output"), Self::DiskReloadError(error) => write!(f, "Error reloading file from disk: {}", error), - Self::NonZeroExitStatus => write!(f, "Formatter exited with non zero exit status:"), + Self::NonZeroExitStatus(Some(output)) => write!(f, "Formatter error: {}", output), + Self::NonZeroExitStatus(None) => { + write!(f, "Formatter exited with non zero exit status:") + } } } } From 3725a6823dd6df6f0a66999ed13e916f28209300 Mon Sep 17 00:00:00 2001 From: PiergiorgioZagaria Date: Tue, 9 Aug 2022 09:01:46 +0200 Subject: [PATCH 2/2] Log stderr in any case --- helix-term/src/commands.rs | 10 ++++++++-- helix-view/src/document.rs | 15 ++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c48e069dadbb..4f2f2eeea27a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4580,10 +4580,16 @@ fn shell_impl( if !output.status.success() { if !output.stderr.is_empty() { - let err = String::from_utf8_lossy(&output.stderr); - bail!("Shell error: {}", err.to_string()); + let err = String::from_utf8_lossy(&output.stderr).to_string(); + log::error!("Shell error: {}", err); + bail!("Shell error: {}", err); } bail!("Shell command failed"); + } else if !output.stderr.is_empty() { + log::debug!( + "Command printed to stderr: {}", + String::from_utf8_lossy(&output.stderr).to_string() + ); } let str = std::str::from_utf8(&output.stdout) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 1f381d5437d8..204be54106aa 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -442,15 +442,20 @@ impl Document { if !output.status.success() { if !output.stderr.is_empty() { - return Err(FormatterError::NonZeroExitStatus(Some( - String::from_utf8_lossy(&output.stderr).to_string(), - ))); + let err = String::from_utf8_lossy(&output.stderr).to_string(); + log::error!("Formatter error: {}", err); + return Err(FormatterError::NonZeroExitStatus(Some(err))); } return Err(FormatterError::NonZeroExitStatus(None)); + } else if !output.stderr.is_empty() { + log::debug!( + "Formatter printed to stderr: {}", + String::from_utf8_lossy(&output.stderr).to_string() + ); } - let str = String::from_utf8(output.stdout) + let str = std::str::from_utf8(&output.stdout) .map_err(|_| FormatterError::InvalidUtf8Output)?; Ok(helix_core::diff::compare_ropes(&text, &Rope::from(str))) @@ -1111,7 +1116,7 @@ impl Display for FormatterError { Self::DiskReloadError(error) => write!(f, "Error reloading file from disk: {}", error), Self::NonZeroExitStatus(Some(output)) => write!(f, "Formatter error: {}", output), Self::NonZeroExitStatus(None) => { - write!(f, "Formatter exited with non zero exit status:") + write!(f, "Formatter exited with non zero exit status") } } }