From 4b748cd16683b0f444ca359edd71d834e3f31f32 Mon Sep 17 00:00:00 2001 From: hamflx Date: Mon, 4 Nov 2024 11:29:39 +0800 Subject: [PATCH] Write registry values directly to avoid Path environment variable truncation. --- src/command/setup.rs | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/command/setup.rs b/src/command/setup.rs index 2f1fd771b..f54422cb7 100644 --- a/src/command/setup.rs +++ b/src/command/setup.rs @@ -225,40 +225,29 @@ If you run into problems running Volta, create {} and run `volta setup` again.", #[cfg(windows)] mod os { - use std::process::Command; - use log::debug; use volta_core::error::{Context, ErrorKind, Fallible}; use volta_core::layout::volta_home; - use winreg::enums::HKEY_CURRENT_USER; + use winreg::enums::{HKEY_CURRENT_USER, KEY_READ, KEY_WRITE}; use winreg::RegKey; pub fn setup_environment() -> Fallible<()> { let shim_dir = volta_home()?.shim_dir().to_string_lossy().to_string(); let hkcu = RegKey::predef(HKEY_CURRENT_USER); let env = hkcu - .open_subkey("Environment") + .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) .with_context(|| ErrorKind::ReadUserPathError)?; let path: String = env .get_value("Path") .with_context(|| ErrorKind::ReadUserPathError)?; if !path.contains(&shim_dir) { - // Use `setx` command to edit the user Path environment variable - let mut command = Command::new("setx"); - command.arg("Path"); - command.arg(format!("{};{}", shim_dir, path)); - - debug!("Modifying User Path with command: {:?}", command); - let output = command - .output() - .with_context(|| ErrorKind::WriteUserPathError)?; + let path = format!("{};{}", shim_dir, path); + debug!("Modifying User Path to: {}", path); - if !output.status.success() { - debug!("[setx stderr]\n{}", String::from_utf8_lossy(&output.stderr)); - debug!("[setx stdout]\n{}", String::from_utf8_lossy(&output.stdout)); - return Err(ErrorKind::WriteUserPathError.into()); - } + // see https://superuser.com/a/387625 + env.set_value("Path", &path) + .with_context(|| ErrorKind::WriteUserPathError)?; } Ok(())