Skip to content

Commit

Permalink
Fetch dependencies for -Zbuild-std before entering the sandbox
Browse files Browse the repository at this point in the history
This allows running `doc -Zbuild-std` from within the sandbox.
Previously, it would error out because cargo tried to download the
standard library's dependencies:

```
[2021-11-27T19:57:24Z INFO  rustwide::cmd] running `Command { std: "docker" "create" "-v" "/home/joshua/src/rust/docs.rs/.workspace/builds/gba-0.5.2/target:/opt/rustwide/target:rw,Z" "-v" "/home/joshua/src/rust/docs.rs/.workspace/builds/gba-0.5.2/source:/opt/rustwide/workdir:ro,Z" "-v" "/home/joshua/src/rust/docs.rs/.workspace/cargo-home:/opt/rustwide/cargo-home:ro,Z" "-v" "/home/joshua/src/rust/docs.rs/.workspace/rustup-home:/opt/rustwide/rustup-home:ro,Z" "-e" "SOURCE_DIR=/opt/rustwide/workdir" "-e" "CARGO_TARGET_DIR=/opt/rustwide/target" "-e" "DOCS_RS=1" "-e" "CARGO_HOME=/opt/rustwide/cargo-home" "-e" "RUSTUP_HOME=/opt/rustwide/rustup-home" "-w" "/opt/rustwide/workdir" "-m" "3221225472" "--user" "1000:1000" "--network" "none" "ghcr.io/rust-lang/crates-build-env/linux-micro" "/opt/rustwide/cargo-home/bin/cargo" "+nightly" "rustdoc" "--lib" "-Zrustdoc-map" "-Z" "unstable-options" "--config" "build.rustdocflags=[\"--cfg\", \"docs_rs\", \"-Z\", \"unstable-options\", \"--emit=invocation-specific\", \"--resource-suffix\", \"-20211126-1.58.0-nightly-6d246f0c8\", \"--static-root-path\", \"/\", \"--cap-lints\", \"warn\", \"--disable-per-crate-search\"]" "-Zunstable-options" "--config=doc.extern-map.registries.crates-io=\"https://docs.rs/{pkg_name}/{version}/thumbv4t-none-eabi\"" "-Zbuild-std" "--target" "thumbv4t-none-eabi", kill_on_drop: false }`
[2021-11-27T19:57:24Z INFO  rustwide::cmd] [stdout] fe2773f8a17ab13ce7b66c7221f91883a7b92b3bdeef7b30bf2ed55aa6b3d511
[2021-11-27T19:57:24Z INFO  rustwide::cmd] running `Command { std: "docker" "start" "-a" "fe2773f8a17ab13ce7b66c7221f91883a7b92b3bdeef7b30bf2ed55aa6b3d511", kill_on_drop: false }`
[2021-11-27T19:57:24Z INFO  rustwide::cmd] [stderr]  Downloading crates ...
[2021-11-27T19:57:24Z INFO  rustwide::cmd] [stderr] warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates.io)
[2021-11-27T19:57:24Z INFO  rustwide::cmd] [stderr] error: failed to download from `https://crates.io/api/v1/crates/libc/0.2.106/download`
[2021-11-27T19:57:24Z INFO  rustwide::cmd] [stderr]
[2021-11-27T19:57:24Z INFO  rustwide::cmd] [stderr] Caused by:
[2021-11-27T19:57:24Z INFO  rustwide::cmd] [stderr]   [6] Couldn't resolve host name (Could not resolve host: crates.io)
```
  • Loading branch information
jyn514 committed Apr 14, 2023
1 parent c2f235b commit 399d37c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- New method `Build::fetch_build_std_dependencies` for using `-Zbuild-std` within the sandbox when
networking is disabled. Previously, this would try to fetch the standard library sources, which
would error when networking was blocked.

## [0.15.2] - 2022-11-08

### Changed
Expand Down
16 changes: 16 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,20 @@ impl<'ws> Build<'ws> {
pub fn host_target_dir(&self) -> PathBuf {
self.dir.target_dir()
}

/// Pre-fetching the dependencies for `-Z build-std` outside the sandbox.
///
/// When this function is called, it is possible to use `-Zbuild-std` inside
/// the sandbox to build the standard library from source even when
/// networking is disabled.
#[cfg(any(feature = "unstable", doc))]
#[cfg_attr(docs_rs, doc(cfg(feature = "unstable")))]
pub fn fetch_build_std_dependencies(&self, targets: &[&str]) -> Result<(), Error> {
crate::prepare::fetch_deps(
&self.dir.workspace,
self.toolchain,
&self.host_source_dir(),
targets,
)
}
}
49 changes: 34 additions & 15 deletions src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,40 @@ impl<'a> Prepare<'a> {
}

fn fetch_deps(&mut self) -> Result<(), Error> {
let mut missing_deps = false;
let res = Command::new(self.workspace, self.toolchain.cargo())
.args(&["fetch", "--manifest-path", "Cargo.toml"])
.cd(self.source_dir)
.process_lines(&mut |line, _| {
if line.contains("failed to load source for dependency") {
missing_deps = true;
}
})
.run();
match res {
Ok(_) => Ok(()),
Err(_) if missing_deps => Err(PrepareError::MissingDependencies.into()),
err => err.map_err(|e| e.into()),
}
fetch_deps(self.workspace, self.toolchain, self.source_dir, &[])
}
}

pub(crate) fn fetch_deps(
workspace: &Workspace,
toolchain: &Toolchain,
source_dir: &Path,
fetch_build_std_targets: &[&str],
) -> Result<(), Error> {
let mut missing_deps = false;
let mut cmd = Command::new(workspace, toolchain.cargo())
.args(&["fetch", "--manifest-path", "Cargo.toml"])
.cd(source_dir);
// Pass `-Zbuild-std` in case a build in the sandbox wants to use it;
// build-std has to have the source for libstd's dependencies available.
if !fetch_build_std_targets.is_empty() {
toolchain.add_component(workspace, "rust-src")?;
cmd = cmd.args(&["-Zbuild-std"]).env("RUSTC_BOOTSTRAP", "1");
}
for target in fetch_build_std_targets {
cmd = cmd.args(&["--target", target]);
}
let res = cmd
.process_lines(&mut |line, _| {
if line.contains("failed to load source for dependency") {
missing_deps = true;
}
})
.run();
match res {
Ok(_) => Ok(()),
Err(_) if missing_deps => Err(PrepareError::MissingDependencies.into()),
err => err.map_err(|e| e.into()),
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/buildtest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ fn test_hello_world() {
});
}

#[test]
#[cfg(feature = "unstable")]
fn test_fetch_build_std() {
use std::path::Path;

let target_file = Path::new(env!("OUT_DIR")).join("target");
let target = std::fs::read_to_string(target_file).unwrap();

runner::run("hello-world", |run| {
run.run(SandboxBuilder::new().enable_networking(false), |build| {
build.fetch_build_std_dependencies(&vec![target.as_str()])?;
let storage = rustwide::logging::LogStorage::new(LevelFilter::Info);
rustwide::logging::capture(&storage, || -> Result<_, Error> {
build
.cargo()
.env("RUSTC_BOOTSTRAP", "1")
.args(&["run", "-Zbuild-std", "--target", &target])
.run()?;
Ok(())
})?;

assert!(storage.to_string().contains("[stdout] Hello, world!\n"));
assert!(storage
.to_string()
.contains("[stdout] Hello, world again!\n"));
Ok(())
})?;
Ok(())
});
}

#[test]
fn path_based_patch() {
runner::run("path-based-patch", |run| {
Expand Down

0 comments on commit 399d37c

Please sign in to comment.