Skip to content

Commit ed11274

Browse files
committed
Auto merge of rust-lang#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
2 parents 95c0459 + da26b21 commit ed11274

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

clippy_dev/src/fmt.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use walkdir::WalkDir;
88

99
#[derive(Debug)]
1010
pub enum CliError {
11-
CommandFailed(String),
11+
CommandFailed(String, String),
1212
IoError(io::Error),
1313
RustfmtNotInstalled,
1414
WalkDirError(walkdir::Error),
@@ -75,8 +75,8 @@ pub fn run(check: bool, verbose: bool) {
7575

7676
fn output_err(err: CliError) {
7777
match err {
78-
CliError::CommandFailed(command) => {
79-
eprintln!("error: A command failed! `{}`", command);
78+
CliError::CommandFailed(command, stderr) => {
79+
eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
8080
},
8181
CliError::IoError(err) => {
8282
eprintln!("error: {}", err);
@@ -136,12 +136,16 @@ fn exec(
136136
println!("{}", format_command(&program, &dir, args));
137137
}
138138

139-
let mut child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
140-
let code = child.wait()?;
141-
let success = code.success();
139+
let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
140+
let output = child.wait_with_output()?;
141+
let success = output.status.success();
142142

143143
if !context.check && !success {
144-
return Err(CliError::CommandFailed(format_command(&program, &dir, args)));
144+
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
145+
return Err(CliError::CommandFailed(
146+
format_command(&program, &dir, args),
147+
String::from(stderr),
148+
));
145149
}
146150

147151
Ok(success)
@@ -177,7 +181,10 @@ fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
177181
{
178182
Err(CliError::RustfmtNotInstalled)
179183
} else {
180-
Err(CliError::CommandFailed(format_command(&program, &dir, args)))
184+
Err(CliError::CommandFailed(
185+
format_command(&program, &dir, args),
186+
std::str::from_utf8(&output.stderr).unwrap_or("").to_string(),
187+
))
181188
}
182189
}
183190

0 commit comments

Comments
 (0)