Skip to content

Commit

Permalink
fix weird bug when out would get overridden by unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 authored and Mark-Simulacrum committed Mar 7, 2022
1 parent 62b522e commit 984527f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bootstrap::{Build, Config, Subcommand, VERSION};

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

// check_version warnings are not printed during setup
let changelog_suggestion =
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use crate::config::{Config, TargetSelection};
use std::thread;

fn configure(cmd: &str, host: &[&str], target: &[&str]) -> Config {
let mut config = Config::parse(&[cmd.to_owned()]);
let mut config = Config::parse(&[cmd.to_owned()], true);
// don't save toolstates
config.save_toolstates = None;
config.dry_run = true;
config.ninja_in_file = false;
// try to avoid spurious failures in dist where we create/delete each others file
config.out = PathBuf::from(env::var_os("BOOTSTRAP_OUTPUT_DIRECTORY").unwrap());
config.initial_rustc = PathBuf::from(env::var_os("RUSTC").unwrap());
config.initial_cargo = PathBuf::from(env::var_os("BOOTSTRAP_INITIAL_CARGO").unwrap());
// try to avoid spurious failures in dist where we create/delete each others file
let dir = config
.out
.join("tmp-rustbuild-tests")
Expand Down
27 changes: 21 additions & 6 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ impl Config {
config
}

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

let mut config = Config::default_opts();
Expand Down Expand Up @@ -682,11 +682,26 @@ impl Config {
let build = toml.build.unwrap_or_default();

set(&mut config.out, build.build_dir.map(String::into));
t!(fs::create_dir_all(&config.out));
config.out = t!(
config.out.canonicalize(),
format!("failed to canonicalize {}", config.out.display())
);
// NOTE: Bootstrap spawns various commands with different working directories.
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.

// FIXME: using `canonicalize()` makes this a lot more complicated than it needs to be -
// if/when `std::path::absolute` lands, we should use that instead.

// HACK: in tests, we override the build directory manually.
// Avoid creating a directory we won't actually need.
// (The original motivation for this is that CI uses read-only directories.)
if !config.out.is_absolute() && !unit_test {
// canonicalize() gives a hard error if the directory doesn't exist
t!(
fs::create_dir_all(&config.out),
format!("failed to create build dir: {}", config.out.display())
);
config.out = t!(
config.out.canonicalize(),
format!("failed to canonicalize {}", config.out.display())
);
}

if config.dry_run {
let dir = config.out.join("tmp-dry-run");
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",

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

let maybe_rules_help = Builder::get_help(&build, subcommand.as_str());
Expand Down
2 changes: 2 additions & 0 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,8 @@ impl Step for Bootstrap {
.current_dir(builder.src.join("src/bootstrap"))
.env("RUSTFLAGS", "-Cdebuginfo=2")
.env("CARGO_TARGET_DIR", builder.out.join("bootstrap"))
// HACK: bootstrap's tests want to know the output directory, but there's no way to set
// it except through config.toml. Set it through an env variable instead.
.env("BOOTSTRAP_OUTPUT_DIRECTORY", &builder.config.out)
.env("BOOTSTRAP_INITIAL_CARGO", &builder.config.initial_cargo)
.env("RUSTC_BOOTSTRAP", "1")
Expand Down

0 comments on commit 984527f

Please sign in to comment.