diff options
author | Bert Belder <bertbelder@gmail.com> | 2020-06-05 22:03:47 +0200 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2020-06-05 22:15:05 +0200 |
commit | ee7727cd078b56d2ecc5d93f9308709e60a18949 (patch) | |
tree | 3ff4d90afd7941782010988f05b05c6ac0c971a3 | |
parent | 78bfeebad11954666d3104ea3556ec9f2e64f6a4 (diff) |
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.
-rw-r--r-- | cli/installer.rs | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/cli/installer.rs b/cli/installer.rs index 78840b929..40e5cf0f4 100644 --- a/cli/installer.rs +++ b/cli/installer.rs @@ -85,22 +85,18 @@ fn get_installer_root() -> Result<PathBuf, Error> { return PathBuf::from(env_dir).canonicalize(); } } - // In Windows's Powershell $HOME environmental variable maybe null - // if so use $USERPROFILE instead. - let home = env::var("HOME") - .map(String::into) - .unwrap_or_else(|_| "".to_string()); - let user_profile = env::var("USERPROFILE") - .map(String::into) - .unwrap_or_else(|_| "".to_string()); - - if home.is_empty() && user_profile.is_empty() { - 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); + // Note: on Windows, the $HOME environment variable may be set by users or by + // third party software, but it is non-standard and should not be relied upon. + let home_env_var = if cfg!(windows) { "USERPROFILE" } else { "HOME" }; + let mut home_path = + env::var_os(home_env_var) + .map(PathBuf::from) + .ok_or_else(|| { + Error::new( + ErrorKind::NotFound, + format!("${} is not defined", home_env_var), + ) + })?; home_path.push(".deno"); Ok(home_path) } |