1
1
use crate :: clippy_project_root;
2
2
use shell_escape:: escape;
3
3
use std:: ffi:: OsStr ;
4
- use std:: io;
5
4
use std:: path:: Path ;
6
5
use std:: process:: { self , Command } ;
6
+ use std:: { fs, io} ;
7
7
use walkdir:: WalkDir ;
8
8
9
9
#[ derive( Debug ) ]
@@ -12,6 +12,7 @@ pub enum CliError {
12
12
IoError ( io:: Error ) ,
13
13
RustfmtNotInstalled ,
14
14
WalkDirError ( walkdir:: Error ) ,
15
+ RaSetupActive ,
15
16
}
16
17
17
18
impl From < io:: Error > for CliError {
@@ -31,12 +32,23 @@ struct FmtContext {
31
32
verbose : bool ,
32
33
}
33
34
35
+ // the "main" function of cargo dev fmt
34
36
pub fn run ( check : bool , verbose : bool ) {
35
37
fn try_run ( context : & FmtContext ) -> Result < bool , CliError > {
36
38
let mut success = true ;
37
39
38
40
let project_root = clippy_project_root ( ) ;
39
41
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
+
40
52
rustfmt_test ( context) ?;
41
53
42
54
success &= cargo_fmt ( context, project_root. as_path ( ) ) ?;
@@ -75,6 +87,13 @@ pub fn run(check: bool, verbose: bool) {
75
87
CliError :: WalkDirError ( err) => {
76
88
eprintln ! ( "error: {}" , err) ;
77
89
} ,
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
+ } ,
78
97
}
79
98
}
80
99
0 commit comments