bootstrap/core/build_steps/
toolstate.rs1use std::collections::HashMap;
8use std::io::{Seek, SeekFrom};
9use std::path::{Path, PathBuf};
10use std::{env, fmt, fs, time};
11
12use serde_derive::{Deserialize, Serialize};
13
14use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
15use crate::utils::helpers::{self, t};
16
17const BETA_WEEK_START: u64 = 35;
19
20#[cfg(target_os = "linux")]
21const OS: Option<&str> = Some("linux");
22
23#[cfg(windows)]
24const OS: Option<&str> = Some("windows");
25
26#[cfg(all(not(target_os = "linux"), not(windows)))]
27const OS: Option<&str> = None;
28
29type ToolstateData = HashMap<Box<str>, ToolState>;
30
31#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, PartialOrd)]
32#[serde(rename_all = "kebab-case")]
33pub enum ToolState {
35 TestFail = 1,
37 TestPass = 2,
39 BuildFail = 0,
41}
42
43impl fmt::Display for ToolState {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 write!(
46 f,
47 "{}",
48 match self {
49 ToolState::TestFail => "test-fail",
50 ToolState::TestPass => "test-pass",
51 ToolState::BuildFail => "build-fail",
52 }
53 )
54 }
55}
56
57fn days_since_beta_promotion() -> u64 {
62 let since_epoch = t!(time::SystemTime::UNIX_EPOCH.elapsed());
63 (since_epoch.as_secs() / 86400 - 20) % 42
64}
65
66static 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];
77
78static NIGHTLY_TOOLS: &[(&str, &str)] = &[("embedded-book", "src/doc/embedded-book")];
84
85fn print_error(tool: &str, submodule: &str) {
86 eprintln!();
87 eprintln!("We detected that this PR updated '{tool}', but its tests failed.");
88 eprintln!();
89 eprintln!("If you do intend to update '{tool}', please check the error messages above and");
90 eprintln!("commit another update.");
91 eprintln!();
92 eprintln!("If you do NOT intend to update '{tool}', please ensure you did not accidentally");
93 eprintln!("change the submodule at '{submodule}'. You may ask your reviewer for the");
94 eprintln!("proper steps.");
95 crate::exit!(3);
96}
97
98fn check_changed_files(builder: &Builder<'_>, toolstates: &HashMap<Box<str>, ToolState>) {
99 let output = helpers::git(None)
101 .arg("diff")
102 .arg("--name-status")
103 .arg("HEAD")
104 .arg("HEAD^")
105 .run_capture(builder)
106 .stdout();
107
108 for (tool, submodule) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
109 let changed = output.lines().any(|l| l.starts_with('M') && l.ends_with(submodule));
110 eprintln!("Verifying status of {tool}...");
111 if !changed {
112 continue;
113 }
114
115 eprintln!("This PR updated '{submodule}', verifying if status is 'test-pass'...");
116 if toolstates[*tool] != ToolState::TestPass {
117 print_error(tool, submodule);
118 }
119 }
120}
121
122#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
123pub struct ToolStateCheck;
124
125impl Step for ToolStateCheck {
126 type Output = ();
127
128 fn run(self, builder: &Builder<'_>) {
154 if builder.config.dry_run() {
155 return;
156 }
157
158 let days_since_beta_promotion = days_since_beta_promotion();
159 let in_beta_week = days_since_beta_promotion >= BETA_WEEK_START;
160 let is_nightly = !(builder.config.channel == "beta" || builder.config.channel == "stable");
161 let toolstates = builder.toolstates();
162
163 let mut did_error = false;
164
165 for (tool, _) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
166 if !toolstates.contains_key(*tool) {
167 did_error = true;
168 eprintln!("ERROR: Tool `{tool}` was not recorded in tool state.");
169 }
170 }
171
172 if did_error {
173 crate::exit!(1);
174 }
175
176 check_changed_files(builder, &toolstates);
177 checkout_toolstate_repo(builder);
178 let old_toolstate = read_old_toolstate();
179
180 for (tool, _) in STABLE_TOOLS.iter() {
181 let state = toolstates[*tool];
182
183 if state != ToolState::TestPass {
184 if !is_nightly {
185 did_error = true;
186 eprintln!("ERROR: Tool `{tool}` should be test-pass but is {state}");
187 } else if in_beta_week {
188 let old_state = old_toolstate
189 .iter()
190 .find(|ts| ts.tool == *tool)
191 .expect("latest.json missing tool")
192 .state();
193 if state < old_state {
194 did_error = true;
195 eprintln!(
196 "ERROR: Tool `{tool}` has regressed from {old_state} to {state} during beta week."
197 );
198 } else {
199 eprintln!(
203 "WARNING: Tool `{tool}` is not test-pass (is `{state}`), \
204 this should be fixed before beta is branched."
205 );
206 }
207 }
208 }
214 }
215
216 if did_error {
217 crate::exit!(1);
218 }
219
220 if builder.config.channel == "nightly" && env::var_os("TOOLSTATE_PUBLISH").is_some() {
221 commit_toolstate_change(builder, &toolstates);
222 }
223 }
224
225 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
226 run.alias("check-tools")
227 }
228
229 fn make_run(run: RunConfig<'_>) {
230 run.builder.ensure(ToolStateCheck);
231 }
232}
233
234impl Builder<'_> {
235 fn toolstates(&self) -> HashMap<Box<str>, ToolState> {
236 if let Some(ref path) = self.config.save_toolstates {
237 if let Some(parent) = path.parent() {
238 t!(std::fs::create_dir_all(parent));
240 }
241 let mut file = t!(fs::OpenOptions::new()
242 .create(true)
243 .truncate(false)
244 .write(true)
245 .read(true)
246 .open(path));
247
248 serde_json::from_reader(&mut file).unwrap_or_default()
249 } else {
250 Default::default()
251 }
252 }
253
254 pub fn save_toolstate(&self, tool: &str, state: ToolState) {
260 use std::io::Write;
261
262 if self.config.dry_run() {
266 return;
267 }
268 if tool == "clippy-driver" || tool == "rustfmt" {
271 return;
272 }
273 if let Some(ref path) = self.config.save_toolstates {
274 if let Some(parent) = path.parent() {
275 t!(std::fs::create_dir_all(parent));
277 }
278 let mut file = t!(fs::OpenOptions::new()
279 .create(true)
280 .truncate(false)
281 .read(true)
282 .write(true)
283 .open(path));
284
285 let mut current_toolstates: HashMap<Box<str>, ToolState> =
286 serde_json::from_reader(&mut file).unwrap_or_default();
287 current_toolstates.insert(tool.into(), state);
288 t!(file.seek(SeekFrom::Start(0)));
289 t!(file.set_len(0));
290 t!(serde_json::to_writer(&file, ¤t_toolstates));
291 t!(writeln!(file)); }
293 }
294}
295
296fn 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
301const TOOLSTATE_DIR: &str = "rust-toolstate";
303
304fn checkout_toolstate_repo(builder: &Builder<'_>) {
306 if let Ok(token) = env::var("TOOLSTATE_REPO_ACCESS_TOKEN") {
307 prepare_toolstate_config(builder, &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 helpers::git(None)
315 .arg("clone")
316 .arg("--depth=1")
317 .arg(toolstate_repo())
318 .arg(TOOLSTATE_DIR)
319 .run(builder);
320}
321
322fn prepare_toolstate_config(builder: &Builder<'_>, token: &str) {
324 fn git_config(builder: &Builder<'_>, key: &str, value: &str) {
325 helpers::git(None).arg("config").arg("--global").arg(key).arg(value).run(builder);
326 }
327
328 git_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");
333
334 let credential = format!("https://{token}:x-oauth-basic@github.com\n",);
335 let git_credential_path = PathBuf::from(t!(env::var("HOME"))).join(".git-credentials");
336 t!(fs::write(git_credential_path, credential));
337}
338
339fn read_old_toolstate() -> Vec<RepoState> {
341 let latest_path = Path::new(TOOLSTATE_DIR).join("_data").join("latest.json");
342 let old_toolstate = t!(fs::read(latest_path));
343 t!(serde_json::from_slice(&old_toolstate))
344}
345
346fn commit_toolstate_change(builder: &Builder<'_>, current_toolstate: &ToolstateData) {
373 let message = format!("({} CI update)", OS.expect("linux/windows only"));
374 let mut success = false;
375 for _ in 1..=5 {
376 publish_test_results(builder, current_toolstate);
380
381 let 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);
389 if !status {
390 success = true;
391 break;
392 }
393
394 let 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 status {
402 success = true;
403 break;
404 }
405 eprintln!("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 }
418
419 if !success {
420 panic!("Failed to update toolstate repository with new data");
421 }
422}
423
424fn publish_test_results(builder: &Builder<'_>, current_toolstate: &ToolstateData) {
430 let commit = helpers::git(None).arg("rev-parse").arg("HEAD").run_capture(builder).stdout();
431
432 let toolstate_serialized = t!(serde_json::to_string(¤t_toolstate));
433
434 let history_path = Path::new(TOOLSTATE_DIR)
435 .join("history")
436 .join(format!("{}.tsv", OS.expect("linux/windows only")));
437 let mut file = t!(fs::read_to_string(&history_path));
438 let end_of_first_line = file.find('\n').unwrap();
439 file.insert_str(end_of_first_line, &format!("\n{}\t{}", commit.trim(), toolstate_serialized));
440 t!(fs::write(&history_path, file));
441}
442
443#[derive(Debug, Deserialize)]
444struct RepoState {
445 tool: String,
446 windows: ToolState,
447 linux: ToolState,
448}
449
450impl RepoState {
451 fn state(&self) -> ToolState {
452 if cfg!(target_os = "linux") {
453 self.linux
454 } else if cfg!(windows) {
455 self.windows
456 } else {
457 unimplemented!()
458 }
459 }
460}