4
4
5
5
use anyhow:: { Error , Result } ;
6
6
use std:: env;
7
- use std:: path:: Path ;
7
+ use std:: path:: { Path , PathBuf } ;
8
8
use std:: process:: Command ;
9
9
10
- /// Return a `String ` to use for the given executable.
10
+ /// Return a `PathBuf ` to use for the given executable.
11
11
///
12
12
/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
13
13
/// gives a valid Cargo executable; or it may return a full path to a valid
14
14
/// 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 > {
16
16
// The current implementation checks three places for an executable to use:
17
17
// 1) Appropriate environment variable (erroring if this is set but not a usable executable)
18
18
// 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
25
25
let env_var = executable_name. to_ascii_uppercase ( ) ;
26
26
if let Ok ( path) = env:: var ( & env_var) {
27
27
if is_valid_executable ( & path) {
28
- Ok ( path)
28
+ Ok ( path. into ( ) )
29
29
} else {
30
30
Err ( Error :: msg ( format ! (
31
31
"`{}` 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
34
34
}
35
35
} else {
36
36
if is_valid_executable ( executable_name) {
37
- return Ok ( executable_name. to_owned ( ) ) ;
37
+ return Ok ( executable_name. into ( ) ) ;
38
38
}
39
39
if let Some ( mut path) = dirs:: home_dir ( ) {
40
40
path. push ( ".cargo" ) ;
41
41
path. push ( "bin" ) ;
42
42
path. push ( executable_name) ;
43
43
if is_valid_executable ( & path) {
44
- return Ok ( path. into_os_string ( ) . into_string ( ) . expect ( "Invalid Unicode in path" ) ) ;
44
+ return Ok ( path) ;
45
45
}
46
46
}
47
47
// This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
0 commit comments