Skip to content

Commit 5af35c1

Browse files
committed
Don't generate bootstrap usage unless it's needed
Previously, `x.py` would unconditionally run `x.py build` to get the help message. After #76165, when checking the CI stage was moved into `Config`, that would cause an assertion failure (but only only in CI!): ``` thread 'main' panicked at 'assertion failed: `(left == right)` left: `1`, right: `2`', src/bootstrap/config.rs:619:49 ``` This changes bootstrap to only generate a help message when it needs to (when someone passes `--help`).
1 parent 78a0931 commit 5af35c1

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

src/bootstrap/flags.rs

+32-31
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ impl Default for Subcommand {
9999

100100
impl Flags {
101101
pub fn parse(args: &[String]) -> Flags {
102-
dbg!(args);
103-
let mut extra_help = String::new();
104102
let mut subcommand_help = String::from(
105103
"\
106104
Usage: x.py <subcommand> [options] [<paths>...]
@@ -172,16 +170,6 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
172170
"VALUE",
173171
);
174172

175-
// fn usage()
176-
let usage =
177-
|exit_code: i32, opts: &Options, subcommand_help: &str, extra_help: &str| -> ! {
178-
println!("{}", opts.usage(subcommand_help));
179-
if !extra_help.is_empty() {
180-
println!("{}", extra_help);
181-
}
182-
process::exit(exit_code);
183-
};
184-
185173
// We can't use getopt to parse the options until we have completed specifying which
186174
// options are valid, but under the current implementation, some options are conditional on
187175
// the subcommand. Therefore we must manually identify the subcommand first, so that we can
@@ -265,12 +253,38 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
265253
_ => {}
266254
};
267255

256+
// fn usage()
257+
let usage = |exit_code: i32, opts: &Options, verbose: bool, subcommand_help: &str| -> ! {
258+
let mut extra_help = String::new();
259+
260+
// All subcommands except `clean` can have an optional "Available paths" section
261+
if verbose {
262+
let config = Config::parse(&["build".to_string()]);
263+
let build = Build::new(config);
264+
265+
let maybe_rules_help = Builder::get_help(&build, subcommand.as_str());
266+
extra_help.push_str(maybe_rules_help.unwrap_or_default().as_str());
267+
} else if !(subcommand.as_str() == "clean" || subcommand.as_str() == "fmt") {
268+
extra_help.push_str(
269+
format!("Run `./x.py {} -h -v` to see a list of available paths.", subcommand)
270+
.as_str(),
271+
);
272+
}
273+
274+
println!("{}", opts.usage(subcommand_help));
275+
if !extra_help.is_empty() {
276+
println!("{}", extra_help);
277+
}
278+
process::exit(exit_code);
279+
};
280+
268281
// Done specifying what options are possible, so do the getopts parsing
269282
let matches = opts.parse(&args[..]).unwrap_or_else(|e| {
270283
// Invalid argument/option format
271284
println!("\n{}\n", e);
272-
usage(1, &opts, &subcommand_help, &extra_help);
285+
usage(1, &opts, false, &subcommand_help);
273286
});
287+
274288
// Extra sanity check to make sure we didn't hit this crazy corner case:
275289
//
276290
// ./x.py --frobulate clean build
@@ -438,24 +452,11 @@ Arguments:
438452
let paths = matches.free[1..].iter().map(|p| p.into()).collect::<Vec<PathBuf>>();
439453

440454
let cfg_file = env::var_os("BOOTSTRAP_CONFIG").map(PathBuf::from);
441-
442-
// All subcommands except `clean` can have an optional "Available paths" section
443-
if matches.opt_present("verbose") {
444-
let config = Config::parse(&["build".to_string()]);
445-
let build = Build::new(config);
446-
447-
let maybe_rules_help = Builder::get_help(&build, subcommand.as_str());
448-
extra_help.push_str(maybe_rules_help.unwrap_or_default().as_str());
449-
} else if !(subcommand.as_str() == "clean" || subcommand.as_str() == "fmt") {
450-
extra_help.push_str(
451-
format!("Run `./x.py {} -h -v` to see a list of available paths.", subcommand)
452-
.as_str(),
453-
);
454-
}
455+
let verbose = matches.opt_present("verbose");
455456

456457
// User passed in -h/--help?
457458
if matches.opt_present("help") {
458-
usage(0, &opts, &subcommand_help, &extra_help);
459+
usage(0, &opts, verbose, &subcommand_help);
459460
}
460461

461462
let cmd = match subcommand.as_str() {
@@ -485,7 +486,7 @@ Arguments:
485486
"clean" => {
486487
if !paths.is_empty() {
487488
println!("\nclean does not take a path argument\n");
488-
usage(1, &opts, &subcommand_help, &extra_help);
489+
usage(1, &opts, verbose, &subcommand_help);
489490
}
490491

491492
Subcommand::Clean { all: matches.opt_present("all") }
@@ -496,12 +497,12 @@ Arguments:
496497
"run" | "r" => {
497498
if paths.is_empty() {
498499
println!("\nrun requires at least a path!\n");
499-
usage(1, &opts, &subcommand_help, &extra_help);
500+
usage(1, &opts, verbose, &subcommand_help);
500501
}
501502
Subcommand::Run { paths }
502503
}
503504
_ => {
504-
usage(1, &opts, &subcommand_help, &extra_help);
505+
usage(1, &opts, verbose, &subcommand_help);
505506
}
506507
};
507508

0 commit comments

Comments
 (0)