about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crates/toolchain/src/lib.rs21
-rw-r--r--editors/code/src/toolchain.ts30
2 files changed, 35 insertions, 16 deletions
diff --git a/crates/toolchain/src/lib.rs b/crates/toolchain/src/lib.rs
index 67bdad2aadd..729f84a8150 100644
--- a/crates/toolchain/src/lib.rs
+++ b/crates/toolchain/src/lib.rs
@@ -31,8 +31,9 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
     //      example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
     // 2) `<executable_name>`
     //      example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
-    // 3) `~/.cargo/bin/<executable_name>`
-    //      example: for cargo, this tries ~/.cargo/bin/cargo
+    // 3) `$CARGO_HOME/bin/<executable_name>`
+    //      where $CARGO_HOME defaults to ~/.cargo (see https://doc.rust-lang.org/cargo/guide/cargo-home.html)
+    //      example: for cargo, this tries $CARGO_HOME/bin/cargo, or ~/.cargo/bin/cargo if $CARGO_HOME is unset.
     //      It seems that this is a reasonable place to try for cargo, rustc, and rustup
     let env_var = executable_name.to_ascii_uppercase();
     if let Some(path) = env::var_os(env_var) {
@@ -43,8 +44,7 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
         return executable_name.into();
     }
 
-    if let Some(mut path) = home::home_dir() {
-        path.push(".cargo");
+    if let Some(mut path) = get_cargo_home() {
         path.push("bin");
         path.push(executable_name);
         if let Some(path) = probe(path) {
@@ -60,6 +60,19 @@ fn lookup_in_path(exec: &str) -> bool {
     env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some()
 }
 
+fn get_cargo_home() -> Option<PathBuf> {
+    if let Some(path) = env::var_os("CARGO_HOME") {
+        return Some(path.into());
+    }
+
+    if let Some(mut path) = home::home_dir() {
+        path.push(".cargo");
+        return Some(path);
+    }
+
+    None
+}
+
 fn probe(path: PathBuf) -> Option<PathBuf> {
     let with_extension = match env::consts::EXE_EXTENSION {
         "" => None,
diff --git a/editors/code/src/toolchain.ts b/editors/code/src/toolchain.ts
index e1ca4954280..eb70b88871e 100644
--- a/editors/code/src/toolchain.ts
+++ b/editors/code/src/toolchain.ts
@@ -156,19 +156,10 @@ export const getPathForExecutable = memoizeAsync(
 
         if (await lookupInPath(executableName)) return executableName;
 
-        try {
-            // hmm, `os.homedir()` seems to be infallible
-            // it is not mentioned in docs and cannot be inferred by the type signature...
-            const standardPath = vscode.Uri.joinPath(
-                vscode.Uri.file(os.homedir()),
-                ".cargo",
-                "bin",
-                executableName
-            );
-
+        const cargoHome = getCargoHome();
+        if (cargoHome) {
+            const standardPath = vscode.Uri.joinPath(cargoHome, "bin", executableName);
             if (await isFileAtUri(standardPath)) return standardPath.fsPath;
-        } catch (err) {
-            log.error("Failed to read the fs info", err);
         }
         return executableName;
     }
@@ -190,6 +181,21 @@ async function lookupInPath(exec: string): Promise<boolean> {
     return false;
 }
 
+function getCargoHome(): vscode.Uri | null {
+    const envVar = process.env["CARGO_HOME"];
+    if (envVar) return vscode.Uri.file(envVar);
+
+    try {
+        // hmm, `os.homedir()` seems to be infallible
+        // it is not mentioned in docs and cannot be inferred by the type signature...
+        return vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".cargo");
+    } catch (err) {
+        log.error("Failed to read the fs info", err);
+    }
+
+    return null;
+}
+
 async function isFileAtPath(path: string): Promise<boolean> {
     return isFileAtUri(vscode.Uri.file(path));
 }