44
55use anyhow:: { Error , Result } ;
66use std:: env;
7- use std:: path:: Path ;
7+ use std:: path:: { Path , PathBuf } ;
88use std:: process:: Command ;
99
10- /// Return a `String ` to use for the given executable.
10+ /// Return a `PathBuf ` to use for the given executable.
1111///
1212/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
1313/// gives a valid Cargo executable; or it may return a full path to a valid
1414/// Cargo.
15- pub fn get_path_for_executable ( executable_name : impl AsRef < str > ) -> Result < String > {
15+ pub fn get_path_for_executable ( executable_name : impl AsRef < str > ) -> Result < PathBuf > {
1616 // The current implementation checks three places for an executable to use:
1717 // 1) Appropriate environment variable (erroring if this is set but not a usable executable)
1818 // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
@@ -25,7 +25,7 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
2525 let env_var = executable_name. to_ascii_uppercase ( ) ;
2626 if let Ok ( path) = env:: var ( & env_var) {
2727 if is_valid_executable ( & path) {
28- Ok ( path)
28+ Ok ( path. into ( ) )
2929 } else {
3030 Err ( Error :: msg ( format ! (
3131 "`{}` environment variable points to something that's not a valid executable" ,
@@ -34,14 +34,14 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
3434 }
3535 } else {
3636 if is_valid_executable ( executable_name) {
37- return Ok ( executable_name. to_owned ( ) ) ;
37+ return Ok ( executable_name. into ( ) ) ;
3838 }
3939 if let Some ( mut path) = dirs:: home_dir ( ) {
4040 path. push ( ".cargo" ) ;
4141 path. push ( "bin" ) ;
4242 path. push ( executable_name) ;
4343 if is_valid_executable ( & path) {
44- return Ok ( path. into_os_string ( ) . into_string ( ) . expect ( "Invalid Unicode in path" ) ) ;
44+ return Ok ( path) ;
4545 }
4646 }
4747 // This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
0 commit comments