]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/toolstate.rs
Rollup merge of #68232 - Mark-Simulacrum:unicode-tables, r=joshtriplett
[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::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)]
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     ("clippy-driver", "src/tools/clippy"),
82 ];
83
84 // These tools are permitted to not build on the beta/stable channels.
85 //
86 // We do require that we checked whether they build or not on the tools builder,
87 // though, as otherwise we will be unable to file an issue if they start
88 // failing.
89 static NIGHTLY_TOOLS: &[(&str, &str)] = &[
90     ("miri", "src/tools/miri"),
91     ("embedded-book", "src/doc/embedded-book"),
92     ("rustc-guide", "src/doc/rustc-guide"),
93 ];
94
95 fn print_error(tool: &str, submodule: &str) {
96     eprintln!("");
97     eprintln!("We detected that this PR updated '{}', but its tests failed.", tool);
98     eprintln!("");
99     eprintln!("If you do intend to update '{}', please check the error messages above and", tool);
100     eprintln!("commit another update.");
101     eprintln!("");
102     eprintln!("If you do NOT intend to update '{}', please ensure you did not accidentally", tool);
103     eprintln!("change the submodule at '{}'. You may ask your reviewer for the", submodule);
104     eprintln!("proper steps.");
105     std::process::exit(3);
106 }
107
108 fn check_changed_files(toolstates: &HashMap<Box<str>, ToolState>) {
109     // Changed files
110     let output = std::process::Command::new("git")
111         .arg("diff")
112         .arg("--name-status")
113         .arg("HEAD")
114         .arg("HEAD^")
115         .output();
116     let output = match output {
117         Ok(o) => o,
118         Err(e) => {
119             eprintln!("Failed to get changed files: {:?}", e);
120             std::process::exit(1);
121         }
122     };
123
124     let output = t!(String::from_utf8(output.stdout));
125
126     for (tool, submodule) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
127         let changed = output.lines().any(|l| l.starts_with("M") && l.ends_with(submodule));
128         eprintln!("Verifying status of {}...", tool);
129         if !changed {
130             continue;
131         }
132
133         eprintln!("This PR updated '{}', verifying if status is 'test-pass'...", submodule);
134         if toolstates[*tool] != ToolState::TestPass {
135             print_error(tool, submodule);
136         }
137     }
138 }
139
140 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
141 pub struct ToolStateCheck;
142
143 impl Step for ToolStateCheck {
144     type Output = ();
145
146     /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
147     ///
148     /// This tool in `src/tools` will verify the validity of all our links in the
149     /// documentation to ensure we don't have a bunch of dead ones.
150     fn run(self, builder: &Builder<'_>) {
151         if builder.config.dry_run {
152             return;
153         }
154
155         let days_since_beta_promotion = days_since_beta_promotion();
156         let in_beta_week = days_since_beta_promotion >= BETA_WEEK_START;
157         let is_nightly = !(builder.config.channel == "beta" || builder.config.channel == "stable");
158         let toolstates = builder.toolstates();
159
160         let mut did_error = false;
161
162         for (tool, _) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
163             if !toolstates.contains_key(*tool) {
164                 did_error = true;
165                 eprintln!("error: Tool `{}` was not recorded in tool state.", tool);
166             }
167         }
168
169         if did_error {
170             std::process::exit(1);
171         }
172
173         check_changed_files(&toolstates);
174
175         for (tool, _) in STABLE_TOOLS.iter() {
176             let state = toolstates[*tool];
177
178             if state != ToolState::TestPass {
179                 if !is_nightly {
180                     did_error = true;
181                     eprintln!("error: Tool `{}` should be test-pass but is {}", tool, state);
182                 } else if in_beta_week {
183                     did_error = true;
184                     eprintln!(
185                         "error: Tool `{}` should be test-pass but is {} during beta week.",
186                         tool, state
187                     );
188                 }
189             }
190         }
191
192         if did_error {
193             std::process::exit(1);
194         }
195
196         if builder.config.channel == "nightly" && env::var_os("TOOLSTATE_PUBLISH").is_some() {
197             commit_toolstate_change(&toolstates, in_beta_week);
198         }
199     }
200
201     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
202         run.path("check-tools")
203     }
204
205     fn make_run(run: RunConfig<'_>) {
206         run.builder.ensure(ToolStateCheck);
207     }
208 }
209
210 impl Builder<'_> {
211     fn toolstates(&self) -> HashMap<Box<str>, ToolState> {
212         if let Some(ref path) = self.config.save_toolstates {
213             if let Some(parent) = path.parent() {
214                 // Ensure the parent directory always exists
215                 t!(std::fs::create_dir_all(parent));
216             }
217             let mut file =
218                 t!(fs::OpenOptions::new().create(true).write(true).read(true).open(path));
219
220             serde_json::from_reader(&mut file).unwrap_or_default()
221         } else {
222             Default::default()
223         }
224     }
225
226     /// Updates the actual toolstate of a tool.
227     ///
228     /// The toolstates are saved to the file specified by the key
229     /// `rust.save-toolstates` in `config.toml`. If unspecified, nothing will be
230     /// done. The file is updated immediately after this function completes.
231     pub fn save_toolstate(&self, tool: &str, state: ToolState) {
232         if let Some(ref path) = self.config.save_toolstates {
233             if let Some(parent) = path.parent() {
234                 // Ensure the parent directory always exists
235                 t!(std::fs::create_dir_all(parent));
236             }
237             let mut file =
238                 t!(fs::OpenOptions::new().create(true).read(true).write(true).open(path));
239
240             let mut current_toolstates: HashMap<Box<str>, ToolState> =
241                 serde_json::from_reader(&mut file).unwrap_or_default();
242             current_toolstates.insert(tool.into(), state);
243             t!(file.seek(SeekFrom::Start(0)));
244             t!(file.set_len(0));
245             t!(serde_json::to_writer(file, &current_toolstates));
246         }
247     }
248 }
249
250 /// This function `commit_toolstate_change` provides functionality for pushing a change
251 /// to the `rust-toolstate` repository.
252 ///
253 /// The function relies on a GitHub bot user, which should have a Personal access
254 /// token defined in the environment variable $TOOLSTATE_REPO_ACCESS_TOKEN. If for
255 /// some reason you need to change the token, please update the Azure Pipelines
256 /// variable group.
257 ///
258 ///   1. Generate a new Personal access token:
259 ///
260 ///       * Login to the bot account, and go to Settings -> Developer settings ->
261 ///           Personal access tokens
262 ///       * Click "Generate new token"
263 ///       * Enable the "public_repo" permission, then click "Generate token"
264 ///       * Copy the generated token (should be a 40-digit hexadecimal number).
265 ///           Save it somewhere secure, as the token would be gone once you leave
266 ///           the page.
267 ///
268 ///   2. Update the variable group in Azure Pipelines
269 ///
270 ///       * Ping a member of the infrastructure team to do this.
271 ///
272 ///   4. Replace the email address below if the bot account identity is changed
273 ///
274 ///       * See <https://help.github.com/articles/about-commit-email-addresses/>
275 ///           if a private email by GitHub is wanted.
276 fn commit_toolstate_change(current_toolstate: &ToolstateData, in_beta_week: bool) {
277     fn git_config(key: &str, value: &str) {
278         let status = Command::new("git").arg("config").arg("--global").arg(key).arg(value).status();
279         let success = match status {
280             Ok(s) => s.success(),
281             Err(_) => false,
282         };
283         if !success {
284             panic!("git config key={} value={} successful (status: {:?})", key, value, status);
285         }
286     }
287
288     // If changing anything here, then please check that src/ci/publish_toolstate.sh is up to date
289     // as well.
290     git_config("user.email", "7378925+rust-toolstate-update@users.noreply.github.com");
291     git_config("user.name", "Rust Toolstate Update");
292     git_config("credential.helper", "store");
293
294     let credential = format!(
295         "https://{}:x-oauth-basic@github.com\n",
296         t!(env::var("TOOLSTATE_REPO_ACCESS_TOKEN")),
297     );
298     let git_credential_path = PathBuf::from(t!(env::var("HOME"))).join(".git-credentials");
299     t!(fs::write(&git_credential_path, credential));
300
301     let status = Command::new("git")
302         .arg("clone")
303         .arg("--depth=1")
304         .arg(t!(env::var("TOOLSTATE_REPO")))
305         .status();
306     let success = match status {
307         Ok(s) => s.success(),
308         Err(_) => false,
309     };
310     if !success {
311         panic!("git clone successful (status: {:?})", status);
312     }
313
314     let old_toolstate = t!(fs::read("rust-toolstate/_data/latest.json"));
315     let old_toolstate: Vec<RepoState> = t!(serde_json::from_slice(&old_toolstate));
316
317     let message = format!("({} CI update)", OS.expect("linux/windows only"));
318     let mut success = false;
319     for _ in 1..=5 {
320         // Update the toolstate results (the new commit-to-toolstate mapping) in the toolstate repo.
321         change_toolstate(&current_toolstate, &old_toolstate, in_beta_week);
322
323         // `git commit` failing means nothing to commit.
324         let status = t!(Command::new("git")
325             .current_dir("rust-toolstate")
326             .arg("commit")
327             .arg("-a")
328             .arg("-m")
329             .arg(&message)
330             .status());
331         if !status.success() {
332             success = true;
333             break;
334         }
335
336         let status = t!(Command::new("git")
337             .current_dir("rust-toolstate")
338             .arg("push")
339             .arg("origin")
340             .arg("master")
341             .status());
342         // If we successfully push, exit.
343         if status.success() {
344             success = true;
345             break;
346         }
347         eprintln!("Sleeping for 3 seconds before retrying push");
348         std::thread::sleep(std::time::Duration::from_secs(3));
349         let status = t!(Command::new("git")
350             .current_dir("rust-toolstate")
351             .arg("fetch")
352             .arg("origin")
353             .arg("master")
354             .status());
355         assert!(status.success());
356         let status = t!(Command::new("git")
357             .current_dir("rust-toolstate")
358             .arg("reset")
359             .arg("--hard")
360             .arg("origin/master")
361             .status());
362         assert!(status.success());
363     }
364
365     if !success {
366         panic!("Failed to update toolstate repository with new data");
367     }
368 }
369
370 fn change_toolstate(
371     current_toolstate: &ToolstateData,
372     old_toolstate: &[RepoState],
373     in_beta_week: bool,
374 ) {
375     let mut regressed = false;
376     for repo_state in old_toolstate {
377         let tool = &repo_state.tool;
378         let state = if cfg!(target_os = "linux") {
379             &repo_state.linux
380         } else if cfg!(windows) {
381             &repo_state.windows
382         } else {
383             unimplemented!()
384         };
385         let new_state = current_toolstate[tool.as_str()];
386
387         if new_state != *state {
388             eprintln!("The state of `{}` has changed from `{}` to `{}`", tool, state, new_state);
389             if (new_state as u8) < (*state as u8) {
390                 if !["rustc-guide", "miri", "embedded-book"].contains(&tool.as_str()) {
391                     regressed = true;
392                 }
393             }
394         }
395     }
396
397     if regressed && in_beta_week {
398         std::process::exit(1);
399     }
400
401     let commit = t!(std::process::Command::new("git").arg("rev-parse").arg("HEAD").output());
402     let commit = t!(String::from_utf8(commit.stdout));
403
404     let toolstate_serialized = t!(serde_json::to_string(&current_toolstate));
405
406     let history_path = format!("rust-toolstate/history/{}.tsv", OS.expect("linux/windows only"));
407     let mut file = t!(fs::read_to_string(&history_path));
408     let end_of_first_line = file.find('\n').unwrap();
409     file.insert_str(end_of_first_line, &format!("\n{}\t{}", commit.trim(), toolstate_serialized));
410     t!(fs::write(&history_path, file));
411 }
412
413 #[derive(Debug, Serialize, Deserialize)]
414 struct RepoState {
415     tool: String,
416     windows: ToolState,
417     linux: ToolState,
418     commit: String,
419     datetime: String,
420 }