Skip to content

Commit 91fa25c

Browse files
committed
clippy dev fmt: don't format if we have a local rustc repo enabled as path dependency via cargo dev ra-setup.
rustfmt would try to format the entire rustc repo, probably because it sees it as a local dependency.
1 parent 404c50f commit 91fa25c

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

clippy_dev/src/fmt.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::clippy_project_root;
22
use shell_escape::escape;
33
use std::ffi::OsStr;
4-
use std::io;
54
use std::path::Path;
65
use std::process::{self, Command};
6+
use std::{fs, io};
77
use walkdir::WalkDir;
88

99
#[derive(Debug)]
@@ -12,6 +12,7 @@ pub enum CliError {
1212
IoError(io::Error),
1313
RustfmtNotInstalled,
1414
WalkDirError(walkdir::Error),
15+
RaSetupActive,
1516
}
1617

1718
impl From<io::Error> for CliError {
@@ -31,12 +32,23 @@ struct FmtContext {
3132
verbose: bool,
3233
}
3334

35+
// the "main" function of cargo dev fmt
3436
pub fn run(check: bool, verbose: bool) {
3537
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
3638
let mut success = true;
3739

3840
let project_root = clippy_project_root();
3941

42+
// if we added a local rustc repo as path dependency to clippy for rust analyzer, we do NOT want to
43+
// format because rustfmt would also format the entire rustc repo as it is a local
44+
// dependency
45+
if fs::read_to_string(project_root.join("Cargo.toml"))
46+
.expect("Failed to read clippy Cargo.toml")
47+
.contains(&"[target.'cfg(NOT_A_PLATFORM)'.dependencies]")
48+
{
49+
return Err(CliError::RaSetupActive);
50+
}
51+
4052
rustfmt_test(context)?;
4153

4254
success &= cargo_fmt(context, project_root.as_path())?;
@@ -75,6 +87,13 @@ pub fn run(check: bool, verbose: bool) {
7587
CliError::WalkDirError(err) => {
7688
eprintln!("error: {}", err);
7789
},
90+
CliError::RaSetupActive => {
91+
eprintln!(
92+
"error: a local rustc repo is enabled as path dependency via `cargo dev ra-setup`.
93+
Not formatting because that would format the local repo as well!
94+
Please revert the changes to Cargo.tomls first."
95+
);
96+
},
7897
}
7998
}
8099

0 commit comments

Comments
 (0)