Skip to content

Commit 7358d65

Browse files
committed
Give a better error message when the CI download fails
1 parent c485ee7 commit 7358d65

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

src/bootstrap/src/core/download.rs

+70-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
path::{Path, PathBuf},
77
process::{Command, Stdio},
88
sync::OnceLock,
9+
time::SystemTime,
910
};
1011

1112
use build_helper::ci::CiEnv;
@@ -194,7 +195,7 @@ impl Config {
194195
let _ = try_run(self, patchelf.arg(fname));
195196
}
196197

197-
fn download_file(&self, url: &str, dest_path: &Path, help_on_error: &str) {
198+
fn download_file(&self, url: &str, dest_path: &Path, help_on_error: &str, commit: &String) {
198199
self.verbose(&format!("download {url}"));
199200
// Use a temporary file in case we crash while downloading, to avoid a corrupt download in cache/.
200201
let tempfile = self.tempdir().join(dest_path.file_name().unwrap());
@@ -203,7 +204,7 @@ impl Config {
203204
// protocols without worrying about merge conflicts if we change the HTTP implementation.
204205
match url.split_once("://").map(|(proto, _)| proto) {
205206
Some("http") | Some("https") => {
206-
self.download_http_with_retries(&tempfile, url, help_on_error)
207+
self.download_http_with_retries(&tempfile, url, help_on_error, commit)
207208
}
208209
Some(other) => panic!("unsupported protocol {other} in {url}"),
209210
None => panic!("no protocol in {url}"),
@@ -214,7 +215,13 @@ impl Config {
214215
);
215216
}
216217

217-
fn download_http_with_retries(&self, tempfile: &Path, url: &str, help_on_error: &str) {
218+
fn download_http_with_retries(
219+
&self,
220+
tempfile: &Path,
221+
url: &str,
222+
help_on_error: &str,
223+
commit: &String,
224+
) {
218225
println!("downloading {url}");
219226
// Try curl. If that fails and we are on windows, fallback to PowerShell.
220227
let mut curl = Command::new("curl");
@@ -250,19 +257,70 @@ impl Config {
250257
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
251258
url, tempfile.to_str().expect("invalid UTF-8 not supported with powershell downloads"),
252259
),
253-
])).is_err() {
260+
])).is_ok() {
254261
return;
255262
}
256263
eprintln!("\nspurious failure, trying again");
257264
}
258265
}
259266
if !help_on_error.is_empty() {
260267
eprintln!("{help_on_error}");
268+
Self::check_outdated(commit);
261269
}
262270
crate::exit!(1);
263271
}
264272
}
265273

274+
fn check_outdated(commit: &String) {
275+
let last_commit: String = String::from_utf8(
276+
Command::new("git")
277+
.arg("show")
278+
.arg("-s")
279+
.arg("--date=short")
280+
.arg("--format=%ct")
281+
.arg("HEAD")
282+
.output()
283+
.expect("Failed to get git log")
284+
.stdout,
285+
)
286+
.unwrap_or("".to_string());
287+
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
288+
Ok(n) => {
289+
let replaced = last_commit.replace("\n", "");
290+
let diff = n.as_secs() - replaced.parse::<u64>().unwrap();
291+
if diff >= 165 * 24 * 60 * 60 {
292+
// HEAD is more than 165 days out of date
293+
eprintln!(
294+
"NOTE: HEAD is {} days out of date, CI builds disappear in 168 days.",
295+
diff / (24 * 60 * 60)
296+
);
297+
let build_date: String = String::from_utf8(
298+
Command::new("git")
299+
.arg("show")
300+
.arg("-s")
301+
.arg("--format=%ar")
302+
.arg(commit)
303+
.output()
304+
.unwrap()
305+
.stdout,
306+
)
307+
.unwrap_or("".to_string());
308+
if build_date.is_empty() {
309+
eprintln!("NOTE: tried to download builds for {}", commit);
310+
} else {
311+
eprintln!(
312+
"NOTE: tried to download builds for {} (from {})",
313+
commit, build_date
314+
);
315+
}
316+
eprintln!("HELP: Consider updating your copy of the rust sources");
317+
return;
318+
}
319+
}
320+
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
321+
}
322+
}
323+
266324
fn unpack(&self, tarball: &Path, dst: &Path, pattern: &str) {
267325
eprintln!("extracting {} to {}", tarball.display(), dst.display());
268326
if !dst.exists() {
@@ -492,7 +550,7 @@ impl Config {
492550
let extra_components = ["cargo"];
493551

494552
let download_beta_component = |config: &Config, filename, prefix: &_, date: &_| {
495-
config.download_component(DownloadSource::Dist, filename, prefix, date, "stage0")
553+
config.download_component(DownloadSource::Dist, filename, prefix, date, "stage0");
496554
};
497555

498556
self.download_toolchain(
@@ -648,7 +706,7 @@ HELP: if trying to compile an old commit of rustc, disable `download-rustc` in c
648706
download-rustc = false
649707
";
650708
}
651-
self.download_file(&format!("{base_url}/{url}"), &tarball, help_on_error);
709+
self.download_file(&format!("{base_url}/{url}"), &tarball, help_on_error, &key.to_string());
652710
if let Some(sha256) = checksum {
653711
if !self.verify(&tarball, sha256) {
654712
panic!("failed to verify {}", tarball.display());
@@ -726,7 +784,12 @@ download-rustc = false
726784
[llvm]
727785
download-ci-llvm = false
728786
";
729-
self.download_file(&format!("{base}/{llvm_sha}/{filename}"), &tarball, help_on_error);
787+
self.download_file(
788+
&format!("{base}/{llvm_sha}/{filename}"),
789+
&tarball,
790+
help_on_error,
791+
&llvm_sha.to_string(),
792+
);
730793
}
731794
let llvm_root = self.ci_llvm_root();
732795
self.unpack(&tarball, &llvm_root, "rust-dev");

0 commit comments

Comments
 (0)