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

Fix Windows Command::env("PATH") #87863

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
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
29 changes: 24 additions & 5 deletions library/std/src/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#[cfg(test)]
mod tests;

use crate::borrow::Borrow;
use crate::cmp;
use crate::collections::BTreeMap;
use crate::convert::{TryFrom, TryInto};
Expand Down Expand Up @@ -46,6 +45,12 @@ pub struct EnvKey {
utf16: Vec<u16>,
}

impl EnvKey {
fn new<T: Into<OsString>>(key: T) -> Self {
EnvKey::from(key.into())
}
}

// Comparing Windows environment variable keys[1] are behaviourally the
// composition of two operations[2]:
//
Expand Down Expand Up @@ -100,6 +105,20 @@ impl PartialEq for EnvKey {
}
}
}
impl PartialOrd<str> for EnvKey {
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
Some(self.cmp(&EnvKey::new(other)))
}
}
impl PartialEq<str> for EnvKey {
fn eq(&self, other: &str) -> bool {
if self.os_string.len() != other.len() {
false
} else {
self.cmp(&EnvKey::new(other)) == cmp::Ordering::Equal
}
}
}
Comment on lines +108 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need these implementations? Silently allocating on every comparison seems like a bad idea.

The PartialEq implementation seems to be used only for == "PATH" and nothing else, so it's not that bad, but it might be good to improve that at some point. The result of that comparison also seems to only affect sys/unix and to be entirely unused on Windows.

The PartialOrd implementation, however, seems unnecessary. It all still compiles when removing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points. I could remove those implementations and maybe I should add a #[cfg(not(windows))] to have_changed_path? So as to guard against the possibility of it accidentally being used in the future, or at least until a longer term fix is made.


// Environment variable keys should preserve their original case even though
// they are compared using a caseless string mapping.
Expand All @@ -115,9 +134,9 @@ impl From<EnvKey> for OsString {
}
}

impl Borrow<OsStr> for EnvKey {
fn borrow(&self) -> &OsStr {
&self.os_string
impl From<&OsStr> for EnvKey {
fn from(k: &OsStr) -> Self {
Self::from(k.to_os_string())
}
}

Expand Down Expand Up @@ -242,7 +261,7 @@ impl Command {
// to read the *child's* PATH if one is provided. See #15149 for more
// details.
let program = maybe_env.as_ref().and_then(|env| {
if let Some(v) = env.get(OsStr::new("PATH")) {
if let Some(v) = env.get(&EnvKey::new("PATH")) {
// Split the value and test each path to see if the
// program exists.
for path in split_paths(&v) {
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/sys_common/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ impl CommandEnv {

// The following functions build up changes
pub fn set(&mut self, key: &OsStr, value: &OsStr) {
let key = EnvKey::from(key);
self.maybe_saw_path(&key);
self.vars.insert(key.to_owned().into(), Some(value.to_owned()));
self.vars.insert(key, Some(value.to_owned()));
}

pub fn remove(&mut self, key: &OsStr) {
let key = EnvKey::from(key);
self.maybe_saw_path(&key);
if self.clear {
self.vars.remove(key);
self.vars.remove(&key);
} else {
self.vars.insert(key.to_owned().into(), None);
self.vars.insert(key, None);
}
}

Expand All @@ -87,7 +89,7 @@ impl CommandEnv {
self.saw_path || self.clear
}

fn maybe_saw_path(&mut self, key: &OsStr) {
fn maybe_saw_path(&mut self, key: &EnvKey) {
if !self.saw_path && key == "PATH" {
self.saw_path = true;
}
Expand Down