Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write registry values directly to avoid Path environment variable truncation. #1931

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions src/command/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down