From edd9a024eb637941efd7c32462c7b2af07336124 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Mon, 1 Jul 2024 15:43:16 -0700 Subject: [PATCH] fix: Respect `CARGO_HOME` during rustup install. (#29) --- CHANGELOG.md | 6 ++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/helpers.rs | 40 +++++++++++++++++++++++++++------------- src/proto.rs | 9 ++++++--- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f568a49..35e6e57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.10.4 + +#### 🐞 Fixes + +- Updated our "automatic rustup installation" to respect the `CARGO_HOME` environment variable. + ## 0.10.3 #### 🚀 Updates diff --git a/Cargo.lock b/Cargo.lock index 025cbd5..0d1da9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2384,7 +2384,7 @@ dependencies = [ [[package]] name = "rust_plugin" -version = "0.10.3" +version = "0.10.4" dependencies = [ "extism-pdk", "proto_pdk", diff --git a/Cargo.toml b/Cargo.toml index dc9d7cb..32d2bcd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_plugin" -version = "0.10.3" +version = "0.10.4" edition = "2021" license = "MIT" publish = false diff --git a/src/helpers.rs b/src/helpers.rs index cd9a71d..06e6bb6 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -9,26 +9,40 @@ extern "ExtismHost" { fn to_virtual_path(input: String) -> String; } -pub fn get_rustup_home(env: &HostEnvironment) -> Result { - // Cargo sets the RUSTUP_HOME env var when running tests, - // which causes a ton of issues, so intercept it here! - if let Some(test_env) = get_test_environment()? { - return Ok(virtual_path!(buf, test_env.sandbox).join(".home/.rustup")); - } +fn get_home_env(key: &str) -> Result, Error> { + match host_env!(key) { + Some(value) => { + if value.is_empty() { + return Ok(None); + } - Ok(match host_env!("RUSTUP_HOME") { - Some(path) => { - let path = PathBuf::from(path); + let path = PathBuf::from(value); // Variable returns a real path, so convert to virtual - if path.is_absolute() { + let path = if path.is_absolute() { virtual_path!(buf, path) } else { virtual_path!("/cwd").join(path) - } + }; + + Ok(Some(path)) } - None => env.home_dir.join(".rustup"), - }) + None => Ok(None), + } +} + +pub fn get_cargo_home(env: &HostEnvironment) -> Result { + Ok(get_home_env("CARGO_HOME")?.unwrap_or_else(|| env.home_dir.join(".cargo"))) +} + +pub fn get_rustup_home(env: &HostEnvironment) -> Result { + // Cargo sets the RUSTUP_HOME env var when running tests, + // which causes a ton of issues, so intercept it here! + if let Some(test_env) = get_test_environment()? { + return Ok(virtual_path!(buf, test_env.sandbox).join(".home/.rustup")); + } + + Ok(get_home_env("RUSTUP_HOME")?.unwrap_or_else(|| env.home_dir.join(".rustup"))) } pub fn get_channel_from_version(spec: &VersionSpec) -> String { diff --git a/src/proto.rs b/src/proto.rs index 08e5610..85f8092 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -40,13 +40,13 @@ pub fn load_versions(Json(_): Json) -> FnResult>(); @@ -144,7 +144,10 @@ pub fn native_install( // Update PATH explicitly, since we can't "reload the shell" // on the host side. This is good enough since it's deterministic. - host_env!("PATH", "/userhome/.cargo/bin"); + host_env!( + "PATH", + format!("{}:$HOME/.cargo/bin", get_cargo_home(&env)?.join("bin")) + ); } let version = &input.context.version;