]> git.lizzy.rs Git - rust.git/blobdiff - clippy_dev/src/fmt.rs
Rollup merge of #88782 - asquared31415:issue-79559, r=cjgillot
[rust.git] / clippy_dev / src / fmt.rs
index 69cd046a855e3112de5f3a9d29c292b49cf2a625..c81eb40d52f3551ffc698db36acf7767b5c3874e 100644 (file)
@@ -1,17 +1,18 @@
+use crate::clippy_project_root;
 use shell_escape::escape;
 use std::ffi::OsStr;
-use std::io;
-use std::path::{Path, PathBuf};
+use std::path::Path;
 use std::process::{self, Command};
+use std::{fs, io};
 use walkdir::WalkDir;
 
 #[derive(Debug)]
 pub enum CliError {
-    CommandFailed(String),
+    CommandFailed(String, String),
     IoError(io::Error),
-    ProjectRootNotFound,
     RustfmtNotInstalled,
     WalkDirError(walkdir::Error),
+    RaSetupActive,
 }
 
 impl From<io::Error> for CliError {
@@ -31,31 +32,39 @@ struct FmtContext {
     verbose: bool,
 }
 
+// the "main" function of cargo dev fmt
 pub fn run(check: bool, verbose: bool) {
     fn try_run(context: &FmtContext) -> Result<bool, CliError> {
         let mut success = true;
 
-        let project_root = project_root()?;
+        let project_root = clippy_project_root();
+
+        // if we added a local rustc repo as path dependency to clippy for rust analyzer, we do NOT want to
+        // format because rustfmt would also format the entire rustc repo as it is a local
+        // dependency
+        if fs::read_to_string(project_root.join("Cargo.toml"))
+            .expect("Failed to read clippy Cargo.toml")
+            .contains(&"[target.'cfg(NOT_A_PLATFORM)'.dependencies]")
+        {
+            return Err(CliError::RaSetupActive);
+        }
 
         rustfmt_test(context)?;
 
         success &= cargo_fmt(context, project_root.as_path())?;
         success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
         success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
+        success &= cargo_fmt(context, &project_root.join("lintcheck"))?;
 
         for entry in WalkDir::new(project_root.join("tests")) {
             let entry = entry?;
             let path = entry.path();
 
-            if path.extension() != Some("rs".as_ref())
-                || entry.file_name() == "ice-3891.rs"
-                // Avoid rustfmt bug rust-lang/rustfmt#1873
-                || cfg!(windows) && entry.file_name() == "implicit_hasher.rs"
-            {
+            if path.extension() != Some("rs".as_ref()) || entry.file_name() == "ice-3891.rs" {
                 continue;
             }
 
-            success &= rustfmt(context, &path)?;
+            success &= rustfmt(context, path)?;
         }
 
         Ok(success)
@@ -63,21 +72,25 @@ fn try_run(context: &FmtContext) -> Result<bool, CliError> {
 
     fn output_err(err: CliError) {
         match err {
-            CliError::CommandFailed(command) => {
-                eprintln!("error: A command failed! `{}`", command);
+            CliError::CommandFailed(command, stderr) => {
+                eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
             },
             CliError::IoError(err) => {
                 eprintln!("error: {}", err);
             },
-            CliError::ProjectRootNotFound => {
-                eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
-            },
             CliError::RustfmtNotInstalled => {
                 eprintln!("error: rustfmt nightly is not installed.");
             },
             CliError::WalkDirError(err) => {
                 eprintln!("error: {}", err);
             },
+            CliError::RaSetupActive => {
+                eprintln!(
+                    "error: a local rustc repo is enabled as path dependency via `cargo dev setup intellij`.
+Not formatting because that would format the local repo as well!
+Please revert the changes to Cargo.tomls first."
+                );
+            },
         }
     }
 
@@ -88,7 +101,7 @@ fn output_err(err: CliError) {
         Ok(false) => {
             eprintln!();
             eprintln!("Formatting check failed.");
-            eprintln!("Run `./util/dev fmt` to update formatting.");
+            eprintln!("Run `cargo dev fmt` to update formatting.");
             1
         },
         Err(err) => {
@@ -120,12 +133,16 @@ fn exec(
         println!("{}", format_command(&program, &dir, args));
     }
 
-    let mut child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
-    let code = child.wait()?;
-    let success = code.success();
+    let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
+    let output = child.wait_with_output()?;
+    let success = output.status.success();
 
     if !context.check && !success {
-        return Err(CliError::CommandFailed(format_command(&program, &dir, args)));
+        let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
+        return Err(CliError::CommandFailed(
+            format_command(&program, &dir, args),
+            String::from(stderr),
+        ));
     }
 
     Ok(success)
@@ -161,7 +178,10 @@ fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
     {
         Err(CliError::RustfmtNotInstalled)
     } else {
-        Err(CliError::CommandFailed(format_command(&program, &dir, args)))
+        Err(CliError::CommandFailed(
+            format_command(&program, &dir, args),
+            std::str::from_utf8(&output.stderr).unwrap_or("").to_string(),
+        ))
     }
 }
 
@@ -176,22 +196,3 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
     }
     Ok(success)
 }
-
-fn project_root() -> Result<PathBuf, CliError> {
-    let current_dir = std::env::current_dir()?;
-    for path in current_dir.ancestors() {
-        let result = std::fs::read_to_string(path.join("Cargo.toml"));
-        if let Err(err) = &result {
-            if err.kind() == io::ErrorKind::NotFound {
-                continue;
-            }
-        }
-
-        let content = result?;
-        if content.contains("[package]\nname = \"clippy\"") {
-            return Ok(path.to_path_buf());
-        }
-    }
-
-    Err(CliError::ProjectRootNotFound)
-}