From 2667f4a517721d564bf72e4ed55ed087542dca61 Mon Sep 17 00:00:00 2001 From: Dylan Bulfin Date: Fri, 29 Nov 2024 15:38:28 -0500 Subject: [PATCH 1/2] Remove cargo dev fmt reliance on rustup --- clippy_dev/src/fmt.rs | 54 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index c66738592820..a629553d0ff2 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -3,6 +3,7 @@ use itertools::Itertools; use rustc_lexer::{TokenKind, tokenize}; use shell_escape::escape; use std::ffi::{OsStr, OsString}; +use std::io::BufRead; use std::ops::ControlFlow; use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; @@ -265,6 +266,42 @@ fn fmt_conf(check: bool) -> Result<(), Error> { Ok(()) } +fn find_rustfmt() -> Result { + #[cfg(windows)] + const WHICH_BIN: &str = "where"; + + #[cfg(not(windows))] + const WHICH_BIN: &str = "which"; + + let output = Command::new("rustup") + .args(["which", "rustfmt"]) + .stderr(Stdio::inherit()) + .output(); + + if let Ok(output) = output { + if output.status.success() { + return Ok(String::from_utf8(output.stdout).expect("invalid rustfmt path")); + } + } + + // `rustup which` has failed, fallback to platform-specific built-in command + let output = Command::new(WHICH_BIN) + .args(["rustfmt"]) + .stderr(Stdio::inherit()) + .output(); + + if let Ok(output) = output { + if output.status.success() { + // Since `where` can display multiple results we want to ignore all but one + if let Some(Ok(line)) = output.stdout.lines().next() { + return Ok(line); + } + } + } + + Err(Error::RustfmtNotInstalled) +} + fn run_rustfmt(context: &FmtContext) -> Result<(), Error> { let project_root = clippy_project_root(); @@ -307,16 +344,15 @@ fn run_rustfmt(context: &FmtContext) -> Result<(), Error> { // the "main" function of cargo dev fmt pub fn run(check: bool, verbose: bool) { - 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; + match find_rustfmt() { + Ok(path) => rustfmt_path = path, + Err(e) => { + e.display(); + 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 { From f3dbae6b8d13efb0f9ca47473f678c8cd1c39c6a Mon Sep 17 00:00:00 2001 From: Dylan Bulfin Date: Sat, 30 Nov 2024 07:48:39 -0500 Subject: [PATCH 2/2] Change cargo dev fmt fallback --- clippy_dev/src/fmt.rs | 57 +++++++++---------------------------------- 1 file changed, 12 insertions(+), 45 deletions(-) diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index a629553d0ff2..0b0269e90192 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -3,7 +3,6 @@ use itertools::Itertools; use rustc_lexer::{TokenKind, tokenize}; use shell_escape::escape; use std::ffi::{OsStr, OsString}; -use std::io::BufRead; use std::ops::ControlFlow; use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; @@ -266,42 +265,6 @@ fn fmt_conf(check: bool) -> Result<(), Error> { Ok(()) } -fn find_rustfmt() -> Result { - #[cfg(windows)] - const WHICH_BIN: &str = "where"; - - #[cfg(not(windows))] - const WHICH_BIN: &str = "which"; - - let output = Command::new("rustup") - .args(["which", "rustfmt"]) - .stderr(Stdio::inherit()) - .output(); - - if let Ok(output) = output { - if output.status.success() { - return Ok(String::from_utf8(output.stdout).expect("invalid rustfmt path")); - } - } - - // `rustup which` has failed, fallback to platform-specific built-in command - let output = Command::new(WHICH_BIN) - .args(["rustfmt"]) - .stderr(Stdio::inherit()) - .output(); - - if let Ok(output) = output { - if output.status.success() { - // Since `where` can display multiple results we want to ignore all but one - if let Some(Ok(line)) = output.stdout.lines().next() { - return Ok(line); - } - } - } - - Err(Error::RustfmtNotInstalled) -} - fn run_rustfmt(context: &FmtContext) -> Result<(), Error> { let project_root = clippy_project_root(); @@ -344,14 +307,18 @@ fn run_rustfmt(context: &FmtContext) -> Result<(), Error> { // the "main" function of cargo dev fmt pub fn run(check: bool, verbose: bool) { - let mut rustfmt_path; - match find_rustfmt() { - Ok(path) => rustfmt_path = path, - Err(e) => { - e.display(); - process::exit(1); - }, - } + let result = Command::new("rustup") + .args(["which", "rustfmt"]) + .stderr(Stdio::inherit()) + .output(); + + let mut rustfmt_path = if let Ok(output) = result + && output.status.success() + { + String::from_utf8(output.stdout).expect("invalid rustfmt path") + } else { + String::from("rustfmt") + }; rustfmt_path.truncate(rustfmt_path.trim_end().len());