@@ -3,7 +3,7 @@ use itertools::Itertools;
3
3
use shell_escape:: escape;
4
4
use std:: ffi:: { OsStr , OsString } ;
5
5
use std:: path:: Path ;
6
- use std:: process:: { self , Command } ;
6
+ use std:: process:: { self , Command , Stdio } ;
7
7
use std:: { fs, io} ;
8
8
use walkdir:: WalkDir ;
9
9
@@ -31,6 +31,7 @@ impl From<walkdir::Error> for CliError {
31
31
struct FmtContext {
32
32
check : bool ,
33
33
verbose : bool ,
34
+ rustfmt_path : String ,
34
35
}
35
36
36
37
// the "main" function of cargo dev fmt
@@ -102,7 +103,23 @@ Please revert the changes to Cargo.tomls first."
102
103
}
103
104
}
104
105
105
- let context = FmtContext { check, verbose } ;
106
+ let output = Command :: new ( "rustup" )
107
+ . args ( [ "which" , "rustfmt" ] )
108
+ . stderr ( Stdio :: inherit ( ) )
109
+ . output ( )
110
+ . expect ( "error running `rustup which rustfmt`" ) ;
111
+ if !output. status . success ( ) {
112
+ eprintln ! ( "`rustup which rustfmt` did not execute successfully" ) ;
113
+ process:: exit ( 1 ) ;
114
+ }
115
+ let mut rustfmt_path = String :: from_utf8 ( output. stdout ) . expect ( "invalid rustfmt path" ) ;
116
+ rustfmt_path. truncate ( rustfmt_path. trim_end ( ) . len ( ) ) ;
117
+
118
+ let context = FmtContext {
119
+ check,
120
+ verbose,
121
+ rustfmt_path,
122
+ } ;
106
123
let result = try_run ( & context) ;
107
124
let code = match result {
108
125
Ok ( true ) => 0 ,
@@ -142,6 +159,7 @@ fn exec(
142
159
}
143
160
144
161
let output = Command :: new ( & program)
162
+ . env ( "RUSTFMT" , & context. rustfmt_path )
145
163
. current_dir ( & dir)
146
164
. args ( args. iter ( ) )
147
165
. output ( )
@@ -162,7 +180,6 @@ fn exec(
162
180
fn cargo_fmt ( context : & FmtContext , path : & Path ) -> Result < bool , CliError > {
163
181
let mut args = vec ! [ "fmt" , "--all" ] ;
164
182
if context. check {
165
- args. push ( "--" ) ;
166
183
args. push ( "--check" ) ;
167
184
}
168
185
let success = exec ( context, "cargo" , path, & args) ?;
@@ -203,7 +220,7 @@ fn rustfmt(context: &FmtContext, paths: impl Iterator<Item = OsString>) -> Resul
203
220
}
204
221
args. extend ( paths) ;
205
222
206
- let success = exec ( context, "rustfmt" , std:: env:: current_dir ( ) ?, & args) ?;
223
+ let success = exec ( context, & context . rustfmt_path , std:: env:: current_dir ( ) ?, & args) ?;
207
224
208
225
Ok ( success)
209
226
}
0 commit comments