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