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