11use crate :: clippy_project_root;
22use shell_escape:: escape;
33use std:: ffi:: OsStr ;
4- use std:: io;
54use std:: path:: Path ;
65use std:: process:: { self , Command } ;
6+ use std:: { fs, io} ;
77use 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
1718impl From < io:: Error > for CliError {
@@ -31,12 +32,23 @@ struct FmtContext {
3132 verbose : bool ,
3233}
3334
35+ // the "main" function of cargo dev fmt
3436pub 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