@@ -268,7 +268,7 @@ impl Command {
268268 } else {
269269 None
270270 } ;
271- let program = resolve_exe ( & self . program , child_paths) ?;
271+ let program = resolve_exe ( & self . program , || env :: var_os ( "PATH" ) , child_paths) ?;
272272 let mut cmd_str =
273273 make_command_line ( program. as_os_str ( ) , & self . args , self . force_quotes_enabled ) ?;
274274 cmd_str. push ( 0 ) ; // add null terminator
@@ -362,7 +362,11 @@ impl fmt::Debug for Command {
362362// Therefore this functions first assumes `.exe` was intended.
363363// It falls back to the plain file name if a full path is given and the extension is omitted
364364// 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 > {
366370 // Early return if there is no filename.
367371 if exe_path. is_empty ( ) || path:: has_trailing_slash ( exe_path) {
368372 return Err ( io:: Error :: new_const (
@@ -406,7 +410,7 @@ fn resolve_exe<'a>(exe_path: &'a OsStr, child_paths: Option<&OsStr>) -> io::Resu
406410 let has_extension = exe_path. bytes ( ) . contains ( & b'.' ) ;
407411
408412 // 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| {
410414 path. push ( & exe_path) ;
411415 if !has_extension {
412416 path. set_extension ( EXE_EXTENSION ) ;
@@ -423,15 +427,20 @@ fn resolve_exe<'a>(exe_path: &'a OsStr, child_paths: Option<&OsStr>) -> io::Resu
423427
424428// Calls `f` for every path that should be used to find an executable.
425429// 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 >
427435where
428- F : FnMut ( PathBuf ) -> Option < PathBuf > ,
436+ Paths : FnOnce ( ) -> Option < OsString > ,
437+ Exists : FnMut ( PathBuf ) -> Option < PathBuf > ,
429438{
430439 // 1. Child paths
431440 // This is for consistency with Rust's historic behaviour.
432441 if let Some ( paths) = child_paths {
433442 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) {
435444 return Some ( path) ;
436445 }
437446 }
@@ -440,7 +449,7 @@ where
440449 // 2. Application path
441450 if let Ok ( mut app_path) = env:: current_exe ( ) {
442451 app_path. pop ( ) ;
443- if let Some ( path) = f ( app_path) {
452+ if let Some ( path) = exists ( app_path) {
444453 return Some ( path) ;
445454 }
446455 }
@@ -450,25 +459,25 @@ where
450459 unsafe {
451460 if let Ok ( Some ( path) ) = super :: fill_utf16_buf (
452461 |buf, size| c:: GetSystemDirectoryW ( buf, size) ,
453- |buf| f ( PathBuf :: from ( OsString :: from_wide ( buf) ) ) ,
462+ |buf| exists ( PathBuf :: from ( OsString :: from_wide ( buf) ) ) ,
454463 ) {
455464 return Some ( path) ;
456465 }
457466 #[ cfg( not( target_vendor = "uwp" ) ) ]
458467 {
459468 if let Ok ( Some ( path) ) = super :: fill_utf16_buf (
460469 |buf, size| c:: GetWindowsDirectoryW ( buf, size) ,
461- |buf| f ( PathBuf :: from ( OsString :: from_wide ( buf) ) ) ,
470+ |buf| exists ( PathBuf :: from ( OsString :: from_wide ( buf) ) ) ,
462471 ) {
463472 return Some ( path) ;
464473 }
465474 }
466475 }
467476
468477 // 5. Parent paths
469- if let Some ( parent_paths) = env :: var_os ( "PATH" ) {
478+ if let Some ( parent_paths) = parent_paths ( ) {
470479 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) {
472481 return Some ( path) ;
473482 }
474483 }
0 commit comments