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