about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-01-31 14:56:53 +0000
committerbors <bors@rust-lang.org>2021-01-31 14:56:53 +0000
commited112741b2662b6fb8a32de0357de235e25da12d (patch)
tree59896193c23e9624773ce154e255f1c57037317e
parent95c0459217d1661edfa794c8bb122452b92fb485 (diff)
parentda26b2149f69e977afb9a758307cef1c759570af (diff)
downloadrust-ed112741b2662b6fb8a32de0357de235e25da12d.tar.gz
rust-ed112741b2662b6fb8a32de0357de235e25da12d.zip
Auto merge of #6656 - phansch:command-failed-print-stderr, r=flip1995
clippy_dev: Pass stderr to CommandFailed

This improves error reporting when running `rustfmt` fails for some reason, as seen [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Issue.20with.20rustfmt). It will now include the stderr output in the `CliError::CommandFailed` error.

changelog: none
-rw-r--r--clippy_dev/src/fmt.rs23
1 files changed, 15 insertions, 8 deletions
diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs
index 6b528d219df..4d0fdadbd85 100644
--- a/clippy_dev/src/fmt.rs
+++ b/clippy_dev/src/fmt.rs
@@ -8,7 +8,7 @@ use walkdir::WalkDir;
 
 #[derive(Debug)]
 pub enum CliError {
-    CommandFailed(String),
+    CommandFailed(String, String),
     IoError(io::Error),
     RustfmtNotInstalled,
     WalkDirError(walkdir::Error),
@@ -75,8 +75,8 @@ pub fn run(check: bool, verbose: bool) {
 
     fn output_err(err: CliError) {
         match err {
-            CliError::CommandFailed(command) => {
-                eprintln!("error: A command failed! `{}`", command);
+            CliError::CommandFailed(command, stderr) => {
+                eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
             },
             CliError::IoError(err) => {
                 eprintln!("error: {}", err);
@@ -136,12 +136,16 @@ fn exec(
         println!("{}", format_command(&program, &dir, args));
     }
 
-    let mut child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
-    let code = child.wait()?;
-    let success = code.success();
+    let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
+    let output = child.wait_with_output()?;
+    let success = output.status.success();
 
     if !context.check && !success {
-        return Err(CliError::CommandFailed(format_command(&program, &dir, args)));
+        let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
+        return Err(CliError::CommandFailed(
+            format_command(&program, &dir, args),
+            String::from(stderr),
+        ));
     }
 
     Ok(success)
@@ -177,7 +181,10 @@ fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
     {
         Err(CliError::RustfmtNotInstalled)
     } else {
-        Err(CliError::CommandFailed(format_command(&program, &dir, args)))
+        Err(CliError::CommandFailed(
+            format_command(&program, &dir, args),
+            std::str::from_utf8(&output.stderr).unwrap_or("").to_string(),
+        ))
     }
 }