1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 04:48:52 -05:00

To find home dir, use only $USERPROFILE on Windows, $HOME on Posix (#6132)

$HOME is meaningless on Windows. It may be set by users or by third
party software, but it is non-standard and should not be relied upon.

Likewise, $USERPROFILE is meaningless on other platforms.
This commit is contained in:
Bert Belder 2020-06-05 22:03:47 +02:00
parent 78bfeebad1
commit ee7727cd07
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461

View file

@ -85,22 +85,18 @@ fn get_installer_root() -> Result<PathBuf, Error> {
return PathBuf::from(env_dir).canonicalize(); return PathBuf::from(env_dir).canonicalize();
} }
} }
// In Windows's Powershell $HOME environmental variable maybe null // Note: on Windows, the $HOME environment variable may be set by users or by
// if so use $USERPROFILE instead. // third party software, but it is non-standard and should not be relied upon.
let home = env::var("HOME") let home_env_var = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
.map(String::into) let mut home_path =
.unwrap_or_else(|_| "".to_string()); env::var_os(home_env_var)
let user_profile = env::var("USERPROFILE") .map(PathBuf::from)
.map(String::into) .ok_or_else(|| {
.unwrap_or_else(|_| "".to_string()); Error::new(
ErrorKind::NotFound,
if home.is_empty() && user_profile.is_empty() { format!("${} is not defined", home_env_var),
return Err(Error::new(ErrorKind::Other, "$HOME is not defined")); )
} })?;
let home_path = if !home.is_empty() { home } else { user_profile };
let mut home_path = PathBuf::from(home_path);
home_path.push(".deno"); home_path.push(".deno");
Ok(home_path) Ok(home_path)
} }