Skip to content

Commit

Permalink
Give a better error message when the CI download fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Teapot4195 committed Jan 28, 2024
1 parent c485ee7 commit 8c8c7cf
Showing 1 changed file with 67 additions and 7 deletions.
74 changes: 67 additions & 7 deletions src/bootstrap/src/core/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
path::{Path, PathBuf},
process::{Command, Stdio},
sync::OnceLock,
time::SystemTime,
};

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

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

fn download_http_with_retries(&self, tempfile: &Path, url: &str, help_on_error: &str) {
fn download_http_with_retries(
&self,
tempfile: &Path,
url: &str,
help_on_error: &str,
commit: &String,
) {
println!("downloading {url}");
// Try curl. If that fails and we are on windows, fallback to PowerShell.
let mut curl = Command::new("curl");
Expand Down Expand Up @@ -250,19 +257,67 @@ impl Config {
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
url, tempfile.to_str().expect("invalid UTF-8 not supported with powershell downloads"),
),
])).is_err() {
])).is_ok() {
return;
}
eprintln!("\nspurious failure, trying again");
}
}
if !help_on_error.is_empty() {
eprintln!("{help_on_error}");
Self::check_outdated(commit);
}
crate::exit!(1);
}
}

fn check_outdated(commit: &String) {
let build_date: String = String::from_utf8(
Command::new("git")
.arg("show")
.arg("-s")
.arg("--format=%ct")
.arg(commit)
.output()
.unwrap()
.stdout,
)
.unwrap_or("".to_string());
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => {
let replaced = build_date.trim();
let diff = n.as_secs() - replaced.parse::<u64>().unwrap();
if diff >= 165 * 24 * 60 * 60 {
let build_date: String = String::from_utf8(
Command::new("git")
.arg("show")
.arg("-s")
.arg("--format=%ar")
.arg(commit)
.output()
.unwrap()
.stdout,
)
.unwrap_or("".to_string());
if build_date.is_empty() {
eprintln!(
"NOTE: tried to download builds for {}, CI builds will be gone in 168 days",
commit
);
} else {
eprintln!(
"NOTE: tried to download builds for {} (from {}), CI builds will be gone in 168 days",
commit, build_date
);
}
eprintln!("HELP: Consider updating your copy of the rust sources");
return;
}
}
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
}
}

fn unpack(&self, tarball: &Path, dst: &Path, pattern: &str) {
eprintln!("extracting {} to {}", tarball.display(), dst.display());
if !dst.exists() {
Expand Down Expand Up @@ -492,7 +547,7 @@ impl Config {
let extra_components = ["cargo"];

let download_beta_component = |config: &Config, filename, prefix: &_, date: &_| {
config.download_component(DownloadSource::Dist, filename, prefix, date, "stage0")
config.download_component(DownloadSource::Dist, filename, prefix, date, "stage0");
};

self.download_toolchain(
Expand Down Expand Up @@ -648,7 +703,7 @@ HELP: if trying to compile an old commit of rustc, disable `download-rustc` in c
download-rustc = false
";
}
self.download_file(&format!("{base_url}/{url}"), &tarball, help_on_error);
self.download_file(&format!("{base_url}/{url}"), &tarball, help_on_error, &key.to_string());
if let Some(sha256) = checksum {
if !self.verify(&tarball, sha256) {
panic!("failed to verify {}", tarball.display());
Expand Down Expand Up @@ -726,7 +781,12 @@ download-rustc = false
[llvm]
download-ci-llvm = false
";
self.download_file(&format!("{base}/{llvm_sha}/{filename}"), &tarball, help_on_error);
self.download_file(
&format!("{base}/{llvm_sha}/{filename}"),
&tarball,
help_on_error,
&llvm_sha.to_string(),
);
}
let llvm_root = self.ci_llvm_root();
self.unpack(&tarball, &llvm_root, "rust-dev");
Expand Down

0 comments on commit 8c8c7cf

Please sign in to comment.