|
| 1 | +//! Test step for `tidy` is somewhat special becomes it combines *two* roles: |
| 2 | +//! |
| 3 | +//! 1. Check code style (among other things) of *other* code in the source tree. |
| 4 | +//! 2. Running the `tidy` tool's *self-tests*. |
| 5 | +
|
| 6 | +// FIXME(#137178): currently, these two roles are combined into *one* step, presumably to make sure |
| 7 | +// that both steps get exercised in CI. However, this makes it annoying if you want to work on tidy |
| 8 | +// itself and *only* want to run tidy's self-tests (e.g. for faster iteration feedback). |
| 9 | + |
| 10 | +use std::ffi::OsStr; |
| 11 | + |
| 12 | +use clap_complete::shells; |
| 13 | +#[cfg(feature = "tracing")] |
| 14 | +use tracing::instrument; |
| 15 | + |
| 16 | +use crate::DocTests; |
| 17 | +use crate::core::build_steps::tool::Tool; |
| 18 | +use crate::core::builder::{Builder, RunConfig, ShouldRun, Step}; |
| 19 | +use crate::core::config::flags::get_completion; |
| 20 | + |
| 21 | +/// Runs `src/tools/tidy` and `cargo fmt --check` to detect various style problems in the |
| 22 | +/// repository. |
| 23 | +/// |
| 24 | +/// (To run the tidy tool's internal tests, use the alias "tidyselftest" instead.) |
| 25 | +// FIXME(#137178): break tidy self test out into its own step. If we still want to preserve the |
| 26 | +// current `./x test tidy` behavior, ensure the tidy self-test step *explicitly*. |
| 27 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 28 | +pub struct Tidy; |
| 29 | + |
| 30 | +impl Step for Tidy { |
| 31 | + type Output = (); |
| 32 | + const DEFAULT: bool = true; |
| 33 | + const ONLY_HOSTS: bool = true; |
| 34 | + |
| 35 | + /// Runs the `tidy` tool. |
| 36 | + /// |
| 37 | + /// This tool in `src/tools` checks up on various bits and pieces of style and |
| 38 | + /// otherwise just implements a few lint-like checks that are specific to the |
| 39 | + /// compiler itself. |
| 40 | + /// |
| 41 | + /// Once tidy passes, this step also runs `fmt --check` if tests are being run |
| 42 | + /// for the `dev` or `nightly` channels. |
| 43 | + fn run(self, builder: &Builder<'_>) { |
| 44 | + let mut cmd = builder.tool_cmd(Tool::Tidy); |
| 45 | + cmd.arg(&builder.src); |
| 46 | + cmd.arg(&builder.initial_cargo); |
| 47 | + cmd.arg(&builder.out); |
| 48 | + // Tidy is heavily IO constrained. Still respect `-j`, but use a higher limit if `jobs` |
| 49 | + // hasn't been configured. |
| 50 | + let jobs = builder.config.jobs.unwrap_or_else(|| { |
| 51 | + 8 * std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32 |
| 52 | + }); |
| 53 | + cmd.arg(jobs.to_string()); |
| 54 | + if builder.is_verbose() { |
| 55 | + cmd.arg("--verbose"); |
| 56 | + } |
| 57 | + if builder.config.cmd.bless() { |
| 58 | + cmd.arg("--bless"); |
| 59 | + } |
| 60 | + if let Some(s) = builder.config.cmd.extra_checks() { |
| 61 | + cmd.arg(format!("--extra-checks={s}")); |
| 62 | + } |
| 63 | + let mut args = std::env::args_os(); |
| 64 | + if args.any(|arg| arg == OsStr::new("--")) { |
| 65 | + cmd.arg("--"); |
| 66 | + cmd.args(args); |
| 67 | + } |
| 68 | + |
| 69 | + if builder.config.channel == "dev" || builder.config.channel == "nightly" { |
| 70 | + if !builder.config.json_output { |
| 71 | + builder.info("fmt check"); |
| 72 | + if builder.initial_rustfmt().is_none() { |
| 73 | + let inferred_rustfmt_dir = builder.initial_sysroot.join("bin"); |
| 74 | + eprintln!( |
| 75 | + "\ |
| 76 | +ERROR: no `rustfmt` binary found in {PATH} |
| 77 | +INFO: `rust.channel` is currently set to \"{CHAN}\" |
| 78 | +HELP: if you are testing a beta branch, set `rust.channel` to \"beta\" in the `config.toml` file |
| 79 | +HELP: to skip test's attempt to check tidiness, pass `--skip src/tools/tidy` to `x.py test`", |
| 80 | + PATH = inferred_rustfmt_dir.display(), |
| 81 | + CHAN = builder.config.channel, |
| 82 | + ); |
| 83 | + crate::exit!(1); |
| 84 | + } |
| 85 | + let all = false; |
| 86 | + crate::core::build_steps::format::format( |
| 87 | + builder, |
| 88 | + !builder.config.cmd.bless(), |
| 89 | + all, |
| 90 | + &[], |
| 91 | + ); |
| 92 | + } else { |
| 93 | + eprintln!( |
| 94 | + "WARNING: `--json-output` is not supported on rustfmt, formatting will be skipped" |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + builder.info("tidy check"); |
| 100 | + cmd.delay_failure().run(builder); |
| 101 | + |
| 102 | + builder.info("x.py completions check"); |
| 103 | + let [bash, zsh, fish, powershell] = ["x.py.sh", "x.py.zsh", "x.py.fish", "x.py.ps1"] |
| 104 | + .map(|filename| builder.src.join("src/etc/completions").join(filename)); |
| 105 | + if builder.config.cmd.bless() { |
| 106 | + builder.ensure(crate::core::build_steps::run::GenerateCompletions); |
| 107 | + } else if get_completion(shells::Bash, &bash).is_some() |
| 108 | + || get_completion(shells::Fish, &fish).is_some() |
| 109 | + || get_completion(shells::PowerShell, &powershell).is_some() |
| 110 | + || crate::flags::get_completion(shells::Zsh, &zsh).is_some() |
| 111 | + { |
| 112 | + eprintln!( |
| 113 | + "x.py completions were changed; run `x.py run generate-completions` to update them" |
| 114 | + ); |
| 115 | + crate::exit!(1); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
| 120 | + let default = run.builder.doc_tests != DocTests::Only; |
| 121 | + run.path("src/tools/tidy").default_condition(default) |
| 122 | + } |
| 123 | + |
| 124 | + fn make_run(run: RunConfig<'_>) { |
| 125 | + run.builder.ensure(Tidy); |
| 126 | + } |
| 127 | +} |
0 commit comments