1
1
use std:: env;
2
2
use std:: io;
3
3
use std:: path:: { Path , PathBuf } ;
4
- use std:: process:: { Command , Output } ;
4
+ use std:: process:: Command ;
5
5
use std:: str:: Utf8Error ;
6
6
use tracing:: info;
7
7
@@ -66,27 +66,18 @@ pub struct RustfmtRunner {
66
66
}
67
67
68
68
impl RustfmtRunner {
69
- // will be used in future PRs, just added to make the compiler happy
70
- #[ allow( dead_code) ]
71
- fn run ( & self , args : & [ & str ] ) -> io:: Result < Output > {
72
- Command :: new ( & self . binary_path )
73
- . env ( "LD_LIBRARY_PATH" , & self . ld_library_path )
74
- . args ( args)
75
- . output ( )
76
- }
77
-
78
69
fn get_binary_version ( & self ) -> Result < String , CheckDiffError > {
79
70
let Ok ( command) = Command :: new ( & self . binary_path )
80
- . env ( "LD_LIB_PATH " , & self . ld_library_path )
71
+ . env ( "LD_LIBRARY_PATH " , & self . ld_library_path )
81
72
. args ( [ "--version" ] )
82
73
. output ( )
83
74
else {
84
75
return Err ( CheckDiffError :: FailedBinaryVersioning (
85
- self . binary_path . to_path_buf ( ) ,
76
+ self . binary_path . clone ( ) ,
86
77
) ) ;
87
78
} ;
88
79
89
- let binary_version = std:: str:: from_utf8 ( & command. stdout ) ?. trim_end ( ) ;
80
+ let binary_version = std:: str:: from_utf8 ( & command. stdout ) ?. trim ( ) ;
90
81
return Ok ( binary_version. to_string ( ) ) ;
91
82
}
92
83
}
@@ -161,21 +152,21 @@ pub fn git_fetch(branch_name: &str) -> Result<(), GitError> {
161
152
return Ok ( ( ) ) ;
162
153
}
163
154
164
- pub fn git_switch ( arg : & str , should_detach : bool ) -> Result < ( ) , GitError > {
155
+ pub fn git_switch ( git_ref : & str , should_detach : bool ) -> Result < ( ) , GitError > {
165
156
let detach_arg = if should_detach { "--detach" } else { "" } ;
166
- let git_cmd = Command :: new ( "git" )
167
- . args ( [ "switch" , arg, detach_arg] )
157
+ let args = [ "switch" , git_ref, detach_arg] ;
158
+ let output = Command :: new ( "git" )
159
+ . args ( args. iter ( ) . filter ( |arg| !arg. is_empty ( ) ) )
168
160
. output ( ) ?;
169
-
170
- if !git_cmd . status . success ( ) {
161
+ if !output . status . success ( ) {
162
+ tracing :: error! ( "Git switch failed: {output:?}" ) ;
171
163
let error = GitError :: FailedSwitch {
172
- stdout : git_cmd . stdout ,
173
- stderr : git_cmd . stderr ,
164
+ stdout : output . stdout ,
165
+ stderr : output . stderr ,
174
166
} ;
175
167
return Err ( error) ;
176
168
}
177
-
178
- info ! ( "Successfully switched to {}" , arg) ;
169
+ info ! ( "Successfully switched to {git_ref}" ) ;
179
170
return Ok ( ( ) ) ;
180
171
}
181
172
@@ -189,14 +180,12 @@ pub fn change_directory_to_path(dest: &Path) -> io::Result<()> {
189
180
return Ok ( ( ) ) ;
190
181
}
191
182
192
- pub fn get_ld_lib_path ( ) -> Result < String , CheckDiffError > {
183
+ pub fn get_ld_library_path ( ) -> Result < String , CheckDiffError > {
193
184
let Ok ( command) = Command :: new ( "rustc" ) . args ( [ "--print" , "sysroot" ] ) . output ( ) else {
194
185
return Err ( CheckDiffError :: FailedCommand ( "Error getting sysroot" ) ) ;
195
186
} ;
196
-
197
187
let sysroot = std:: str:: from_utf8 ( & command. stdout ) ?. trim_end ( ) ;
198
-
199
- let ld_lib_path = format ! ( "{}/lib" , sysroot. trim_end( ) ) ;
188
+ let ld_lib_path = format ! ( "{}/lib" , sysroot) ;
200
189
return Ok ( ld_lib_path) ;
201
190
}
202
191
@@ -218,7 +207,7 @@ pub fn build_rustfmt_from_src(binary_path: PathBuf) -> Result<RustfmtRunner, Che
218
207
// binary can find it's runtime dependencies.
219
208
// See https://github.com/rust-lang/rustfmt/issues/5675
220
209
// This will prepend the `LD_LIBRARY_PATH` for the master rustfmt binary
221
- let ld_lib_path = get_ld_lib_path ( ) ?;
210
+ let ld_lib_path = get_ld_library_path ( ) ?;
222
211
223
212
info ! ( "Building rustfmt from source" ) ;
224
213
let Ok ( _) = Command :: new ( "cargo" )
@@ -257,16 +246,14 @@ pub fn compile_rustfmt(
257
246
258
247
let cargo_version = get_cargo_version ( ) ?;
259
248
info ! ( "Compiling with {}" , cargo_version) ;
260
- let src_runner = build_rustfmt_from_src ( dest. join ( "/rustfmt" ) ) ?;
261
-
249
+ let src_runner = build_rustfmt_from_src ( dest. join ( "src_rustfmt" ) ) ?;
262
250
let should_detach = commit_hash. is_some ( ) ;
263
251
git_switch (
264
252
commit_hash. unwrap_or ( feature_branch) . as_str ( ) ,
265
253
should_detach,
266
254
) ?;
267
255
268
- let feature_runner = build_rustfmt_from_src ( dest. join ( "/feature_rustfmt" ) ) ?;
269
-
256
+ let feature_runner = build_rustfmt_from_src ( dest. join ( "feature_rustfmt" ) ) ?;
270
257
info ! ( "RUSFMT_BIN {}" , src_runner. get_binary_version( ) ?) ;
271
258
info ! (
272
259
"Runtime dependencies for (src) rustfmt -- LD_LIBRARY_PATH: {}" ,
0 commit comments