Skip to content

Commit a35774b

Browse files
committed
auto merge of #15158 : alexcrichton/rust/windows-paths, r=brson
In order to have the spawning semantics be the same for unix/windows, the child's PATH environment variable needs to be searched rather than the parent's environment variable. If the child is inheriting the parent's PATH, then no action need be taken as windows will do the heavy lifting. If the child specifies its own PATH, then it is searched beforehand for the target program and the result is favored if a hit is found. cc #15149, but does not close the issue because libgreen still needs to be updated.
2 parents ffd9966 + b1a964a commit a35774b

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/libnative/io/process.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,25 @@ fn spawn_process_os(cfg: ProcessConfig,
305305
})
306306
}
307307

308+
// To have the spawning semantics of unix/windows stay the same, we need to
309+
// read the *child's* PATH if one is provided. See #15149 for more details.
310+
let program = cfg.env.and_then(|env| {
311+
for &(ref key, ref v) in env.iter() {
312+
if b"PATH" != key.as_bytes_no_nul() { continue }
313+
314+
// Split the value and test each path to see if the program exists.
315+
for path in os::split_paths(v.as_bytes_no_nul()).move_iter() {
316+
let path = path.join(cfg.program.as_bytes_no_nul())
317+
.with_extension(os::consts::EXE_EXTENSION);
318+
if path.exists() {
319+
return Some(path.to_c_str())
320+
}
321+
}
322+
break
323+
}
324+
None
325+
});
326+
308327
unsafe {
309328
let mut si = zeroed_startupinfo();
310329
si.cb = mem::size_of::<STARTUPINFO>() as DWORD;
@@ -362,7 +381,8 @@ fn spawn_process_os(cfg: ProcessConfig,
362381
try!(set_fd(&out_fd, &mut si.hStdOutput, false));
363382
try!(set_fd(&err_fd, &mut si.hStdError, false));
364383

365-
let cmd_str = make_command_line(cfg.program, cfg.args);
384+
let cmd_str = make_command_line(program.as_ref().unwrap_or(cfg.program),
385+
cfg.args);
366386
let mut pi = zeroed_process_information();
367387
let mut create_err = None;
368388

src/test/run-pass/issue-15149.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(phase)]
12+
extern crate native;
13+
#[phase(plugin)]
14+
extern crate green;
15+
16+
use native::NativeTaskBuilder;
17+
use std::io::{TempDir, Command, fs};
18+
use std::os;
19+
use std::task::TaskBuilder;
20+
21+
// FIXME(#15149) libgreen still needs to be update. There is an open PR for it
22+
// but it is not yet merged.
23+
// green_start!(main)
24+
25+
fn main() {
26+
// If we're the child, make sure we were invoked correctly
27+
let args = os::args();
28+
if args.len() > 1 && args.get(1).as_slice() == "child" {
29+
return assert_eq!(args.get(0).as_slice(), "mytest");
30+
}
31+
32+
test();
33+
let (tx, rx) = channel();
34+
TaskBuilder::new().native().spawn(proc() {
35+
tx.send(test());
36+
});
37+
rx.recv();
38+
}
39+
40+
fn test() {
41+
// If we're the parent, copy our own binary to a tempr directory, and then
42+
// make it executable.
43+
let dir = TempDir::new("mytest").unwrap();
44+
let me = os::self_exe_name().unwrap();
45+
let dest = dir.path().join(format!("mytest{}", os::consts::EXE_SUFFIX));
46+
fs::copy(&me, &dest).unwrap();
47+
48+
// Append the temp directory to our own PATH.
49+
let mut path = os::split_paths(os::getenv("PATH").unwrap_or(String::new()));
50+
path.push(dir.path().clone());
51+
let path = os::join_paths(path.as_slice()).unwrap();
52+
53+
Command::new("mytest").env("PATH", path.as_slice())
54+
.arg("child")
55+
.spawn().unwrap();
56+
}

0 commit comments

Comments
 (0)