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

Find the program using PATHEXT #37381

Closed
Closed
Show file tree
Hide file tree
Changes from 6 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
34 changes: 28 additions & 6 deletions src/libstd/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ impl Command {
pub fn env_clear(&mut self) {
self.env = Some(HashMap::new())
}
pub fn with_env(&mut self, env: &HashMap<OsString, OsString>) {
self.env = Some(env)
}
pub fn cwd(&mut self, dir: &OsStr) {
self.cwd = Some(dir.to_os_string())
}
Expand All @@ -125,12 +128,9 @@ impl Command {
self.stderr = Some(stderr);
}

pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
-> io::Result<(Process, StdioPipes)> {
// To have the spawning semantics of unix/windows stay the same, we need
// to read the *child's* PATH if one is provided. See #15149 for more
// details.
let program = self.env.as_ref().and_then(|env| {
pub fn find_program(&mut self) -> Option<Path> {
self.env.as_ref().and_then(|env| {
let env_pathext = env.get("PATHEXT");
for (key, v) in env {
if OsStr::new("PATH") != &**key { continue }

Expand All @@ -142,11 +142,33 @@ impl Command {
if fs::metadata(&path).is_ok() {
return Some(path.into_os_string())
}

// Windows relies on path extensions to resolve commands.
// Path extensions are found in the PATHEXT environment variable.
let exts = match env_pathext {
Some(e) => e,
None => continue,
};

for ext in split_paths(&exts).filter_map(|e| e.to_str()) {
let path = path.with_extension(ext.trim_matches('.'));
if fs::metadata(&path).is_ok() {
return Some(path.into_os_string())
}
}
}
break
}
None
});
}

pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
-> io::Result<(Process, StdioPipes)> {
// To have the spawning semantics of unix/windows stay the same, we need
// to read the *child's* PATH if one is provided. See #15149 for more
// details.
let program = self.find_program();

let mut si = zeroed_startupinfo();
si.cb = mem::size_of::<c::STARTUPINFO>() as c::DWORD;
Expand Down
80 changes: 80 additions & 0 deletions src/test/run-pass/process_command/find_program.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[cfg(test)]
mod test_find_program {
use std::ffi::{OsStr, OsString};
use std::process::Command;
use std::collections::HashMap;
use std::path::Path;
use std::fs::canonicalize;
use std::env::join_paths;

fn gen_env() -> HashMap<OsString, OsString> {
let env: HashMap<OsString, OsString> = HashMap::new();
env.insert(OsString::from("HOMEDRIVE"), OsString::from("C:"));
let p1 = canonicalize("./src/test/run-pass/process_command/fixtures/bin").unwrap();
let p2 = canonicalize("./src/test/run-pass/process_command/fixtures").unwrap();
let p3 = canonicalize("./src/test/run-pass/process_command").unwrap();
let paths = vec![p1, p2, p3];
let path = join_paths(paths).unwrap();
env.insert(OsString::from("PATH"), OsString::from(&path));
env.insert(OsString::from("USERNAME"), OsString::from("rust"));
env
}

fn command_with_pathext(cmd: &str) -> Command {
let mut env = gen_env();
env.insert(
OsString::from("PATHEXT"),
OsString::from(".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.MSC")
);
let mut cmd = Command::new(cmd);
cmd.with_env(&env);
cmd
}

fn command_without_pathext(cmd: &str) -> Command {
let env = gen_env();
let mut cmd = Command::new(cmd);
cmd.with_env(&env);
cmd
}

mod with_pathext_set {
use super::command_with_pathext;

fn command_on_path_found() {
let c = command_with_pathext("bin");
let bat = canonicalize("./src/test/run-pass/process_command/fixtures/bin/bin.bat");
assert_eq!(bat.ok(), c.find_program());
Copy link
Member

Choose a reason for hiding this comment

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

Hm unfortunately the find_program function here is a private implementation detail of Command so this function I don't think will pass on Windows :(

Copy link
Author

Choose a reason for hiding this comment

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

@alexcrichton I made it to be public, just as Command.cmd() and Command.env().. I wonder if it should be private as you mentioned and instead test within the Module? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Ah the public function you added is actually an internal implementation detail that's wrapped with a std::process::Command, so it won't be accessible here.

But yeah moving the test to that module would be ok.

Copy link
Author

Choose a reason for hiding this comment

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

Awesome! Im working on it

}

fn command_not_found() {
let c = command_with_pathext("missing");
assert_eq!(None, c.find_program());
}
}

mod without_pathext_set {
use super::command_without_pathext;

fn bat_command_not_found() {
let c = command_without_pathext("bin");
assert_eq!(None, c.find_program());
}

fn exe_command_found() {
let c = command_without_pathext("exec");
let exe = canonicalize("./src/test/run-pass/process_command/fixtures/bin/exec.exe");
assert_eq!(exe.ok(), c.find_program());
}
}
}
Empty file.
Empty file.