src/bootstrap/src/core/build_steps/toolstate.rs RUST 461 lines View on github.com → Search inside
1//! [Toolstate] checks to keep tools building2//!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.html67use std::collections::HashMap;8use std::io::{Seek, SeekFrom};9use std::path::{Path, PathBuf};10use std::{env, fmt, fs, time};1112use serde_derive::{Deserialize, Serialize};1314use crate::core::builder::{Builder, CommandLineStep, RunConfig, ShouldRun};15use crate::utils::helpers::{self, t};1617// Each cycle is 42 days long (6 weeks); the last week is 35..=42 then.18const BETA_WEEK_START: u64 = 35;1920#[cfg(target_os = "linux")]21const OS: Option<&str> = Some("linux");2223#[cfg(windows)]24const OS: Option<&str> = Some("windows");2526#[cfg(all(not(target_os = "linux"), not(windows)))]27const OS: Option<&str> = None;2829type ToolstateData = HashMap<Box<str>, ToolState>;3031#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, PartialOrd)]32#[serde(rename_all = "kebab-case")]33/// Whether a tool can be compiled, tested or neither34pub enum ToolState {35    /// The tool compiles successfully, but the test suite fails36    TestFail = 1,37    /// The tool compiles successfully and its test suite passes38    TestPass = 2,39    /// The tool can't even be compiled40    BuildFail = 0,41}4243impl 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}5657/// 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) % 4264}6566// 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 not69// 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];7778// 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 start82// failing.83static NIGHTLY_TOOLS: &[(&str, &str)] = &[("embedded-book", "src/doc/embedded-book")];8485fn 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    helpers::exit_process(3);96}9798fn check_changed_files(builder: &Builder<'_>, toolstates: &HashMap<Box<str>, ToolState>) {99    // Changed files100    let output = helpers::git(None)101        .arg("diff")102        .arg("--name-status")103        .arg("HEAD")104        .arg("HEAD^")105        .run_capture(builder)106        .stdout();107108    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        }114115        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}121122#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]123pub struct ToolStateCheck;124125impl CommandLineStep for ToolStateCheck {126    type Output = ();127128    /// Checks tool state status.129    ///130    /// This is intended to be used in the `checktools.sh` script. To use131    /// this, set `save-toolstates` in `bootstrap.toml` so that tool status will132    /// be saved to a JSON file. Then, run `x.py test --no-fail-fast` for all133    /// of the tools to populate the JSON file. After that is done, this134    /// command can be run to check for any status failures, and exits with an135    /// error if there are any.136    ///137    /// This also handles publishing the results to the `history` directory of138    /// the toolstate repo <https://github.com/rust-lang-nursery/rust-toolstate>139    /// if the env var `TOOLSTATE_PUBLISH` is set. Note that there is a140    /// *separate* step of updating the `latest.json` file and creating GitHub141    /// issues and comments in `src/ci/publish_toolstate.sh`, which is only142    /// performed on master. (The shell/python code is intended to be migrated143    /// 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, see148    ///   <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" a151    ///   stable tool. That is, the status is not allowed to get worse152    ///   (test-pass to test-fail or build-fail).153    fn run(self, builder: &Builder<'_>) {154        if builder.config.dry_run() {155            return;156        }157158        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();162163        let mut did_error = false;164165        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        }171172        if did_error {173            helpers::exit_process(1);174        }175176        check_changed_files(builder, &toolstates);177        checkout_toolstate_repo(builder);178        let old_toolstate = read_old_toolstate();179180        for (tool, _) in STABLE_TOOLS.iter() {181            let state = toolstates[*tool];182183            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_toolstate189                        .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 most200                        // people won't read. It's mostly here for testing and201                        // 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 updating209                // `latest.json` and creating comments/issues warning people210                // if there is a regression. That all happens in a separate CI211                // job on the master branch once the PR has passed all tests212                // on the `auto` branch.213            }214        }215216        if did_error {217            helpers::exit_process(1);218        }219220        if builder.config.channel == "nightly" && env::var_os("TOOLSTATE_PUBLISH").is_some() {221            commit_toolstate_change(builder, &toolstates);222        }223    }224225    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {226        run.alias("check-tools")227    }228229    fn make_run(run: RunConfig<'_>) {230        run.builder.ensure(ToolStateCheck);231    }232}233234impl 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 exists239                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));247248            serde_json::from_reader(&mut file).unwrap_or_default()249        } else {250            Default::default()251        }252    }253254    /// Updates the actual toolstate of a tool.255    ///256    /// The toolstates are saved to the file specified by the key257    /// `rust.save-toolstates` in `bootstrap.toml`. If unspecified, nothing will be258    /// 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;261262        // If we're in a dry run setting we don't want to save toolstates as263        // that means if we e.g. panic down the line it'll look like we tested264        // 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 checking269        // 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 exists276                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));284285            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 newline292        }293    }294}295296fn toolstate_repo() -> String {297    env::var("TOOLSTATE_REPO")298        .unwrap_or_else(|_| "https://github.com/rust-lang-nursery/rust-toolstate.git".to_string())299}300301/// Directory where the toolstate repo is checked out.302const TOOLSTATE_DIR: &str = "rust-toolstate";303304/// 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    }313314    helpers::git(None)315        .arg("clone")316        .arg("--depth=1")317        .arg(toolstate_repo())318        .arg(TOOLSTATE_DIR)319        .run(builder);320}321322/// 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    }327328    // If changing anything here, then please check that `src/ci/publish_toolstate.sh` is up to date329    // 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");333334    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}338339/// 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}345346/// This function `commit_toolstate_change` provides functionality for pushing a change347/// to the `rust-toolstate` repository.348///349/// The function relies on a GitHub bot user, which should have a Personal access350/// token defined in the environment variable $TOOLSTATE_REPO_ACCESS_TOKEN. If for351/// some reason you need to change the token, please update the Azure Pipelines352/// 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 tokens358///       * 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 leave362///         the page.363///364///   2. Update the variable group in Azure Pipelines365///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 changed369///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-landing378        // via `src/ci/docker/publish_toolstate.sh`.379        publish_test_results(builder, current_toolstate);380381        // `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        }393394        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    }418419    if !success {420        panic!("Failed to update toolstate repository with new data");421    }422}423424/// Updates the "history" files with the latest results.425///426/// These results will later be promoted to `latest.json` by the427/// `publish_toolstate.py` script if the PR passes all tests and is merged to428/// 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();431432    let toolstate_serialized = t!(serde_json::to_string(&current_toolstate));433434    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}442443#[derive(Debug, Deserialize)]444struct RepoState {445    tool: String,446    windows: ToolState,447    linux: ToolState,448}449450impl RepoState {451    fn state(&self) -> ToolState {452        if cfg!(target_os = "linux") {453            self.linux454        } else if cfg!(windows) {455            self.windows456        } else {457            unimplemented!()458        }459    }460}

