Skip to content

Commit

Permalink
Merge branch 'main' into middleware-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMathWalker committed Jan 11, 2024
2 parents cd6415d + 99eae0a commit 02c0413
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust stable toolchain
uses: actions-rust-lang/[email protected]
with:
rustflags: ""
- name: Install Rust nightly toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: nightly
components: rust-docs-json
rustflags: ""
- name: Install Rust stable toolchain
uses: actions-rust-lang/[email protected]
with:
rustflags: ""
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
with:
Expand Down
16 changes: 10 additions & 6 deletions libs/pavexc/src/rustdoc/compute/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,22 +680,26 @@ impl<'a> ThirdPartyCrateCacheKey<'a> {
}
}

/// Return the output of `cargo +nightly --verbose --version`,
/// which can be used to fingerprint the `cargo` toolchain used by Pavex.
/// Return the output of `cargo --verbose --version` for the nightly toolchain,
/// which can be used to fingerprint the toolchain used by Pavex.
pub fn cargo_fingerprint() -> Result<String, anyhow::Error> {
let err_msg = || {
"Failed to run `cargo +nightly --verbose --version`.\n
"Failed to run `cargo --verbose --version` on `nightly`.\n
Is the `nightly` toolchain installed?\n
If not, invoke `rustup toolchain install nightly` to fix it."
};
let mut cmd = std::process::Command::new("cargo");
cmd.arg("+nightly").arg("--verbose").arg("--version");
let mut cmd = std::process::Command::new("rustup");
cmd.arg("run")
.arg("nightly")
.arg("cargo")
.arg("--verbose")
.arg("--version");
let output = cmd.output().with_context(err_msg)?;
if !output.status.success() {
anyhow::bail!(err_msg());
}
let output = String::from_utf8(output.stdout).with_context(|| {
"An invocation of `cargo +nightly --verbose --version` returned non-UTF8 data as output."
"An invocation of `cargo --verbose --version` for the nightly toolchain returned non-UTF8 data as output."
})?;
Ok(output)
}
6 changes: 4 additions & 2 deletions libs/pavexc/src/rustdoc/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ where
{
// TODO: check that we have the nightly toolchain available beforehand in order to return
// a good error.
let mut cmd = std::process::Command::new("cargo");
cmd.arg("+nightly")
let mut cmd = std::process::Command::new("rustup");
cmd.arg("run")
.arg("nightly")
.arg("cargo")
.arg("doc")
.arg("--no-deps")
.arg("-q")
Expand Down
65 changes: 39 additions & 26 deletions libs/pavexc/src/rustdoc/compute/toolchain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Context;
use std::path::{Path, PathBuf};
use once_cell::sync::OnceCell;
use std::path::PathBuf;

#[tracing::instrument(
skip_all,
Expand Down Expand Up @@ -29,31 +30,43 @@ fn get_json_docs_root_folder_via_rustup() -> Result<PathBuf, anyhow::Error> {
/// `<toolchain root folder>/bin/cargo`. Therefore we compute `<toolchain root folder>` by chopping
/// off the final two components of the path returned by `rustup`.
fn get_nightly_toolchain_root_folder_via_rustup() -> Result<PathBuf, anyhow::Error> {
let mut cmd = std::process::Command::new("rustup");
cmd.arg("which")
.arg("--toolchain")
.arg("nightly")
.arg("cargo");

let output = cmd.output().with_context(|| {
format!("Failed to run a `rustup` command. Is `rustup` installed?\n{cmd:?}")
})?;

if !output.status.success() {
anyhow::bail!(
"An invocation of `rustup` exited with non-zero status code.\n{:?}",
cmd
);
}
let path = std::str::from_utf8(&output.stdout)
.with_context(|| {
format!("An invocation of `rustup` returned non-UTF8 data as output.\n{cmd:?}")
})?
.trim();
let path = Path::new(path);
let cargo_path = get_nightly_cargo_via_rustup()?;
debug_assert!(
path.ends_with("bin/cargo"),
"The path to the `cargo` binary for nightly doesn't have the expected structure: {path:?}"
cargo_path.ends_with("bin/cargo"),
"The path to the `cargo` binary for nightly doesn't have the expected structure: {cargo_path:?}"
);
Ok(path.parent().unwrap().parent().unwrap().to_path_buf())
Ok(cargo_path.parent().unwrap().parent().unwrap().to_path_buf())
}

static NIGHTLY_CARGO: OnceCell<PathBuf> = OnceCell::new();

pub(super) fn get_nightly_cargo_via_rustup() -> Result<PathBuf, anyhow::Error> {
fn compute_nightly_cargo_via_rustup() -> Result<PathBuf, anyhow::Error> {
let mut cmd = std::process::Command::new("rustup");
cmd.arg("which")
.arg("--toolchain")
.arg("nightly")
.arg("cargo");

let output = cmd.output().with_context(|| {
format!("Failed to run a `rustup` command. Is `rustup` installed?\n{cmd:?}")
})?;

if !output.status.success() {
anyhow::bail!(
"An invocation of `rustup` exited with non-zero status code.\n{:?}",
cmd
);
}
let path = std::str::from_utf8(&output.stdout)
.with_context(|| {
format!("An invocation of `rustup` returned non-UTF8 data as output.\n{cmd:?}")
})?
.trim();
Ok(PathBuf::from(path))
}

NIGHTLY_CARGO
.get_or_try_init(|| compute_nightly_cargo_via_rustup())
.map(ToOwned::to_owned)
}

0 comments on commit 02c0413

Please sign in to comment.