Skip to content

Commit 984527f

Browse files
jyn514Mark-Simulacrum
authored andcommitted
fix weird bug when out would get overridden by unit tests
1 parent 62b522e commit 984527f

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

src/bootstrap/bin/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bootstrap::{Build, Config, Subcommand, VERSION};
1111

1212
fn main() {
1313
let args = env::args().skip(1).collect::<Vec<_>>();
14-
let config = Config::parse(&args);
14+
let config = Config::parse(&args, false);
1515

1616
// check_version warnings are not printed during setup
1717
let changelog_suggestion =

src/bootstrap/builder/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use crate::config::{Config, TargetSelection};
33
use std::thread;
44

55
fn configure(cmd: &str, host: &[&str], target: &[&str]) -> Config {
6-
let mut config = Config::parse(&[cmd.to_owned()]);
6+
let mut config = Config::parse(&[cmd.to_owned()], true);
77
// don't save toolstates
88
config.save_toolstates = None;
99
config.dry_run = true;
1010
config.ninja_in_file = false;
11-
// try to avoid spurious failures in dist where we create/delete each others file
1211
config.out = PathBuf::from(env::var_os("BOOTSTRAP_OUTPUT_DIRECTORY").unwrap());
1312
config.initial_rustc = PathBuf::from(env::var_os("RUSTC").unwrap());
1413
config.initial_cargo = PathBuf::from(env::var_os("BOOTSTRAP_INITIAL_CARGO").unwrap());
14+
// try to avoid spurious failures in dist where we create/delete each others file
1515
let dir = config
1616
.out
1717
.join("tmp-rustbuild-tests")

src/bootstrap/config.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ impl Config {
619619
config
620620
}
621621

622-
pub fn parse(args: &[String]) -> Config {
622+
pub fn parse(args: &[String], unit_test: bool) -> Config {
623623
let flags = Flags::parse(&args);
624624

625625
let mut config = Config::default_opts();
@@ -682,11 +682,26 @@ impl Config {
682682
let build = toml.build.unwrap_or_default();
683683

684684
set(&mut config.out, build.build_dir.map(String::into));
685-
t!(fs::create_dir_all(&config.out));
686-
config.out = t!(
687-
config.out.canonicalize(),
688-
format!("failed to canonicalize {}", config.out.display())
689-
);
685+
// NOTE: Bootstrap spawns various commands with different working directories.
686+
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
687+
688+
// FIXME: using `canonicalize()` makes this a lot more complicated than it needs to be -
689+
// if/when `std::path::absolute` lands, we should use that instead.
690+
691+
// HACK: in tests, we override the build directory manually.
692+
// Avoid creating a directory we won't actually need.
693+
// (The original motivation for this is that CI uses read-only directories.)
694+
if !config.out.is_absolute() && !unit_test {
695+
// canonicalize() gives a hard error if the directory doesn't exist
696+
t!(
697+
fs::create_dir_all(&config.out),
698+
format!("failed to create build dir: {}", config.out.display())
699+
);
700+
config.out = t!(
701+
config.out.canonicalize(),
702+
format!("failed to canonicalize {}", config.out.display())
703+
);
704+
}
690705

691706
if config.dry_run {
692707
let dir = config.out.join("tmp-dry-run");

src/bootstrap/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
343343

344344
// All subcommands except `clean` can have an optional "Available paths" section
345345
if verbose {
346-
let config = Config::parse(&["build".to_string()]);
346+
let config = Config::parse(&["build".to_string()], false);
347347
let build = Build::new(config);
348348

349349
let maybe_rules_help = Builder::get_help(&build, subcommand.as_str());

src/bootstrap/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,8 @@ impl Step for Bootstrap {
23462346
.current_dir(builder.src.join("src/bootstrap"))
23472347
.env("RUSTFLAGS", "-Cdebuginfo=2")
23482348
.env("CARGO_TARGET_DIR", builder.out.join("bootstrap"))
2349+
// HACK: bootstrap's tests want to know the output directory, but there's no way to set
2350+
// it except through config.toml. Set it through an env variable instead.
23492351
.env("BOOTSTRAP_OUTPUT_DIRECTORY", &builder.config.out)
23502352
.env("BOOTSTRAP_INITIAL_CARGO", &builder.config.initial_cargo)
23512353
.env("RUSTC_BOOTSTRAP", "1")

0 commit comments

Comments
 (0)