Code quality findings 28

Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
if toolstates[*tool] != ToolState::TestPass {
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
let state = toolstates[*tool];
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
.expect("latest.json missing tool")
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
let message = format!("({} CI update)", OS.expect("linux/windows only"));
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
.join(format!("{}.tsv", OS.expect("linux/windows only")));
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
let end_of_first_line = file.find('\n').unwrap();
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!();
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("We detected that this PR updated '{tool}', but its tests failed.");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!();
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("If you do intend to update '{tool}', please check the error messages above and");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("commit another update.");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!();
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("If you do NOT intend to update '{tool}', please ensure you did not accidentally");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("change the submodule at '{submodule}'. You may ask your reviewer for the");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("proper steps.");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("Verifying status of {tool}...");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("This PR updated '{submodule}', verifying if status is 'test-pass'...");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("ERROR: Tool `{tool}` was not recorded in tool state.");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("ERROR: Tool `{tool}` should be test-pass but is {state}");
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!(
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!(
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("Cleaning old toolstate directory...");
Info: This standard library function returns a Result. Ensure the Result is handled properly (e.g., using '?', match, if let) rather than potentially panicking with .unwrap() or .expect().
info correctness unhandled-result
t!(fs::write(git_credential_path, credential));
Info: This standard library function returns a Result. Ensure the Result is handled properly (e.g., using '?', match, if let) rather than potentially panicking with .unwrap() or .expect().
info correctness unhandled-result
let old_toolstate = t!(fs::read(latest_path));
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
eprintln!("Sleeping for 3 seconds before retrying push");
Info: This standard library function returns a Result. Ensure the Result is handled properly (e.g., using '?', match, if let) rather than potentially panicking with .unwrap() or .expect().
info correctness unhandled-result
let mut file = t!(fs::read_to_string(&history_path));
Info: This standard library function returns a Result. Ensure the Result is handled properly (e.g., using '?', match, if let) rather than potentially panicking with .unwrap() or .expect().
info correctness unhandled-result
t!(fs::write(&history_path, file));
Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
unimplemented!()

Security findings 1

Detected a password embedded in a service connection URI, which may expose direct access to the referenced service.
warning security generic-credential-uri medium confidence

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.

Generated by searchcode.com · powered by code_get_file + code_get_findings