Don't insert PATH variable on Windows (#3422)

* Don't insert PATH variable on Windows

* Simplify fix

* Just centralize the var

* Add a message about why we have to workaround the issue
This commit is contained in:
JT
2021-05-13 15:03:49 +12:00
committed by GitHub
parent 874ecd6c88
commit 9b8b1bad57
10 changed files with 42 additions and 25 deletions

View File

@ -16,6 +16,7 @@ pub fn nu(
let mut nu_dict = TaggedDictBuilder::new(&tag);
let mut dict = TaggedDictBuilder::new(&tag);
for v in env.iter() {
if v.0 != "PATH" && v.0 != "Path" {
dict.insert_untagged(v.0, UntaggedValue::string(v.1));
@ -49,6 +50,19 @@ pub fn nu(
}
}
// A note about environment variables:
//
// Environment variables in Unix platforms are case-sensitive. On Windows, case-sensitivity is context-dependent.
// In DOS, running `SET` will show you the list of environment variables, but their names will be all upper-cased.
// In PowerShell, running `Get-ChildItem Env:` will show you a list of environment variables, and they will match
// the case in the environment variable section of the user configuration
//
// Rust currently returns the DOS-style, all-uppercase environment variables on Windows (as of 1.52) when running
// std::env::vars(), rather than the case-sensitive Environment.GetEnvironmentVariables() of .NET that PowerShell
// uses.
//
// For now, we work around the discrepency as best we can by merging the two into what is shown to the user as the
// 'path' column of `$nu`
let mut table = vec![];
for v in env.iter() {
if v.0 == "PATH" || v.0 == "Path" {

View File

@ -11,6 +11,7 @@ use nu_errors::ShellError;
use nu_protocol::{hir, ConfigPath};
use nu_source::{Span, Tag};
use nu_stream::InputStream;
use nu_test_support::NATIVE_PATH_ENV_VAR;
use parking_lot::Mutex;
use std::sync::atomic::AtomicBool;
use std::{path::Path, sync::Arc};
@ -112,7 +113,7 @@ impl EvaluationContext {
let env_vars = self.scope.get_env_vars();
for (var, val) in env_vars {
if var == "PATH" || var == "Path" || var == "path" {
if var == NATIVE_PATH_ENV_VAR {
std::env::set_var(var, val);
break;
}
@ -174,13 +175,7 @@ impl EvaluationContext {
let joined_paths = cfg_paths
.map(|mut cfg_paths| {
//existing paths are prepended to path
let env_paths = if let Some(env_paths) = self.scope.get_env("PATH") {
Some(env_paths)
} else if let Some(env_paths) = self.scope.get_env("Path") {
Some(env_paths)
} else {
self.scope.get_env("path")
};
let env_paths = self.scope.get_env(NATIVE_PATH_ENV_VAR);
if let Some(env_paths) = env_paths {
let mut env_paths = std::env::split_paths(&env_paths).collect::<Vec<_>>();
@ -210,7 +205,7 @@ impl EvaluationContext {
self.scope.enter_scope_with_tag(tag);
self.scope.add_env(cfg.env_map());
if let Some(path) = joined_paths {
self.scope.add_env_var("PATH", path);
self.scope.add_env_var(NATIVE_PATH_ENV_VAR, path);
}
self.scope.set_exit_scripts(exit_scripts);
@ -243,13 +238,7 @@ impl EvaluationContext {
let joined_paths = cfg_paths
.map(|mut cfg_paths| {
//existing paths are prepended to path
let env_paths = if let Some(env_paths) = self.scope.get_env("PATH") {
Some(env_paths)
} else if let Some(env_paths) = self.scope.get_env("Path") {
Some(env_paths)
} else {
self.scope.get_env("path")
};
let env_paths = self.scope.get_env(NATIVE_PATH_ENV_VAR);
if let Some(env_paths) = env_paths {
let mut env_paths = std::env::split_paths(&env_paths).collect::<Vec<_>>();
@ -279,7 +268,7 @@ impl EvaluationContext {
frame.env = cfg.env_map();
if let Some(path) = joined_paths {
frame.env.insert("PATH".to_string(), path);
frame.env.insert(NATIVE_PATH_ENV_VAR.to_string(), path);
}
frame.exitscripts = exit_scripts;