From 79476e490bf7f927d09b7264a5ecac0e5a832eae Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 18:33:30 +1000
Subject: [PATCH] Fix `custom_install_path` detection

Installing to `CARGO_INSTALL_ROOT` or the local executable dir should be
considered a custom installation path.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers.rs | 15 +++++++++------
 src/main.rs    | 12 +++++-------
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/helpers.rs b/src/helpers.rs
index a1ef3c5f..570b243e 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -172,22 +172,25 @@ pub async fn download_tar_based_and_visit<V: TarEntriesVisitor + Debug + Send +
 
 /// Fetch install path from environment
 /// roughly follows <https://doc.rust-lang.org/cargo/commands/cargo-install.html#description>
-pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> Option<PathBuf> {
+///
+/// Return (install_path, is_custom_install_path)
+pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<PathBuf>, bool) {
     // Command line override first first
     if let Some(p) = install_path {
-        return Some(PathBuf::from(p.as_ref()));
+        return (Some(PathBuf::from(p.as_ref())), true);
     }
 
     // Environmental variables
     if let Ok(p) = std::env::var("CARGO_INSTALL_ROOT") {
         debug!("using CARGO_INSTALL_ROOT ({p})");
         let b = PathBuf::from(p);
-        return Some(b.join("bin"));
+        return (Some(b.join("bin")), true);
     }
+
     if let Ok(p) = std::env::var("CARGO_HOME") {
         debug!("using CARGO_HOME ({p})");
         let b = PathBuf::from(p);
-        return Some(b.join("bin"));
+        return (Some(b.join("bin")), false);
     }
 
     // Standard $HOME/.cargo/bin
@@ -196,7 +199,7 @@ pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> Option<PathB
         if d.exists() {
             debug!("using $HOME/.cargo/bin");
 
-            return Some(d);
+            return (Some(d), false);
         }
     }
 
@@ -207,7 +210,7 @@ pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> Option<PathB
         debug!("Fallback to {}", d.display());
     }
 
-    dir
+    (dir, true)
 }
 
 /// Atomically install a file.
diff --git a/src/main.rs b/src/main.rs
index 22659542..c13a91b5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -241,13 +241,11 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
     let desired_targets = get_desired_targets(&opts.targets);
 
     // Compute install directory
-    let custom_install_path = opts.install_path.is_some();
-    let install_path: Arc<Path> = Arc::from(
-        get_install_path(opts.install_path.as_deref()).ok_or_else(|| {
-            error!("No viable install path found of specified, try `--install-path`");
-            miette!("No install path found or specified")
-        })?,
-    );
+    let (install_path, custom_install_path) = get_install_path(opts.install_path.as_deref());
+    let install_path: Arc<Path> = Arc::from(install_path.ok_or_else(|| {
+        error!("No viable install path found of specified, try `--install-path`");
+        miette!("No install path found or specified")
+    })?);
     debug!("Using install path: {}", install_path.display());
 
     // Create a temporary directory for downloads etc.