From 7cd36b23beb5815fc05b5ecc50f35b9a29633d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Thu, 3 Oct 2019 21:59:09 +0200 Subject: [PATCH] try go fix cargo fmt invocation --- clippy_dev/Cargo.toml | 1 + clippy_dev/src/fmt.rs | 32 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/clippy_dev/Cargo.toml b/clippy_dev/Cargo.toml index e2e946d06f27..8e8771a20d5f 100644 --- a/clippy_dev/Cargo.toml +++ b/clippy_dev/Cargo.toml @@ -11,3 +11,4 @@ regex = "1" lazy_static = "1.0" shell-escape = "0.1" walkdir = "2" +home = "0.5.0" diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index 9f0b68baf9db..181559431813 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -1,3 +1,4 @@ +use home; use shell_escape::escape; use std::ffi::OsStr; use std::io; @@ -31,6 +32,26 @@ struct FmtContext { verbose: bool, } +fn cargo_fmt(context: &FmtContext, path: &Path) -> Result { + let mut args = vec!["+nightly", "fmt", "--all"]; + if context.check { + args.push("--"); + args.push("--check"); + } + // in case something aliases cargo=cargo --foo, the order of args will be messed up + // because we will end up with cargo --foo +nightly fmt but the "+nightly" needs to come first + // get the absolute path to the cargo binary inside $CARGO_HOME + let cargo_home = home::cargo_home().expect("failed to get cargo home path!"); + let cargo_path = cargo_home.join("bin").join("cargo"); + assert!( + cargo_path.is_file(), + format!("ERROR: '{}' is not a path to the cargo binary!", cargo_path.display()) + ); + let success = exec(context, cargo_path, path, &args)?; + + Ok(success) +} + pub fn run(check: bool, verbose: bool) { fn try_run(context: &FmtContext) -> Result { let mut success = true; @@ -134,17 +155,6 @@ fn exec( Ok(success) } -fn cargo_fmt(context: &FmtContext, path: &Path) -> Result { - let mut args = vec!["+nightly", "fmt", "--all"]; - if context.check { - args.push("--"); - args.push("--check"); - } - let success = exec(context, "cargo", path, &args)?; - - Ok(success) -} - fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> { let program = "rustfmt"; let dir = std::env::current_dir()?;