@@ -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 ,
@@ -141,8 +158,12 @@ fn exec(
141
158
println ! ( "{}" , format_command( & program, & dir, args) ) ;
142
159
}
143
160
144
- let child = Command :: new ( & program) . current_dir ( & dir) . args ( args. iter ( ) ) . spawn ( ) ?;
145
- let output = child. wait_with_output ( ) ?;
161
+ let output = Command :: new ( & program)
162
+ . env ( "RUSTFMT" , & context. rustfmt_path )
163
+ . current_dir ( & dir)
164
+ . args ( args. iter ( ) )
165
+ . output ( )
166
+ . unwrap ( ) ;
146
167
let success = output. status . success ( ) ;
147
168
148
169
if !context. check && !success {
@@ -159,7 +180,6 @@ fn exec(
159
180
fn cargo_fmt ( context : & FmtContext , path : & Path ) -> Result < bool , CliError > {
160
181
let mut args = vec ! [ "fmt" , "--all" ] ;
161
182
if context. check {
162
- args. push ( "--" ) ;
163
183
args. push ( "--check" ) ;
164
184
}
165
185
let success = exec ( context, "cargo" , path, & args) ?;
@@ -200,7 +220,7 @@ fn rustfmt(context: &FmtContext, paths: impl Iterator<Item = OsString>) -> Resul
200
220
}
201
221
args. extend ( paths) ;
202
222
203
- let success = exec ( context, "rustfmt" , std:: env:: current_dir ( ) ?, & args) ?;
223
+ let success = exec ( context, & context . rustfmt_path , std:: env:: current_dir ( ) ?, & args) ?;
204
224
205
225
Ok ( success)
206
226
}
0 commit comments