Skip to content

Commit

Permalink
Pass cargo configuration flags to docker.
Browse files Browse the repository at this point in the history
Ensures the following flags are passed to docker:
- BROWSER
- CARGO_BUILD_DEP_INFO_BASEDIR
- CARGO_BUILD_INCREMENTAL
- CARGO_BUILD_JOBS
- CARGO_BUILD_RUSTDOCFLAGS
- CARGO_BUILD_RUSTFLAGS
- CARGO_CACHE_RUSTC_INFO
- CARGO_CACHE_RUSTC_INFO
- CARGO_FUTURE_INCOMPAT_REPORT_FREQUENCY
- CARGO_HTTP_CAINFO
- CARGO_HTTP_CHECK_REVOKE
- CARGO_HTTP_DEBUG
- CARGO_HTTP_LOW_SPEED_LIMIT
- CARGO_HTTP_MULTIPLEXING
- CARGO_HTTP_PROXY
- CARGO_HTTP_SSL_VERSION
- CARGO_HTTP_TIMEOUT
- CARGO_HTTP_USER_AGENT
- CARGO_INCREMENTAL
- CARGO_INCREMENTAL
- CARGO_NET_GIT_FETCH_WITH_CLI
- CARGO_NET_OFFLINE
- CARGO_NET_RETRY
- HTTP_TIMEOUT
- HTTPS_PROXY
- RUSTDOCFLAGS
- RUSTFLAGS
- TERM

These are always correct, since these configure how `cargo` behavior is
supposed to work, but don't rely on any paths on the host filesystem. We also have some future-proofing, since we accept anything starting with `CARGO_` except the following variables:

It also ensures the following overridden environment variables are not passed:
- CARGO_HOME
- CARGO_TARGET_DIR
- CARGO_BUILD_TARGET_DIR

And it ensures the following not-yet-supported environment variables are not passed:
- CARGO_BUILD_RUSTC
- CARGO_BUILD_RUSTC_WRAPPER
- CARGO_BUILD_RUSTC_WORKSPACE_WRAPPER
- CARGO_BUILD_RUSTDOC
  • Loading branch information
Alexhuszagh committed Jun 26, 2022
1 parent 037070d commit 6d8194f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - ReleaseDate

### Changed
- #869 - ensure cargo configuration environment variable flags are passed to the docker container.

### Fixed

- #867 - fixed parsing of `build.env,passthrough` config values.
Expand Down
35 changes: 35 additions & 0 deletions src/docker/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,40 @@ pub(crate) fn cargo_safe_command(uses_xargo: bool) -> SafeCommand {
}
}

fn add_cargo_configuration_envvars(docker: &mut Command) {
let non_cargo_prefix = &[
"http_proxy",
"TERM",
"RUSTDOCFLAGS",
"RUSTFLAGS",
"BROWSER",
"HTTPS_PROXY",
"HTTP_TIMEOUT",
"https_proxy",
];
let cargo_prefix_skip = &[
"CARGO_HOME",
"CARGO_TARGET_DIR",
"CARGO_BUILD_TARGET_DIR",
"CARGO_BUILD_RUSTC",
"CARGO_BUILD_RUSTC_WRAPPER",
"CARGO_BUILD_RUSTC_WORKSPACE_WRAPPER",
"CARGO_BUILD_RUSTDOC",
];
let is_cargo_passthrough = |key: &str| -> bool {
non_cargo_prefix.contains(&key)
|| key.starts_with("CARGO_") && !cargo_prefix_skip.contains(&key)
};

// also need to accept any additional flags used to configure
// cargo, but only pass what's actually present.
for (key, _) in env::vars() {
if is_cargo_passthrough(&key) {
docker.args(&["-e", &key]);
}
}
}

pub(crate) fn mount(docker: &mut Command, val: &Path, prefix: &str) -> Result<String> {
let host_path = file::canonicalize(val)?;
let mount_path = canonicalize_mount_path(&host_path)?;
Expand Down Expand Up @@ -248,6 +282,7 @@ pub(crate) fn docker_envvars(docker: &mut Command, config: &Config, target: &Tar
.args(&["-e", "CARGO_HOME=/cargo"])
.args(&["-e", "CARGO_TARGET_DIR=/target"])
.args(&["-e", &cross_runner]);
add_cargo_configuration_envvars(docker);

if let Some(username) = id::username().unwrap() {
docker.args(&["-e", &format!("USER={username}")]);
Expand Down

0 comments on commit 6d8194f

Please sign in to comment.