From 7cf4f44e561acd2c69b9a4fd1b5e97ff59a6bbbe Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Mon, 10 Jan 2022 14:30:45 -0600 Subject: [PATCH 1/2] Fix output capturing --- clippy_dev/src/fmt.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index 9ceadee58ea..625fef23afb 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -141,8 +141,11 @@ fn exec( println!("{}", format_command(&program, &dir, args)); } - let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?; - let output = child.wait_with_output()?; + let output = Command::new(&program) + .current_dir(&dir) + .args(args.iter()) + .output() + .unwrap(); let success = output.status.success(); if !context.check && !success { From d356fb9fa20198d770fc7e05bb71aea00875b3ea Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Mon, 10 Jan 2022 13:53:02 -0600 Subject: [PATCH 2/2] Use `rustup which rustfmt` --- clippy_dev/src/fmt.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index 625fef23afb..d513a229b7e 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -3,7 +3,7 @@ use shell_escape::escape; use std::ffi::{OsStr, OsString}; use std::path::Path; -use std::process::{self, Command}; +use std::process::{self, Command, Stdio}; use std::{fs, io}; use walkdir::WalkDir; @@ -31,6 +31,7 @@ fn from(error: walkdir::Error) -> Self { struct FmtContext { check: bool, verbose: bool, + rustfmt_path: String, } // the "main" function of cargo dev fmt @@ -102,7 +103,23 @@ fn output_err(err: CliError) { } } - let context = FmtContext { check, verbose }; + let output = Command::new("rustup") + .args(["which", "rustfmt"]) + .stderr(Stdio::inherit()) + .output() + .expect("error running `rustup which rustfmt`"); + if !output.status.success() { + eprintln!("`rustup which rustfmt` did not execute successfully"); + process::exit(1); + } + let mut rustfmt_path = String::from_utf8(output.stdout).expect("invalid rustfmt path"); + rustfmt_path.truncate(rustfmt_path.trim_end().len()); + + let context = FmtContext { + check, + verbose, + rustfmt_path, + }; let result = try_run(&context); let code = match result { Ok(true) => 0, @@ -142,6 +159,7 @@ fn exec( } let output = Command::new(&program) + .env("RUSTFMT", &context.rustfmt_path) .current_dir(&dir) .args(args.iter()) .output() @@ -162,7 +180,6 @@ fn exec( fn cargo_fmt(context: &FmtContext, path: &Path) -> Result { let mut args = vec!["fmt", "--all"]; if context.check { - args.push("--"); args.push("--check"); } let success = exec(context, "cargo", path, &args)?; @@ -203,7 +220,7 @@ fn rustfmt(context: &FmtContext, paths: impl Iterator) -> Resul } args.extend(paths); - let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?; + let success = exec(context, &context.rustfmt_path, std::env::current_dir()?, &args)?; Ok(success) }