bootstrap/core/build_steps/
toolstate.rs

1//! [Toolstate] checks to keep tools building
2//!
3//! Reachable via `./x.py test` but mostly relevant for CI, since it isn't run locally by default.
4//!
5//! [Toolstate]: https://forge.rust-lang.org/infra/toolstate.html
6
7use std::collections::HashMap;
8use std::io::{Seek, SeekFrom};
9use std::path::{Path, PathBuf};
10use std::{env, fmt, fs, time};
11
12use serde_derive::{Deserialize, Serialize};
13
14use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
15use crate::utils::helpers::{self, t};
16
17// Each cycle is 42 days long (6 weeks); the last week is 35..=42 then.
18const BETA_WEEK_START: u64 = 35;
19
20#[cfg(target_os = "linux")]
21const OS: Option<&str> = Some("linux");
22
23#[cfg(windows)]
24const OS: Option<&str> = Some("windows");
25
26#[cfg(all(not(target_os = "linux"), not(windows)))]
27const OS: Option<&str> = None;
28
29type ToolstateData = HashMap<Box<str>, ToolState>;
30
31#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, PartialOrd)]
32#[serde(rename_all = "kebab-case")]
33/// Whether a tool can be compiled, tested or neither
34pub enum ToolState {
35    /// The tool compiles successfully, but the test suite fails
36    TestFail = 1,
37    /// The tool compiles successfully and its test suite passes
38    TestPass = 2,
39    /// The tool can't even be compiled
40    BuildFail = 0,
41}
42
43impl fmt::Display for ToolState {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        write!(
46            f,
47            "{}",
48            match self {
49                ToolState::TestFail => "test-fail",
50                ToolState::TestPass => "test-pass",
51                ToolState::BuildFail => "build-fail",
52            }
53        )
54    }
55}
56
57/// Number of days after the last promotion of beta.
58/// Its value is 41 on the Tuesday where "Promote master to beta (T-2)" happens.
59/// The Wednesday after this has value 0.
60/// We track this value to prevent regressing tools in the last week of the 6-week cycle.
61fn days_since_beta_promotion() -> u64 {
62    let since_epoch = t!(time::SystemTime::UNIX_EPOCH.elapsed());
63    (since_epoch.as_secs() / 86400 - 20) % 42
64}
65
66// These tools must test-pass on the beta/stable channels.
67//
68// On the nightly channel, their build step must be attempted, but they may not
69// be able to build successfully.
70static STABLE_TOOLS: &[(&str, &str)] = &[
71    ("book", "src/doc/book"),
72    ("nomicon", "src/doc/nomicon"),
73    ("reference", "src/doc/reference"),
74    ("rust-by-example", "src/doc/rust-by-example"),
75    ("edition-guide", "src/doc/edition-guide"),
76];
77
78// These tools are permitted to not build on the beta/stable channels.
79//
80// We do require that we checked whether they build or not on the tools builder,
81// though, as otherwise we will be unable to file an issue if they start
82// failing.
83static NIGHTLY_TOOLS: &[(&str, &str)] = &[("embedded-book", "src/doc/embedded-book")];
84
85fn print_error(tool: &str, submodule: &str) {
86    eprintln!();
87    eprintln!("We detected that this PR updated '{tool}', but its tests failed.");
88    eprintln!();
89    eprintln!("If you do intend to update '{tool}', please check the error messages above and");
90    eprintln!("commit another update.");
91    eprintln!();
92    eprintln!("If you do NOT intend to update '{tool}', please ensure you did not accidentally");
93    eprintln!("change the submodule at '{submodule}'. You may ask your reviewer for the");
94    eprintln!("proper steps.");
95    crate::exit!(3);
96}
97
98fn check_changed_files(builder: &Builder<'_>, toolstates: &HashMap<Box<str>, ToolState>) {
99    // Changed files
100    let output = helpers::git(None)
101        .arg("diff")
102        .arg("--name-status")
103        .arg("HEAD")
104        .arg("HEAD^")
105        .run_capture(builder)
106        .stdout();
107
108    for (tool, submodule) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
109        let changed = output.lines().any(|l| l.starts_with('M') && l.ends_with(submodule));
110        eprintln!("Verifying status of {tool}...");
111        if !changed {
112            continue;
113        }
114
115        eprintln!("This PR updated '{submodule}', verifying if status is 'test-pass'...");
116        if toolstates[*tool] != ToolState::TestPass {
117            print_error(tool, submodule);
118        }
119    }
120}
121
122#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
123pub struct ToolStateCheck;
124
125impl Step for ToolStateCheck {
126    type Output = ();
127
128    /// Checks tool state status.
129    ///
130    /// This is intended to be used in the `checktools.sh` script. To use
131    /// this, set `save-toolstates` in `config.toml` so that tool status will
132    /// be saved to a JSON file. Then, run `x.py test --no-fail-fast` for all
133    /// of the tools to populate the JSON file. After that is done, this
134    /// command can be run to check for any status failures, and exits with an
135    /// error if there are any.
136    ///
137    /// This also handles publishing the results to the `history` directory of
138    /// the toolstate repo <https://github.com/rust-lang-nursery/rust-toolstate>
139    /// if the env var `TOOLSTATE_PUBLISH` is set. Note that there is a
140    /// *separate* step of updating the `latest.json` file and creating GitHub
141    /// issues and comments in `src/ci/publish_toolstate.sh`, which is only
142    /// performed on master. (The shell/python code is intended to be migrated
143    /// here eventually.)
144    ///
145    /// The rules for failure are:
146    /// * If the PR modifies a tool, the status must be test-pass.
147    ///   NOTE: There is intent to change this, see
148    ///   <https://github.com/rust-lang/rust/issues/65000>.
149    /// * All "stable" tools must be test-pass on the stable or beta branches.
150    /// * During beta promotion week, a PR is not allowed to "regress" a
151    ///   stable tool. That is, the status is not allowed to get worse
152    ///   (test-pass to test-fail or build-fail).
153    fn run(self, builder: &Builder<'_>) {
154        if builder.config.dry_run() {
155            return;
156        }
157
158        let days_since_beta_promotion = days_since_beta_promotion();
159        let in_beta_week = days_since_beta_promotion >= BETA_WEEK_START;
160        let is_nightly = !(builder.config.channel == "beta" || builder.config.channel == "stable");
161        let toolstates = builder.toolstates();
162
163        let mut did_error = false;
164
165        for (tool, _) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
166            if !toolstates.contains_key(*tool) {
167                did_error = true;
168                eprintln!("ERROR: Tool `{tool}` was not recorded in tool state.");
169            }
170        }
171
172        if did_error {
173            crate::exit!(1);
174        }
175
176        check_changed_files(builder, &toolstates);
177        checkout_toolstate_repo(builder);
178        let old_toolstate = read_old_toolstate();
179
180        for (tool, _) in STABLE_TOOLS.iter() {
181            let state = toolstates[*tool];
182
183            if state != ToolState::TestPass {
184                if !is_nightly {
185                    did_error = true;
186                    eprintln!("ERROR: Tool `{tool}` should be test-pass but is {state}");
187                } else if in_beta_week {
188                    let old_state = old_toolstate
189                        .iter()
190                        .find(|ts| ts.tool == *tool)
191                        .expect("latest.json missing tool")
192                        .state();
193                    if state < old_state {
194                        did_error = true;
195                        eprintln!(
196                            "ERROR: Tool `{tool}` has regressed from {old_state} to {state} during beta week."
197                        );
198                    } else {
199                        // This warning only appears in the logs, which most
200                        // people won't read. It's mostly here for testing and
201                        // debugging.
202                        eprintln!(
203                            "WARNING: Tool `{tool}` is not test-pass (is `{state}`), \
204                            this should be fixed before beta is branched."
205                        );
206                    }
207                }
208                // `publish_toolstate.py` is responsible for updating
209                // `latest.json` and creating comments/issues warning people
210                // if there is a regression. That all happens in a separate CI
211                // job on the master branch once the PR has passed all tests
212                // on the `auto` branch.
213            }
214        }
215
216        if did_error {
217            crate::exit!(1);
218        }
219
220        if builder.config.channel == "nightly" && env::var_os("TOOLSTATE_PUBLISH").is_some() {
221            commit_toolstate_change(builder, &toolstates);
222        }
223    }
224
225    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
226        run.alias("check-tools")
227    }
228
229    fn make_run(run: RunConfig<'_>) {
230        run.builder.ensure(ToolStateCheck);
231    }
232}
233
234impl Builder<'_> {
235    fn toolstates(&self) -> HashMap<Box<str>, ToolState> {
236        if let Some(ref path) = self.config.save_toolstates {
237            if let Some(parent) = path.parent() {
238                // Ensure the parent directory always exists
239                t!(std::fs::create_dir_all(parent));
240            }
241            let mut file = t!(fs::OpenOptions::new()
242                .create(true)
243                .truncate(false)
244                .write(true)
245                .read(true)
246                .open(path));
247
248            serde_json::from_reader(&mut file).unwrap_or_default()
249        } else {
250            Default::default()
251        }
252    }
253
254    /// Updates the actual toolstate of a tool.
255    ///
256    /// The toolstates are saved to the file specified by the key
257    /// `rust.save-toolstates` in `config.toml`. If unspecified, nothing will be
258    /// done. The file is updated immediately after this function completes.
259    pub fn save_toolstate(&self, tool: &str, state: ToolState) {
260        use std::io::Write;
261
262        // If we're in a dry run setting we don't want to save toolstates as
263        // that means if we e.g. panic down the line it'll look like we tested
264        // everything (but we actually haven't).
265        if self.config.dry_run() {
266            return;
267        }
268        // Toolstate isn't tracked for clippy or rustfmt, but since most tools do, we avoid checking
269        // in all the places we could save toolstate and just do so here.
270        if tool == "clippy-driver" || tool == "rustfmt" {
271            return;
272        }
273        if let Some(ref path) = self.config.save_toolstates {
274            if let Some(parent) = path.parent() {
275                // Ensure the parent directory always exists
276                t!(std::fs::create_dir_all(parent));
277            }
278            let mut file = t!(fs::OpenOptions::new()
279                .create(true)
280                .truncate(false)
281                .read(true)
282                .write(true)
283                .open(path));
284
285            let mut current_toolstates: HashMap<Box<str>, ToolState> =
286                serde_json::from_reader(&mut file).unwrap_or_default();
287            current_toolstates.insert(tool.into(), state);
288            t!(file.seek(SeekFrom::Start(0)));
289            t!(file.set_len(0));
290            t!(serde_json::to_writer(&file, &current_toolstates));
291            t!(writeln!(file)); // make sure this ends in a newline
292        }
293    }
294}
295
296fn toolstate_repo() -> String {
297    env::var("TOOLSTATE_REPO")
298        .unwrap_or_else(|_| "https://github.com/rust-lang-nursery/rust-toolstate.git".to_string())
299}
300
301/// Directory where the toolstate repo is checked out.
302const TOOLSTATE_DIR: &str = "rust-toolstate";
303
304/// Checks out the toolstate repo into `TOOLSTATE_DIR`.
305fn checkout_toolstate_repo(builder: &Builder<'_>) {
306    if let Ok(token) = env::var("TOOLSTATE_REPO_ACCESS_TOKEN") {
307        prepare_toolstate_config(builder, &token);
308    }
309    if Path::new(TOOLSTATE_DIR).exists() {
310        eprintln!("Cleaning old toolstate directory...");
311        t!(fs::remove_dir_all(TOOLSTATE_DIR));
312    }
313
314    helpers::git(None)
315        .arg("clone")
316        .arg("--depth=1")
317        .arg(toolstate_repo())
318        .arg(TOOLSTATE_DIR)
319        .run(builder);
320}
321
322/// Sets up config and authentication for modifying the toolstate repo.
323fn prepare_toolstate_config(builder: &Builder<'_>, token: &str) {
324    fn git_config(builder: &Builder<'_>, key: &str, value: &str) {
325        helpers::git(None).arg("config").arg("--global").arg(key).arg(value).run(builder);
326    }
327
328    // If changing anything here, then please check that `src/ci/publish_toolstate.sh` is up to date
329    // as well.
330    git_config(builder, "user.email", "7378925+rust-toolstate-update@users.noreply.github.com");
331    git_config(builder, "user.name", "Rust Toolstate Update");
332    git_config(builder, "credential.helper", "store");
333
334    let credential = format!("https://{token}:x-oauth-basic@github.com\n",);
335    let git_credential_path = PathBuf::from(t!(env::var("HOME"))).join(".git-credentials");
336    t!(fs::write(git_credential_path, credential));
337}
338
339/// Reads the latest toolstate from the toolstate repo.
340fn read_old_toolstate() -> Vec<RepoState> {
341    let latest_path = Path::new(TOOLSTATE_DIR).join("_data").join("latest.json");
342    let old_toolstate = t!(fs::read(latest_path));
343    t!(serde_json::from_slice(&old_toolstate))
344}
345
346/// This function `commit_toolstate_change` provides functionality for pushing a change
347/// to the `rust-toolstate` repository.
348///
349/// The function relies on a GitHub bot user, which should have a Personal access
350/// token defined in the environment variable $TOOLSTATE_REPO_ACCESS_TOKEN. If for
351/// some reason you need to change the token, please update the Azure Pipelines
352/// variable group.
353///
354///   1. Generate a new Personal access token:
355///
356///       * Login to the bot account, and go to Settings -> Developer settings ->
357///           Personal access tokens
358///       * Click "Generate new token"
359///       * Enable the "public_repo" permission, then click "Generate token"
360///       * Copy the generated token (should be a 40-digit hexadecimal number).
361///           Save it somewhere secure, as the token would be gone once you leave
362///           the page.
363///
364///   2. Update the variable group in Azure Pipelines
365///
366///       * Ping a member of the infrastructure team to do this.
367///
368///   4. Replace the email address below if the bot account identity is changed
369///
370///       * See <https://help.github.com/articles/about-commit-email-addresses/>
371///           if a private email by GitHub is wanted.
372fn commit_toolstate_change(builder: &Builder<'_>, current_toolstate: &ToolstateData) {
373    let message = format!("({} CI update)", OS.expect("linux/windows only"));
374    let mut success = false;
375    for _ in 1..=5 {
376        // Upload the test results (the new commit-to-toolstate mapping) to the toolstate repo.
377        // This does *not* change the "current toolstate"; that only happens post-landing
378        // via `src/ci/docker/publish_toolstate.sh`.
379        publish_test_results(builder, current_toolstate);
380
381        // `git commit` failing means nothing to commit.
382        let status = helpers::git(Some(Path::new(TOOLSTATE_DIR)))
383            .allow_failure()
384            .arg("commit")
385            .arg("-a")
386            .arg("-m")
387            .arg(&message)
388            .run(builder);
389        if !status {
390            success = true;
391            break;
392        }
393
394        let status = helpers::git(Some(Path::new(TOOLSTATE_DIR)))
395            .allow_failure()
396            .arg("push")
397            .arg("origin")
398            .arg("master")
399            .run(builder);
400        // If we successfully push, exit.
401        if status {
402            success = true;
403            break;
404        }
405        eprintln!("Sleeping for 3 seconds before retrying push");
406        std::thread::sleep(std::time::Duration::from_secs(3));
407        helpers::git(Some(Path::new(TOOLSTATE_DIR)))
408            .arg("fetch")
409            .arg("origin")
410            .arg("master")
411            .run(builder);
412        helpers::git(Some(Path::new(TOOLSTATE_DIR)))
413            .arg("reset")
414            .arg("--hard")
415            .arg("origin/master")
416            .run(builder);
417    }
418
419    if !success {
420        panic!("Failed to update toolstate repository with new data");
421    }
422}
423
424/// Updates the "history" files with the latest results.
425///
426/// These results will later be promoted to `latest.json` by the
427/// `publish_toolstate.py` script if the PR passes all tests and is merged to
428/// master.
429fn publish_test_results(builder: &Builder<'_>, current_toolstate: &ToolstateData) {
430    let commit = helpers::git(None).arg("rev-parse").arg("HEAD").run_capture(builder).stdout();
431
432    let toolstate_serialized = t!(serde_json::to_string(&current_toolstate));
433
434    let history_path = Path::new(TOOLSTATE_DIR)
435        .join("history")
436        .join(format!("{}.tsv", OS.expect("linux/windows only")));
437    let mut file = t!(fs::read_to_string(&history_path));
438    let end_of_first_line = file.find('\n').unwrap();
439    file.insert_str(end_of_first_line, &format!("\n{}\t{}", commit.trim(), toolstate_serialized));
440    t!(fs::write(&history_path, file));
441}
442
443#[derive(Debug, Deserialize)]
444struct RepoState {
445    tool: String,
446    windows: ToolState,
447    linux: ToolState,
448}
449
450impl RepoState {
451    fn state(&self) -> ToolState {
452        if cfg!(target_os = "linux") {
453            self.linux
454        } else if cfg!(windows) {
455            self.windows
456        } else {
457            unimplemented!()
458        }
459    }
460}