Skip to content

Commit 5c579a5

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 f945bed commit 5c579a5

File tree

6 files changed

+81
-7
lines changed

6 files changed

+81
-7
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+
- Builds in the sandbox can now use `-Zbuild-std` when networking is disabled.
11+
Previously, this would try to fetch the standard library sources, which would
12+
error when networking was blocked.
13+
814
## [0.15.2] - 2022-11-08
915

1016
### Changed

src/prepare.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,18 @@ impl<'a> Prepare<'a> {
139139

140140
fn fetch_deps(&mut self) -> Result<(), Error> {
141141
let mut missing_deps = false;
142-
let res = Command::new(self.workspace, self.toolchain.cargo())
142+
let mut cmd = Command::new(self.workspace, self.toolchain.cargo())
143143
.args(&["fetch", "--manifest-path", "Cargo.toml"])
144-
.cd(self.source_dir)
144+
.cd(self.source_dir);
145+
// Pass `-Zbuild-std` in case a build in the sandbox wants to use it;
146+
// build-std has to have the source for libstd's dependencies available.
147+
if let Some(target) = self.workspace.fetch_build_std_dependencies() {
148+
self.toolchain.add_component(self.workspace, "rust-src")?;
149+
cmd = cmd
150+
.args(&["-Zbuild-std", "--target", target])
151+
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
152+
}
153+
let res = cmd
145154
.process_lines(&mut |line, _| {
146155
if line.contains("failed to load source for dependency") {
147156
missing_deps = true;

src/workspace.rs

+21
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub struct WorkspaceBuilder {
2727
command_timeout: Option<Duration>,
2828
command_no_output_timeout: Option<Duration>,
2929
fetch_registry_index_during_builds: bool,
30+
/// Stores the `--target` to fetch dependencies for.
31+
fetch_build_std_dependencies: Option<String>,
3032
running_inside_docker: bool,
3133
fast_init: bool,
3234
rustup_profile: String,
@@ -45,6 +47,7 @@ impl WorkspaceBuilder {
4547
command_timeout: DEFAULT_COMMAND_TIMEOUT,
4648
command_no_output_timeout: DEFAULT_COMMAND_NO_OUTPUT_TIMEOUT,
4749
fetch_registry_index_during_builds: true,
50+
fetch_build_std_dependencies: None,
4851
running_inside_docker: false,
4952
fast_init: false,
5053
rustup_profile: DEFAULT_RUSTUP_PROFILE.into(),
@@ -107,6 +110,18 @@ impl WorkspaceBuilder {
107110
self
108111
}
109112

113+
/// Enable or disable pre-fetching the dependencies for `-Z build-std` when preparing the sandbox (disabled by default).
114+
///
115+
/// When this option is enabled, it is possible to use `-Zbuild-std` inside
116+
/// the sandbox to build the standard library from source even when
117+
/// networking is disabled.
118+
#[cfg(any(feature = "unstable", doc))]
119+
#[cfg_attr(docs_rs, doc(cfg(feature = "unstable")))]
120+
pub fn fetch_build_std_dependencies(mut self, target: String) -> Self {
121+
self.fetch_build_std_dependencies = Some(target);
122+
self
123+
}
124+
110125
/// Enable or disable support for running Rustwide itself inside Docker (disabled by default).
111126
///
112127
/// When support is enabled Rustwide will try to detect whether it's actually running inside a
@@ -160,6 +175,7 @@ impl WorkspaceBuilder {
160175
command_timeout: self.command_timeout,
161176
command_no_output_timeout: self.command_no_output_timeout,
162177
fetch_registry_index_during_builds: self.fetch_registry_index_during_builds,
178+
fetch_build_std_dependencies: self.fetch_build_std_dependencies,
163179
current_container: None,
164180
rustup_profile: self.rustup_profile,
165181
}),
@@ -183,6 +199,7 @@ struct WorkspaceInner {
183199
command_timeout: Option<Duration>,
184200
command_no_output_timeout: Option<Duration>,
185201
fetch_registry_index_during_builds: bool,
202+
fetch_build_std_dependencies: Option<String>,
186203
current_container: Option<CurrentContainer>,
187204
rustup_profile: String,
188205
}
@@ -299,6 +316,10 @@ impl Workspace {
299316
self.inner.fetch_registry_index_during_builds
300317
}
301318

319+
pub(crate) fn fetch_build_std_dependencies(&self) -> Option<&str> {
320+
self.inner.fetch_build_std_dependencies.as_deref()
321+
}
322+
302323
pub(crate) fn current_container(&self) -> Option<&CurrentContainer> {
303324
self.inner.current_container.as_ref()
304325
}

tests/buildtest/mod.rs

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

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

tests/buildtest/runner.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use rustwide::{cmd::SandboxBuilder, Build, BuildBuilder, Crate, Toolchain, Works
33
use std::path::Path;
44

55
pub(crate) fn run(crate_name: &str, f: impl FnOnce(&mut Runner) -> Result<(), Error>) {
6-
let mut runner = Runner::new(crate_name).unwrap();
6+
let workspace = crate::utils::init_workspace().unwrap();
7+
let mut runner = Runner::new(crate_name, workspace).unwrap();
78
f(&mut runner).unwrap();
89
}
910

@@ -15,8 +16,7 @@ pub(crate) struct Runner {
1516
}
1617

1718
impl Runner {
18-
fn new(crate_name: &str) -> Result<Self, Error> {
19-
let workspace = crate::utils::init_workspace()?;
19+
pub(crate) fn new(crate_name: &str, workspace: Workspace) -> Result<Self, Error> {
2020
let krate = Crate::local(
2121
&Path::new("tests")
2222
.join("buildtest")

tests/utils/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) fn init_workspace() -> Result<Workspace, Error> {
1313
init_named_workspace("integration")
1414
}
1515

16-
pub(crate) fn init_named_workspace(name: &str) -> Result<Workspace, Error> {
16+
pub(crate) fn prepare_named_workspace(name: &str) -> Result<WorkspaceBuilder, Error> {
1717
init_logs();
1818
let workspace_path = workspace_path(name);
1919
let mut builder = WorkspaceBuilder::new(&workspace_path, USER_AGENT).fast_init(true);
@@ -29,7 +29,11 @@ pub(crate) fn init_named_workspace(name: &str) -> Result<Workspace, Error> {
2929
)?);
3030
}
3131

32-
builder.init()
32+
Ok(builder)
33+
}
34+
35+
pub(crate) fn init_named_workspace(name: &str) -> Result<Workspace, Error> {
36+
prepare_named_workspace(name)?.init()
3337
}
3438

3539
fn init_logs() {

0 commit comments

Comments
 (0)