@@ -268,7 +268,7 @@ impl Command {
268
268
} else {
269
269
None
270
270
} ;
271
- let program = resolve_exe ( & self . program , child_paths) ?;
271
+ let program = resolve_exe ( & self . program , || env :: var_os ( "PATH" ) , child_paths) ?;
272
272
let mut cmd_str =
273
273
make_command_line ( program. as_os_str ( ) , & self . args , self . force_quotes_enabled ) ?;
274
274
cmd_str. push ( 0 ) ; // add null terminator
@@ -362,7 +362,11 @@ impl fmt::Debug for Command {
362
362
// Therefore this functions first assumes `.exe` was intended.
363
363
// It falls back to the plain file name if a full path is given and the extension is omitted
364
364
// or if only a file name is given and it already contains an extension.
365
- fn resolve_exe < ' a > ( exe_path : & ' a OsStr , child_paths : Option < & OsStr > ) -> io:: Result < PathBuf > {
365
+ fn resolve_exe < ' a > (
366
+ exe_path : & ' a OsStr ,
367
+ parent_paths : impl FnOnce ( ) -> Option < OsString > ,
368
+ child_paths : Option < & OsStr > ,
369
+ ) -> io:: Result < PathBuf > {
366
370
// Early return if there is no filename.
367
371
if exe_path. is_empty ( ) || path:: has_trailing_slash ( exe_path) {
368
372
return Err ( io:: Error :: new_const (
@@ -406,7 +410,7 @@ fn resolve_exe<'a>(exe_path: &'a OsStr, child_paths: Option<&OsStr>) -> io::Resu
406
410
let has_extension = exe_path. bytes ( ) . contains ( & b'.' ) ;
407
411
408
412
// Search the directories given by `search_paths`.
409
- let result = search_paths ( child_paths, |mut path| {
413
+ let result = search_paths ( parent_paths , child_paths, |mut path| {
410
414
path. push ( & exe_path) ;
411
415
if !has_extension {
412
416
path. set_extension ( EXE_EXTENSION ) ;
@@ -423,15 +427,20 @@ fn resolve_exe<'a>(exe_path: &'a OsStr, child_paths: Option<&OsStr>) -> io::Resu
423
427
424
428
// Calls `f` for every path that should be used to find an executable.
425
429
// Returns once `f` returns the path to an executable or all paths have been searched.
426
- fn search_paths < F > ( child_paths : Option < & OsStr > , mut f : F ) -> Option < PathBuf >
430
+ fn search_paths < Paths , Exists > (
431
+ parent_paths : Paths ,
432
+ child_paths : Option < & OsStr > ,
433
+ mut exists : Exists ,
434
+ ) -> Option < PathBuf >
427
435
where
428
- F : FnMut ( PathBuf ) -> Option < PathBuf > ,
436
+ Paths : FnOnce ( ) -> Option < OsString > ,
437
+ Exists : FnMut ( PathBuf ) -> Option < PathBuf > ,
429
438
{
430
439
// 1. Child paths
431
440
// This is for consistency with Rust's historic behaviour.
432
441
if let Some ( paths) = child_paths {
433
442
for path in env:: split_paths ( paths) . filter ( |p| !p. as_os_str ( ) . is_empty ( ) ) {
434
- if let Some ( path) = f ( path) {
443
+ if let Some ( path) = exists ( path) {
435
444
return Some ( path) ;
436
445
}
437
446
}
@@ -440,7 +449,7 @@ where
440
449
// 2. Application path
441
450
if let Ok ( mut app_path) = env:: current_exe ( ) {
442
451
app_path. pop ( ) ;
443
- if let Some ( path) = f ( app_path) {
452
+ if let Some ( path) = exists ( app_path) {
444
453
return Some ( path) ;
445
454
}
446
455
}
@@ -450,25 +459,25 @@ where
450
459
unsafe {
451
460
if let Ok ( Some ( path) ) = super :: fill_utf16_buf (
452
461
|buf, size| c:: GetSystemDirectoryW ( buf, size) ,
453
- |buf| f ( PathBuf :: from ( OsString :: from_wide ( buf) ) ) ,
462
+ |buf| exists ( PathBuf :: from ( OsString :: from_wide ( buf) ) ) ,
454
463
) {
455
464
return Some ( path) ;
456
465
}
457
466
#[ cfg( not( target_vendor = "uwp" ) ) ]
458
467
{
459
468
if let Ok ( Some ( path) ) = super :: fill_utf16_buf (
460
469
|buf, size| c:: GetWindowsDirectoryW ( buf, size) ,
461
- |buf| f ( PathBuf :: from ( OsString :: from_wide ( buf) ) ) ,
470
+ |buf| exists ( PathBuf :: from ( OsString :: from_wide ( buf) ) ) ,
462
471
) {
463
472
return Some ( path) ;
464
473
}
465
474
}
466
475
}
467
476
468
477
// 5. Parent paths
469
- if let Some ( parent_paths) = env :: var_os ( "PATH" ) {
478
+ if let Some ( parent_paths) = parent_paths ( ) {
470
479
for path in env:: split_paths ( & parent_paths) . filter ( |p| !p. as_os_str ( ) . is_empty ( ) ) {
471
- if let Some ( path) = f ( path) {
480
+ if let Some ( path) = exists ( path) {
472
481
return Some ( path) ;
473
482
}
474
483
}
0 commit comments