Skip to content

Commit d356fb9

Browse files
committed
Use rustup which rustfmt
1 parent 7cf4f44 commit d356fb9

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

clippy_dev/src/fmt.rs

Lines changed: 21 additions & 4 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,
@@ -142,6 +159,7 @@ fn exec(
142159
}
143160

144161
let output = Command::new(&program)
162+
.env("RUSTFMT", &context.rustfmt_path)
145163
.current_dir(&dir)
146164
.args(args.iter())
147165
.output()
@@ -162,7 +180,6 @@ fn exec(
162180
fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
163181
let mut args = vec!["fmt", "--all"];
164182
if context.check {
165-
args.push("--");
166183
args.push("--check");
167184
}
168185
let success = exec(context, "cargo", path, &args)?;
@@ -203,7 +220,7 @@ fn rustfmt(context: &FmtContext, paths: impl Iterator<Item = OsString>) -> Resul
203220
}
204221
args.extend(paths);
205222

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

208225
Ok(success)
209226
}

0 commit comments

Comments
 (0)