Auto merge of #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.
This commit is contained in:
commit
133b3668bb
@ -3,7 +3,7 @@
|
|||||||
use shell_escape::escape;
|
use shell_escape::escape;
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::{self, Command};
|
use std::process::{self, Command, Stdio};
|
||||||
use std::{fs, io};
|
use std::{fs, io};
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
@ -31,6 +31,7 @@ fn from(error: walkdir::Error) -> Self {
|
|||||||
struct FmtContext {
|
struct FmtContext {
|
||||||
check: bool,
|
check: bool,
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
|
rustfmt_path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// the "main" function of cargo dev fmt
|
// 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 result = try_run(&context);
|
||||||
let code = match result {
|
let code = match result {
|
||||||
Ok(true) => 0,
|
Ok(true) => 0,
|
||||||
@ -141,8 +158,12 @@ fn exec(
|
|||||||
println!("{}", format_command(&program, &dir, args));
|
println!("{}", format_command(&program, &dir, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
|
let output = Command::new(&program)
|
||||||
let output = child.wait_with_output()?;
|
.env("RUSTFMT", &context.rustfmt_path)
|
||||||
|
.current_dir(&dir)
|
||||||
|
.args(args.iter())
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
let success = output.status.success();
|
let success = output.status.success();
|
||||||
|
|
||||||
if !context.check && !success {
|
if !context.check && !success {
|
||||||
@ -159,7 +180,6 @@ fn exec(
|
|||||||
fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
|
fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
|
||||||
let mut args = vec!["fmt", "--all"];
|
let mut args = vec!["fmt", "--all"];
|
||||||
if context.check {
|
if context.check {
|
||||||
args.push("--");
|
|
||||||
args.push("--check");
|
args.push("--check");
|
||||||
}
|
}
|
||||||
let success = exec(context, "cargo", path, &args)?;
|
let success = exec(context, "cargo", path, &args)?;
|
||||||
@ -200,7 +220,7 @@ fn rustfmt(context: &FmtContext, paths: impl Iterator<Item = OsString>) -> Resul
|
|||||||
}
|
}
|
||||||
args.extend(paths);
|
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)
|
Ok(success)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user