From 7bdc720a9a84bfead498ae7a64868cd67506eae4 Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Fri, 22 Jul 2022 22:59:02 +1000
Subject: [PATCH 01/11] Fix updating metafiles: Skip on custom install path

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/main.rs | 43 +++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 2061ee8d..22659542 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -241,6 +241,7 @@ 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`");
@@ -338,27 +339,29 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
     }
 
     block_in_place(|| {
-        debug!("Writing .crates.toml");
-        metafiles::v1::CratesToml::append(
-            metadata_vec
-                .iter()
-                .map(|metadata| (&metadata.cvs, metadata.bins.clone())),
-        )?;
+        if !custom_install_path {
+            debug!("Writing .crates.toml");
+            metafiles::v1::CratesToml::append(
+                metadata_vec
+                    .iter()
+                    .map(|metadata| (&metadata.cvs, metadata.bins.clone())),
+            )?;
 
-        debug!("Writing .crates2.json");
-        metafiles::v2::Crates2Json::append(metadata_vec.into_iter().map(|metadata| {
-            (
-                metadata.cvs,
-                metafiles::v2::CrateInfo {
-                    version_req: Some(metadata.version_req),
-                    bins: metadata.bins,
-                    profile: "release".into(),
-                    target: metadata.target,
-                    rustc: format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
-                    ..Default::default()
-                },
-            )
-        }))?;
+            debug!("Writing .crates2.json");
+            metafiles::v2::Crates2Json::append(metadata_vec.into_iter().map(|metadata| {
+                (
+                    metadata.cvs,
+                    metafiles::v2::CrateInfo {
+                        version_req: Some(metadata.version_req),
+                        bins: metadata.bins,
+                        profile: "release".into(),
+                        target: metadata.target,
+                        rustc: format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
+                        ..Default::default()
+                    },
+                )
+            }))?;
+        }
 
         if opts.no_cleanup {
             // Consume temp_dir without removing it from fs.

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 02/11] 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.

From 5620810c55ea7e4d440f4eb6b5019a280c3eac76 Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 18:36:01 +1000
Subject: [PATCH 03/11] Simplify `get_install_path` using `cargo_home`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers.rs | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)

diff --git a/src/helpers.rs b/src/helpers.rs
index 570b243e..d70df19b 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -187,20 +187,9 @@ pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<Path
         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")), false);
-    }
-
-    // Standard $HOME/.cargo/bin
-    if let Some(d) = dirs::home_dir() {
-        let d = d.join(".cargo/bin");
-        if d.exists() {
-            debug!("using $HOME/.cargo/bin");
-
-            return (Some(d), false);
-        }
+    if let Ok(p) = cargo_home() {
+        debug!("using ({}) as cargo home", p.display());
+        return (Some(p.into()), false);
     }
 
     // Local executable dir if no cargo is found

From 5ea66574c343deaac39f0e5d0a674f55a4b2942b Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 18:37:59 +1000
Subject: [PATCH 04/11] Mod `get_install_path` to ret `Arc<Path>` instead of
 `PathBuf`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers.rs | 9 +++++----
 src/main.rs    | 4 ++--
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/helpers.rs b/src/helpers.rs
index d70df19b..26031bc8 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -3,6 +3,7 @@ use std::fs;
 use std::io;
 use std::ops;
 use std::path::{Path, PathBuf};
+use std::sync::Arc;
 
 use bytes::Bytes;
 use cargo_toml::Manifest;
@@ -174,17 +175,17 @@ pub async fn download_tar_based_and_visit<V: TarEntriesVisitor + Debug + Send +
 /// roughly follows <https://doc.rust-lang.org/cargo/commands/cargo-install.html#description>
 ///
 /// Return (install_path, is_custom_install_path)
-pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<PathBuf>, bool) {
+pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<Arc<Path>>, bool) {
     // Command line override first first
     if let Some(p) = install_path {
-        return (Some(PathBuf::from(p.as_ref())), true);
+        return (Some(Arc::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")), true);
+        return (Some(Arc::from(b.join("bin"))), true);
     }
 
     if let Ok(p) = cargo_home() {
@@ -199,7 +200,7 @@ pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<Path
         debug!("Fallback to {}", d.display());
     }
 
-    (dir, true)
+    (dir.map(Arc::from), true)
 }
 
 /// Atomically install a file.
diff --git a/src/main.rs b/src/main.rs
index c13a91b5..0b36b005 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -242,10 +242,10 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
 
     // Compute install directory
     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(|| {
+    let install_path = 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.

From 3c09cfb196b506e7345241e1c974f71b3f1ac688 Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 18:51:50 +1000
Subject: [PATCH 05/11] Fix `ci-scripts/run_tests_unix.sh`: unset `CARGO_HOME`

and `CARGO_INSTALL_ROOT`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 ci-scripts/run_tests_unix.sh | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/ci-scripts/run_tests_unix.sh b/ci-scripts/run_tests_unix.sh
index f07f89f7..e49b81a0 100755
--- a/ci-scripts/run_tests_unix.sh
+++ b/ci-scripts/run_tests_unix.sh
@@ -5,6 +5,9 @@ set -euxo pipefail
 bins="cargo-deb cargo-llvm-cov cargo-binstall"
 test_bins="cargo-deb cargo-llvm-cov"
 
+unset CARGO_INSTALL_ROOT
+unset CARGO_HOME
+
 # Install binaries using cargo-binstall
 # shellcheck disable=SC2086
 "./$1" binstall --log-level debug --no-confirm $bins

From 5bf2b4e45d245d63bafa4280835cafe06fc1f35b Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 19:08:29 +1000
Subject: [PATCH 06/11] Fix `helpers::cargo_home`: `home::cargo_home` is buggy

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers.rs | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/helpers.rs b/src/helpers.rs
index 26031bc8..52d26f0d 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -1,3 +1,4 @@
+use std::env;
 use std::fmt::Debug;
 use std::fs;
 use std::io;
@@ -44,10 +45,34 @@ mod crate_name;
 pub use crate_name::CrateName;
 
 pub fn cargo_home() -> Result<&'static Path, io::Error> {
+    fn cargo_home_inner() -> Result<PathBuf, io::Error> {
+        if let Some(p) = env::var_os("CARGO_HOME") {
+            let p = PathBuf::from(p);
+            debug!("using CARGO_HOME ({})", p.display());
+            return Ok(p);
+        }
+
+        // Standard $HOME/.cargo/bin
+        if let Some(mut d) = dirs::home_dir() {
+            d.push(".cargo");
+
+            if d.exists() {
+                debug!("using $HOME/.cargo: {}", d.display());
+
+                return Ok(d);
+            }
+        }
+
+        Err(io::Error::new(
+            io::ErrorKind::NotFound,
+            "Failed to detect cargo home",
+        ))
+    }
+
     static CARGO_HOME: OnceCell<PathBuf> = OnceCell::new();
 
     CARGO_HOME
-        .get_or_try_init(home::cargo_home)
+        .get_or_try_init(cargo_home_inner)
         .map(ops::Deref::deref)
 }
 

From 3838219d89ac821bb97070fb9c90def25cb1adba Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 19:09:06 +1000
Subject: [PATCH 07/11] Rm dep `home`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 Cargo.lock | 10 ----------
 Cargo.toml |  1 -
 2 files changed, 11 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 4ddc98b4..a476fa6b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -111,7 +111,6 @@ dependencies = [
  "flate2",
  "futures-util",
  "guess_host_triple",
- "home",
  "jobserver",
  "log",
  "miette",
@@ -521,15 +520,6 @@ dependencies = [
  "libc",
 ]
 
-[[package]]
-name = "home"
-version = "0.5.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654"
-dependencies = [
- "winapi",
-]
-
 [[package]]
 name = "http"
 version = "0.2.8"
diff --git a/Cargo.toml b/Cargo.toml
index 883a9e72..85be70fd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -28,7 +28,6 @@ crates_io_api = { version = "0.8.0", default-features = false, features = ["rust
 dirs = "4.0.0"
 flate2 = { version = "1.0.24", features = ["zlib-ng"], default-features = false }
 futures-util = { version = "0.3.21", default-features = false }
-home = "0.5.3"
 jobserver = "0.1.24"
 log = "0.4.14"
 miette = { version = "5.1.1", features = ["fancy-no-backtrace"] }

From 49f665d680b8bc90b592975b416b3947d187d31a Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 19:10:15 +1000
Subject: [PATCH 08/11] Fix confusing comment in `helpers::cargo_home`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/helpers.rs b/src/helpers.rs
index 52d26f0d..d5c18ffa 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -52,7 +52,7 @@ pub fn cargo_home() -> Result<&'static Path, io::Error> {
             return Ok(p);
         }
 
-        // Standard $HOME/.cargo/bin
+        // Standard $HOME/.cargo
         if let Some(mut d) = dirs::home_dir() {
             d.push(".cargo");
 

From b4d2e9b99c693d5177824e649c48b26167baa2a1 Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 19:16:23 +1000
Subject: [PATCH 09/11] Fix `get_install_path` where `cargo_home` succeeds

The returned path must join with `"bin"`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/helpers.rs b/src/helpers.rs
index d5c18ffa..b2ed7b3e 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -215,7 +215,7 @@ pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<Arc<
 
     if let Ok(p) = cargo_home() {
         debug!("using ({}) as cargo home", p.display());
-        return (Some(p.into()), false);
+        return (Some(p.join("bin").into()), false);
     }
 
     // Local executable dir if no cargo is found

From ad2d393be730844de96a392ea5aa46be9e3426d0 Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 19:17:26 +1000
Subject: [PATCH 10/11] Add back dep home v0.5.3

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 Cargo.lock | 10 ++++++++++
 Cargo.toml |  1 +
 2 files changed, 11 insertions(+)

diff --git a/Cargo.lock b/Cargo.lock
index a476fa6b..4ddc98b4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -111,6 +111,7 @@ dependencies = [
  "flate2",
  "futures-util",
  "guess_host_triple",
+ "home",
  "jobserver",
  "log",
  "miette",
@@ -520,6 +521,15 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "home"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654"
+dependencies = [
+ "winapi",
+]
+
 [[package]]
 name = "http"
 version = "0.2.8"
diff --git a/Cargo.toml b/Cargo.toml
index 85be70fd..883a9e72 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -28,6 +28,7 @@ crates_io_api = { version = "0.8.0", default-features = false, features = ["rust
 dirs = "4.0.0"
 flate2 = { version = "1.0.24", features = ["zlib-ng"], default-features = false }
 futures-util = { version = "0.3.21", default-features = false }
+home = "0.5.3"
 jobserver = "0.1.24"
 log = "0.4.14"
 miette = { version = "5.1.1", features = ["fancy-no-backtrace"] }

From 5367b366d1cf9e39994e0b735d84c60fdf64b04c Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Jul 2022 19:17:47 +1000
Subject: [PATCH 11/11] Use `home::cargo_home` in `helpers::cargo_home`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers.rs | 26 +-------------------------
 1 file changed, 1 insertion(+), 25 deletions(-)

diff --git a/src/helpers.rs b/src/helpers.rs
index b2ed7b3e..e6bf8b40 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -45,34 +45,10 @@ mod crate_name;
 pub use crate_name::CrateName;
 
 pub fn cargo_home() -> Result<&'static Path, io::Error> {
-    fn cargo_home_inner() -> Result<PathBuf, io::Error> {
-        if let Some(p) = env::var_os("CARGO_HOME") {
-            let p = PathBuf::from(p);
-            debug!("using CARGO_HOME ({})", p.display());
-            return Ok(p);
-        }
-
-        // Standard $HOME/.cargo
-        if let Some(mut d) = dirs::home_dir() {
-            d.push(".cargo");
-
-            if d.exists() {
-                debug!("using $HOME/.cargo: {}", d.display());
-
-                return Ok(d);
-            }
-        }
-
-        Err(io::Error::new(
-            io::ErrorKind::NotFound,
-            "Failed to detect cargo home",
-        ))
-    }
-
     static CARGO_HOME: OnceCell<PathBuf> = OnceCell::new();
 
     CARGO_HOME
-        .get_or_try_init(cargo_home_inner)
+        .get_or_try_init(home::cargo_home)
         .map(ops::Deref::deref)
 }