@@ -31,8 +31,9 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
31
31
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
32
32
// 2) `<executable_name>`
33
33
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
34
- // 3) `~/.cargo/bin/<executable_name>`
35
- // example: for cargo, this tries ~/.cargo/bin/cargo
34
+ // 3) `$CARGO_HOME/bin/<executable_name>`
35
+ // where $CARGO_HOME defaults to ~/.cargo (see https://doc.rust-lang.org/cargo/guide/cargo-home.html)
36
+ // example: for cargo, this tries $CARGO_HOME/bin/cargo, or ~/.cargo/bin/cargo if $CARGO_HOME is unset.
36
37
// It seems that this is a reasonable place to try for cargo, rustc, and rustup
37
38
let env_var = executable_name. to_ascii_uppercase ( ) ;
38
39
if let Some ( path) = env:: var_os ( env_var) {
@@ -43,8 +44,7 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
43
44
return executable_name. into ( ) ;
44
45
}
45
46
46
- if let Some ( mut path) = home:: home_dir ( ) {
47
- path. push ( ".cargo" ) ;
47
+ if let Some ( mut path) = get_cargo_home ( ) {
48
48
path. push ( "bin" ) ;
49
49
path. push ( executable_name) ;
50
50
if let Some ( path) = probe ( path) {
@@ -60,6 +60,19 @@ fn lookup_in_path(exec: &str) -> bool {
60
60
env:: split_paths ( & paths) . map ( |path| path. join ( exec) ) . find_map ( probe) . is_some ( )
61
61
}
62
62
63
+ fn get_cargo_home ( ) -> Option < PathBuf > {
64
+ if let Some ( path) = env:: var_os ( "CARGO_HOME" ) {
65
+ return Some ( path. into ( ) ) ;
66
+ }
67
+
68
+ if let Some ( mut path) = home:: home_dir ( ) {
69
+ path. push ( ".cargo" ) ;
70
+ return Some ( path) ;
71
+ }
72
+
73
+ None
74
+ }
75
+
63
76
fn probe ( path : PathBuf ) -> Option < PathBuf > {
64
77
let with_extension = match env:: consts:: EXE_EXTENSION {
65
78
"" => None ,
0 commit comments