Skip to content

Commit fcd99aa

Browse files
committed
rustc_driver: Frob the global PATH less
Environment variables are global state so this can lead to surprising results if the driver is called in a multithreaded environment (e.g. doctests). There shouldn't be any memory corruption that's possible, but a lot of the bots have been failing because they can't find `cc` or `gcc` in the path during doctests, and I highly suspect that it is due to the compiler modifying `PATH` in a multithreaded fashion. This commit moves the logic for appending to `PATH` to only affect the child process instead of also affecting the parent, at least for the linking stage. When loading dynamic libraries the compiler still modifies `PATH` on Windows, but this may be more difficult to fix than spawning off a new process.
1 parent 8af39ce commit fcd99aa

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/librustc_driver/driver.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -762,18 +762,11 @@ pub fn phase_5_run_llvm_passes(sess: &Session,
762762
pub fn phase_6_link_output(sess: &Session,
763763
trans: &trans::CrateTranslation,
764764
outputs: &OutputFilenames) {
765-
let old_path = env::var_os("PATH").unwrap_or(OsString::new());
766-
let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths();
767-
new_path.extend(env::split_paths(&old_path));
768-
env::set_var("PATH", &env::join_paths(&new_path).unwrap());
769-
770765
time(sess.time_passes(), "linking", (), |_|
771766
link::link_binary(sess,
772767
trans,
773768
outputs,
774769
&trans.link.crate_name));
775-
776-
env::set_var("PATH", &old_path);
777770
}
778771

779772
fn escape_dep_filename(filename: &str) -> String {

src/librustc_trans/back/link.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use util::sha2::{Digest, Sha256};
3030
use util::fs::fix_windows_verbatim_for_gcc;
3131
use rustc_back::tempdir::TempDir;
3232

33+
use std::env;
3334
use std::fs::{self, PathExt};
3435
use std::io::{self, Read, Write};
3536
use std::mem;
@@ -796,7 +797,16 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool,
796797

797798
// The invocations of cc share some flags across platforms
798799
let pname = get_cc_prog(sess);
799-
let mut cmd = Command::new(&pname[..]);
800+
let mut cmd = Command::new(&pname);
801+
802+
// The compiler's sysroot often has some bundled tools, so add it to the
803+
// PATH for the child.
804+
let mut new_path = sess.host_filesearch(PathKind::All)
805+
.get_tools_search_paths();
806+
if let Some(path) = env::var_os("PATH") {
807+
new_path.extend(env::split_paths(&path));
808+
}
809+
cmd.env("PATH", env::join_paths(new_path).unwrap());
800810

801811
let root = sess.target_filesearch(PathKind::Native).get_lib_path();
802812
cmd.args(&sess.target.target.options.pre_link_args);

0 commit comments

Comments
 (0)