Skip to content

Commit 399d37c

Browse files
committed
Fetch dependencies for -Zbuild-std before entering the sandbox
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) ```
1 parent c2f235b commit 399d37c

File tree

4 files changed

+87
-15
lines changed

4 files changed

+87
-15
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## Unreleased
77

8+
### Added
9+
10+
- New method `Build::fetch_build_std_dependencies` for using `-Zbuild-std` within the sandbox when
11+
networking is disabled. Previously, this would try to fetch the standard library sources, which
12+
would error when networking was blocked.
13+
814
## [0.15.2] - 2022-11-08
915

1016
### Changed

src/build.rs

+16
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,20 @@ impl<'ws> Build<'ws> {
314314
pub fn host_target_dir(&self) -> PathBuf {
315315
self.dir.target_dir()
316316
}
317+
318+
/// Pre-fetching the dependencies for `-Z build-std` outside the sandbox.
319+
///
320+
/// When this function is called, it is possible to use `-Zbuild-std` inside
321+
/// the sandbox to build the standard library from source even when
322+
/// networking is disabled.
323+
#[cfg(any(feature = "unstable", doc))]
324+
#[cfg_attr(docs_rs, doc(cfg(feature = "unstable")))]
325+
pub fn fetch_build_std_dependencies(&self, targets: &[&str]) -> Result<(), Error> {
326+
crate::prepare::fetch_deps(
327+
&self.dir.workspace,
328+
self.toolchain,
329+
&self.host_source_dir(),
330+
targets,
331+
)
332+
}
317333
}

src/prepare.rs

+34-15
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,40 @@ impl<'a> Prepare<'a> {
138138
}
139139

140140
fn fetch_deps(&mut self) -> Result<(), Error> {
141-
let mut missing_deps = false;
142-
let res = Command::new(self.workspace, self.toolchain.cargo())
143-
.args(&["fetch", "--manifest-path", "Cargo.toml"])
144-
.cd(self.source_dir)
145-
.process_lines(&mut |line, _| {
146-
if line.contains("failed to load source for dependency") {
147-
missing_deps = true;
148-
}
149-
})
150-
.run();
151-
match res {
152-
Ok(_) => Ok(()),
153-
Err(_) if missing_deps => Err(PrepareError::MissingDependencies.into()),
154-
err => err.map_err(|e| e.into()),
155-
}
141+
fetch_deps(self.workspace, self.toolchain, self.source_dir, &[])
142+
}
143+
}
144+
145+
pub(crate) fn fetch_deps(
146+
workspace: &Workspace,
147+
toolchain: &Toolchain,
148+
source_dir: &Path,
149+
fetch_build_std_targets: &[&str],
150+
) -> Result<(), Error> {
151+
let mut missing_deps = false;
152+
let mut cmd = Command::new(workspace, toolchain.cargo())
153+
.args(&["fetch", "--manifest-path", "Cargo.toml"])
154+
.cd(source_dir);
155+
// Pass `-Zbuild-std` in case a build in the sandbox wants to use it;
156+
// build-std has to have the source for libstd's dependencies available.
157+
if !fetch_build_std_targets.is_empty() {
158+
toolchain.add_component(workspace, "rust-src")?;
159+
cmd = cmd.args(&["-Zbuild-std"]).env("RUSTC_BOOTSTRAP", "1");
160+
}
161+
for target in fetch_build_std_targets {
162+
cmd = cmd.args(&["--target", target]);
163+
}
164+
let res = cmd
165+
.process_lines(&mut |line, _| {
166+
if line.contains("failed to load source for dependency") {
167+
missing_deps = true;
168+
}
169+
})
170+
.run();
171+
match res {
172+
Ok(_) => Ok(()),
173+
Err(_) if missing_deps => Err(PrepareError::MissingDependencies.into()),
174+
err => err.map_err(|e| e.into()),
156175
}
157176
}
158177

tests/buildtest/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,37 @@ fn test_hello_world() {
2626
});
2727
}
2828

29+
#[test]
30+
#[cfg(feature = "unstable")]
31+
fn test_fetch_build_std() {
32+
use std::path::Path;
33+
34+
let target_file = Path::new(env!("OUT_DIR")).join("target");
35+
let target = std::fs::read_to_string(target_file).unwrap();
36+
37+
runner::run("hello-world", |run| {
38+
run.run(SandboxBuilder::new().enable_networking(false), |build| {
39+
build.fetch_build_std_dependencies(&vec![target.as_str()])?;
40+
let storage = rustwide::logging::LogStorage::new(LevelFilter::Info);
41+
rustwide::logging::capture(&storage, || -> Result<_, Error> {
42+
build
43+
.cargo()
44+
.env("RUSTC_BOOTSTRAP", "1")
45+
.args(&["run", "-Zbuild-std", "--target", &target])
46+
.run()?;
47+
Ok(())
48+
})?;
49+
50+
assert!(storage.to_string().contains("[stdout] Hello, world!\n"));
51+
assert!(storage
52+
.to_string()
53+
.contains("[stdout] Hello, world again!\n"));
54+
Ok(())
55+
})?;
56+
Ok(())
57+
});
58+
}
59+
2960
#[test]
3061
fn path_based_patch() {
3162
runner::run("path-based-patch", |run| {

0 commit comments

Comments
 (0)