Skip to content

Commit

Permalink
rustwide_builder: allow using a try build
Browse files Browse the repository at this point in the history
  • Loading branch information
jsha committed Nov 4, 2022
1 parent 1702933 commit df8a824
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export DOCSRS_PREFIX=ignored/cratesfyi-prefix
export DOCSRS_DATABASE_URL=postgresql://cratesfyi:password@localhost:15432
export DOCSRS_LOG=docs_rs=debug,rustwide=info
# To build with a PR that hasn't landed in a rust dist toolchain yet,
# you can set this to the git sha of a try build:
# https://forge.rust-lang.org/infra/docs/rustc-ci.html#try-builds
export DOCSRS_TOOLCHAIN=nightly
export AWS_ACCESS_KEY_ID=cratesfyi
export AWS_SECRET_ACCESS_KEY=secret_key
export S3_ENDPOINT=http://localhost:9000
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ schemamama = "0.3"
schemamama_postgres = "0.3"
systemstat = "0.2.0"
prometheus = { version = "0.13.0", default-features = false }
rustwide = "0.15.0"
rustwide = { version = "0.15.0", features = ["unstable-toolchain-ci"] }
mime_guess = "2"
zstd = "0.11.0"
hostname = "0.3.1"
Expand Down
32 changes: 30 additions & 2 deletions src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use anyhow::{anyhow, bail, Error};
use docsrs_metadata::{Metadata, DEFAULT_TARGETS, HOST_TARGET};
use failure::Error as FailureError;
use postgres::Client;
use regex::Regex;
use rustwide::cmd::{Command, CommandError, SandboxBuilder, SandboxImage};
use rustwide::logging::{self, LogStorage};
use rustwide::toolchain::ToolchainError;
Expand Down Expand Up @@ -73,7 +74,16 @@ impl RustwideBuilder {
.purge_all_build_dirs()
.map_err(FailureError::compat)?;

let toolchain = Toolchain::dist(&config.toolchain);
// If the toolchain is all hex, assume it references an artifact from
// CI, for instance an `@bors try` build.
let re = Regex::new(r"^[a-fA-F0-9]+$").unwrap();
let toolchain = if re.is_match(&config.toolchain) {
debug!("using CI build {}", &config.toolchain);
Toolchain::ci(&config.toolchain, false)
} else {
debug!("using toolchain {}", &config.toolchain);
Toolchain::dist(&config.toolchain)
};

Ok(RustwideBuilder {
workspace,
Expand Down Expand Up @@ -108,6 +118,23 @@ impl RustwideBuilder {
}

pub fn update_toolchain(&mut self) -> Result<bool> {
// For CI builds, a lot of the normal update_toolchain things don't apply.
// CI builds are only for one platform (https://forge.rust-lang.org/infra/docs/rustc-ci.html#try-builds)
// so we only try installing for the current platform. If that's not a match,
// for instance if we're running on macOS or Windows, this will error.
// Also, detecting the rustc version relies on calling rustc through rustup with the
// +channel argument, but the +channel argument doesn't work for CI builds. So
// we fake the rustc version and install from scratch every time since we can't detect
// the already-installed rustc version.
if let Some(ci) = self.toolchain.as_ci() {
self.toolchain
.install(&self.workspace)
.map_err(FailureError::compat)?;
self.rustc_version = format!("rustc 1.9999.0-nightly ({} 2999-12-29)", ci.sha());
self.add_essential_files()?;
return Ok(true);
}

// Ignore errors if detection fails.
let old_version = self.detect_rustc_version().ok();

Expand Down Expand Up @@ -174,6 +201,8 @@ impl RustwideBuilder {
Ok(has_changed)
}

/// Return a string containing the output of `rustc --version`. Only valid
// for dist toolchains. Will error if run with a CI toolchain.
fn detect_rustc_version(&self) -> Result<String> {
info!("detecting rustc's version...");
let res = Command::new(&self.workspace, self.toolchain.rustc())
Expand All @@ -190,7 +219,6 @@ impl RustwideBuilder {
}

pub fn add_essential_files(&mut self) -> Result<()> {
self.rustc_version = self.detect_rustc_version()?;
let rustc_version = parse_rustc_version(&self.rustc_version)?;

info!("building a dummy crate to get essential files");
Expand Down
2 changes: 1 addition & 1 deletion src/utils/rustc_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn parse_rustc_version<S: AsRef<str>>(version: S) -> Result<String> {
let version_regex = Regex::new(r" ([\w.-]+) \((\w+) (\d+)-(\d+)-(\d+)\)")?;
let captures = version_regex
.captures(version.as_ref())
.with_context(|| anyhow!("Failed to parse rustc version"))?;
.with_context(|| anyhow!("Failed to parse rustc version '{}'", version.as_ref()))?;

Ok(format!(
"{}{}{}-{}-{}",
Expand Down

0 comments on commit df8a824

Please sign in to comment.