Skip to content

Commit c057802

Browse files
committed
Auto merge of #26382 - alexcrichton:less-racy-path, r=brson
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.
2 parents a951569 + fcd99aa commit c057802

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/librustc_driver/driver.rs

-7
Original file line numberDiff line numberDiff line change
@@ -778,18 +778,11 @@ pub fn phase_5_run_llvm_passes(sess: &Session,
778778
pub fn phase_6_link_output(sess: &Session,
779779
trans: &trans::CrateTranslation,
780780
outputs: &OutputFilenames) {
781-
let old_path = env::var_os("PATH").unwrap_or(OsString::new());
782-
let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths();
783-
new_path.extend(env::split_paths(&old_path));
784-
env::set_var("PATH", &env::join_paths(&new_path).unwrap());
785-
786781
time(sess.time_passes(), "linking", (), |_|
787782
link::link_binary(sess,
788783
trans,
789784
outputs,
790785
&trans.link.crate_name));
791-
792-
env::set_var("PATH", &old_path);
793786
}
794787

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

src/librustc_trans/back/link.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use util::sha2::{Digest, Sha256};
2929
use util::fs::fix_windows_verbatim_for_gcc;
3030
use rustc_back::tempdir::TempDir;
3131

32+
use std::env;
3233
use std::fs::{self, PathExt};
3334
use std::io::{self, Read, Write};
3435
use std::mem;
@@ -794,7 +795,16 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool,
794795

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

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

0 commit comments

Comments
 (0)