Skip to content

Commit 5a3a8c6

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 326694e commit 5a3a8c6

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ 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. Note that you will still need to run
13+
`toolchain.add_component("rust-src")` before trying to use build-std.
14+
815
## [0.14.0] - 2021-08-19
916

1017
### Added

src/prepare.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,21 @@ 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)
145-
.process_lines(&mut |line, _| {
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 self.workspace.fetch_build_std_dependencies() {
148+
cmd = cmd
149+
.args(&["-Zbuild-std"])
150+
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
151+
}
152+
let res = cmd.process_lines(&mut |line, _| {
146153
if line.contains("failed to load source for dependency") {
147154
missing_deps = true;
148155
}
149-
})
150-
.run();
156+
}).run();
151157
match res {
152158
Ok(_) => Ok(()),
153159
Err(_) if missing_deps => Err(PrepareError::MissingDependencies.into()),

src/workspace.rs

+21
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct WorkspaceBuilder {
2727
command_timeout: Option<Duration>,
2828
command_no_output_timeout: Option<Duration>,
2929
fetch_registry_index_during_builds: bool,
30+
fetch_build_std_dependencies: bool,
3031
running_inside_docker: bool,
3132
fast_init: bool,
3233
rustup_profile: String,
@@ -45,6 +46,7 @@ impl WorkspaceBuilder {
4546
command_timeout: DEFAULT_COMMAND_TIMEOUT,
4647
command_no_output_timeout: DEFAULT_COMMAND_NO_OUTPUT_TIMEOUT,
4748
fetch_registry_index_during_builds: true,
49+
fetch_build_std_dependencies: false,
4850
running_inside_docker: false,
4951
fast_init: false,
5052
rustup_profile: DEFAULT_RUSTUP_PROFILE.into(),
@@ -107,6 +109,19 @@ impl WorkspaceBuilder {
107109
self
108110
}
109111

112+
/// Enable or disable pre-fetching the dependencies for `-Z build-std` when preparing the sandbox (disabled by default).
113+
///
114+
/// When this option is enabled, it is possible to use `-Zbuild-std` inside
115+
/// the sandbox to build the standard library from source even when
116+
/// networking is disabled. You will still need to run
117+
/// `toolchain.add_component("rust-src")` before entering the sandbox.
118+
#[cfg(any(feature = "unstable", doc))]
119+
#[cfg_attr(docs_rs, doc(cfg(feature = "unstable")))]
120+
pub fn fetch_build_std_dependencies(mut self, enable: bool) -> Self {
121+
self.fetch_build_std_dependencies = enable;
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: bool,
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) -> bool {
320+
self.inner.fetch_build_std_dependencies
321+
}
322+
302323
pub(crate) fn current_container(&self) -> Option<&CurrentContainer> {
303324
self.inner.current_container.as_ref()
304325
}

0 commit comments

Comments
 (0)