Skip to content

Commit 133b366

Browse files
committed
Auto merge of rust-lang#8265 - camsteffen:which-rustfmt, r=xFrednet
Cache rustfmt path changelog: none Call `rustup which rustfmt` and use the output. This shaves off ~0.7 seconds for `cargo dev fmt` for me.
2 parents 6f33f69 + d356fb9 commit 133b366

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

clippy_dev/src/fmt.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use itertools::Itertools;
33
use shell_escape::escape;
44
use std::ffi::{OsStr, OsString};
55
use std::path::Path;
6-
use std::process::{self, Command};
6+
use std::process::{self, Command, Stdio};
77
use std::{fs, io};
88
use walkdir::WalkDir;
99

@@ -31,6 +31,7 @@ impl From<walkdir::Error> for CliError {
3131
struct FmtContext {
3232
check: bool,
3333
verbose: bool,
34+
rustfmt_path: String,
3435
}
3536

3637
// the "main" function of cargo dev fmt
@@ -102,7 +103,23 @@ Please revert the changes to Cargo.tomls first."
102103
}
103104
}
104105

105-
let context = FmtContext { check, verbose };
106+
let output = Command::new("rustup")
107+
.args(["which", "rustfmt"])
108+
.stderr(Stdio::inherit())
109+
.output()
110+
.expect("error running `rustup which rustfmt`");
111+
if !output.status.success() {
112+
eprintln!("`rustup which rustfmt` did not execute successfully");
113+
process::exit(1);
114+
}
115+
let mut rustfmt_path = String::from_utf8(output.stdout).expect("invalid rustfmt path");
116+
rustfmt_path.truncate(rustfmt_path.trim_end().len());
117+
118+
let context = FmtContext {
119+
check,
120+
verbose,
121+
rustfmt_path,
122+
};
106123
let result = try_run(&context);
107124
let code = match result {
108125
Ok(true) => 0,
@@ -141,8 +158,12 @@ fn exec(
141158
println!("{}", format_command(&program, &dir, args));
142159
}
143160

144-
let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
145-
let output = child.wait_with_output()?;
161+
let output = Command::new(&program)
162+
.env("RUSTFMT", &context.rustfmt_path)
163+
.current_dir(&dir)
164+
.args(args.iter())
165+
.output()
166+
.unwrap();
146167
let success = output.status.success();
147168

148169
if !context.check && !success {
@@ -159,7 +180,6 @@ fn exec(
159180
fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
160181
let mut args = vec!["fmt", "--all"];
161182
if context.check {
162-
args.push("--");
163183
args.push("--check");
164184
}
165185
let success = exec(context, "cargo", path, &args)?;
@@ -200,7 +220,7 @@ fn rustfmt(context: &FmtContext, paths: impl Iterator<Item = OsString>) -> Resul
200220
}
201221
args.extend(paths);
202222

203-
let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
223+
let success = exec(context, &context.rustfmt_path, std::env::current_dir()?, &args)?;
204224

205225
Ok(success)
206226
}

0 commit comments

Comments
 (0)