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
67use 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, RunConfig, ShouldRun, Step};
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 neither
34pub enum ToolState {
35/// The tool compiles successfully, but the test suite fails
36TestFail = 1,
37/// The tool compiles successfully and its test suite passes
38TestPass = 2,
39/// The tool can't even be compiled
40BuildFail = 0,
41}
4243impl fmt::Display for ToolState {
44fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45write!(
46 f,
47"{}",
48match 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 {
62let since_epoch = t!(time::SystemTime::UNIX_EPOCH.elapsed());
63 (since_epoch.as_secs() / 86400 - 20) % 42
64}
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 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];
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 start
82// failing.
83static NIGHTLY_TOOLS: &[(&str, &str)] = &[("embedded-book", "src/doc/embedded-book")];
8485fn print_error(tool: &str, submodule: &str) {
86eprintln!();
87eprintln!("We detected that this PR updated '{tool}', but its tests failed.");
88eprintln!();
89eprintln!("If you do intend to update '{tool}', please check the error messages above and");
90eprintln!("commit another update.");
91eprintln!();
92eprintln!("If you do NOT intend to update '{tool}', please ensure you did not accidentally");
93eprintln!("change the submodule at '{submodule}'. You may ask your reviewer for the");
94eprintln!("proper steps.");
95crate::exit!(3);
96}
9798fn check_changed_files(builder: &Builder<'_>, toolstates: &HashMap<Box<str>, ToolState>) {
99// Changed files
100let output = helpers::git(None)
101 .arg("diff")
102 .arg("--name-status")
103 .arg("HEAD")
104 .arg("HEAD^")
105 .run_capture(builder)
106 .stdout();
107108for (tool, submodule) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
109let changed = output.lines().any(|l| l.starts_with('M') && l.ends_with(submodule));
110eprintln!("Verifying status of {tool}...");
111if !changed {
112continue;
113 }
114115eprintln!("This PR updated '{submodule}', verifying if status is 'test-pass'...");
116if toolstates[*tool] != ToolState::TestPass {
117 print_error(tool, submodule);
118 }
119 }
120}
121122#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
123pub struct ToolStateCheck;
124125impl Step for ToolStateCheck {
126type Output = ();
127128/// 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).
153fn run(self, builder: &Builder<'_>) {
154if builder.config.dry_run() {
155return;
156 }
157158let days_since_beta_promotion = days_since_beta_promotion();
159let in_beta_week = days_since_beta_promotion >= BETA_WEEK_START;
160let is_nightly = !(builder.config.channel == "beta" || builder.config.channel == "stable");
161let toolstates = builder.toolstates();
162163let mut did_error = false;
164165for (tool, _) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
166if !toolstates.contains_key(*tool) {
167 did_error = true;
168eprintln!("ERROR: Tool `{tool}` was not recorded in tool state.");
169 }
170 }
171172if did_error {
173crate::exit!(1);
174 }
175176 check_changed_files(builder, &toolstates);
177 checkout_toolstate_repo(builder);
178let old_toolstate = read_old_toolstate();
179180for (tool, _) in STABLE_TOOLS.iter() {
181let state = toolstates[*tool];
182183if state != ToolState::TestPass {
184if !is_nightly {
185 did_error = true;
186eprintln!("ERROR: Tool `{tool}` should be test-pass but is {state}");
187 } else if in_beta_week {
188let old_state = old_toolstate
189 .iter()
190 .find(|ts| ts.tool == *tool)
191 .expect("latest.json missing tool")
192 .state();
193if state < old_state {
194 did_error = true;
195eprintln!(
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.
202eprintln!(
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 }
215216if did_error {
217crate::exit!(1);
218 }
219220if builder.config.channel == "nightly" && env::var_os("TOOLSTATE_PUBLISH").is_some() {
221 commit_toolstate_change(builder, &toolstates);
222 }
223 }
224225fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
226 run.alias("check-tools")
227 }
228229fn make_run(run: RunConfig<'_>) {
230 run.builder.ensure(ToolStateCheck);
231 }
232}
233234impl Builder<'_> {
235fn toolstates(&self) -> HashMap<Box<str>, ToolState> {
236if let Some(ref path) = self.config.save_toolstates {
237if let Some(parent) = path.parent() {
238// Ensure the parent directory always exists
239t!(std::fs::create_dir_all(parent));
240 }
241let 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 key
257 /// `rust.save-toolstates` in `config.toml`. If unspecified, nothing will be
258 /// done. The file is updated immediately after this function completes.
259pub fn save_toolstate(&self, tool: &str, state: ToolState) {
260use std::io::Write;
261262// 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).
265if self.config.dry_run() {
266return;
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.
270if tool == "clippy-driver" || tool == "rustfmt" {
271return;
272 }
273if let Some(ref path) = self.config.save_toolstates {
274if let Some(parent) = path.parent() {
275// Ensure the parent directory always exists
276t!(std::fs::create_dir_all(parent));
277 }
278let mut file = t!(fs::OpenOptions::new()
279 .create(true)
280 .truncate(false)
281 .read(true)
282 .write(true)
283 .open(path));
284285let mut current_toolstates: HashMap<Box<str>, ToolState> =
286 serde_json::from_reader(&mut file).unwrap_or_default();
287 current_toolstates.insert(tool.into(), state);
288t!(file.seek(SeekFrom::Start(0)));
289t!(file.set_len(0));
290t!(serde_json::to_writer(&file, ¤t_toolstates));
291t!(writeln!(file)); // make sure this ends in a newline
292}
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<'_>) {
306if let Ok(token) = env::var("TOOLSTATE_REPO_ACCESS_TOKEN") {
307 prepare_toolstate_config(builder, &token);
308 }
309if Path::new(TOOLSTATE_DIR).exists() {
310eprintln!("Cleaning old toolstate directory...");
311t!(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) {
324fn 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 date
329 // as well.
330git_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");
333334let credential = format!("https://{token}:x-oauth-basic@github.com\n",);
335let git_credential_path = PathBuf::from(t!(env::var("HOME"))).join(".git-credentials");
336t!(fs::write(git_credential_path, credential));
337}
338339/// Reads the latest toolstate from the toolstate repo.
340fn read_old_toolstate() -> Vec<RepoState> {
341let latest_path = Path::new(TOOLSTATE_DIR).join("_data").join("latest.json");
342let old_toolstate = t!(fs::read(latest_path));
343t!(serde_json::from_slice(&old_toolstate))
344}
345346/// 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) {
373let message = format!("({} CI update)", OS.expect("linux/windows only"));
374let mut success = false;
375for _ 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`.
379publish_test_results(builder, current_toolstate);
380381// `git commit` failing means nothing to commit.
382let 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);
389if !status {
390 success = true;
391break;
392 }
393394let 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.
401if status {
402 success = true;
403break;
404 }
405eprintln!("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 }
418419if !success {
420panic!("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 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) {
430let commit = helpers::git(None).arg("rev-parse").arg("HEAD").run_capture(builder).stdout();
431432let toolstate_serialized = t!(serde_json::to_string(¤t_toolstate));
433434let history_path = Path::new(TOOLSTATE_DIR)
435 .join("history")
436 .join(format!("{}.tsv", OS.expect("linux/windows only")));
437let mut file = t!(fs::read_to_string(&history_path));
438let end_of_first_line = file.find('\n').unwrap();
439 file.insert_str(end_of_first_line, &format!("\n{}\t{}", commit.trim(), toolstate_serialized));
440t!(fs::write(&history_path, file));
441}
442443#[derive(Debug, Deserialize)]
444struct RepoState {
445 tool: String,
446 windows: ToolState,
447 linux: ToolState,
448}
449450impl RepoState {
451fn state(&self) -> ToolState {
452if cfg!(target_os = "linux") {
453self.linux
454 } else if cfg!(windows) {
455self.windows
456 } else {
457unimplemented!()
458 }
459 }
460}