Skip to content

Commit 7cd36b2

Browse files
committed
try go fix cargo fmt invocation
1 parent 737f0a6 commit 7cd36b2

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

clippy_dev/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ regex = "1"
1111
lazy_static = "1.0"
1212
shell-escape = "0.1"
1313
walkdir = "2"
14+
home = "0.5.0"

clippy_dev/src/fmt.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use home;
12
use shell_escape::escape;
23
use std::ffi::OsStr;
34
use std::io;
@@ -31,6 +32,26 @@ struct FmtContext {
3132
verbose: bool,
3233
}
3334

35+
fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
36+
let mut args = vec!["+nightly", "fmt", "--all"];
37+
if context.check {
38+
args.push("--");
39+
args.push("--check");
40+
}
41+
// in case something aliases cargo=cargo --foo, the order of args will be messed up
42+
// because we will end up with cargo --foo +nightly fmt but the "+nightly" needs to come first
43+
// get the absolute path to the cargo binary inside $CARGO_HOME
44+
let cargo_home = home::cargo_home().expect("failed to get cargo home path!");
45+
let cargo_path = cargo_home.join("bin").join("cargo");
46+
assert!(
47+
cargo_path.is_file(),
48+
format!("ERROR: '{}' is not a path to the cargo binary!", cargo_path.display())
49+
);
50+
let success = exec(context, cargo_path, path, &args)?;
51+
52+
Ok(success)
53+
}
54+
3455
pub fn run(check: bool, verbose: bool) {
3556
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
3657
let mut success = true;
@@ -134,17 +155,6 @@ fn exec(
134155
Ok(success)
135156
}
136157

137-
fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
138-
let mut args = vec!["+nightly", "fmt", "--all"];
139-
if context.check {
140-
args.push("--");
141-
args.push("--check");
142-
}
143-
let success = exec(context, "cargo", path, &args)?;
144-
145-
Ok(success)
146-
}
147-
148158
fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
149159
let program = "rustfmt";
150160
let dir = std::env::current_dir()?;

0 commit comments

Comments
 (0)