Skip to content

Commit

Permalink
Allow users to ignore config files in the package.
Browse files Browse the repository at this point in the history
Adds the `CROSS_IGNORE_CARGO_CONFIG` environment variable, which if set
will mount an anoymous data volume for each `.cargo` subdirectory for
the current directory and any parent directories up to the workspace
root. If the build is called outside the workspace root or at the
workspace root, only mount at the `$PWD/.cargo`.
  • Loading branch information
Alexhuszagh committed Jul 11, 2022
1 parent d526484 commit bb1f3d7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/936.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "added",
"description": "allow users to ignore config files in the package.",
"issues": [621]
}
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ impl Environment {
.ok()
}

fn ignore_cargo_config(&self) -> Option<bool> {
env::var("CROSS_IGNORE_CARGO_CONFIG")
.map(|s| bool_from_envvar(&s))
.ok()
}

fn custom_toolchain(&self) -> bool {
std::env::var("CROSS_CUSTOM_TOOLCHAIN").is_ok()
}
Expand Down Expand Up @@ -291,6 +297,10 @@ impl Config {
self.env.custom_toolchain()
}

pub fn ignore_cargo_config(&self) -> Option<bool> {
self.env.ignore_cargo_config()
}

pub fn env_passthrough(&self, target: &Target) -> Result<Option<Vec<String>>> {
self.vec_from_config(
target,
Expand Down
3 changes: 2 additions & 1 deletion src/docker/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ pub(crate) fn run(
docker
.args(&["-v", &format!("{}:/rust:Z,ro", dirs.sysroot.to_utf8()?)])
.args(&["-v", &format!("{}:/target:Z", dirs.target.to_utf8()?)]);
docker_cwd(&mut docker, &paths)?;
let ignore_cargo_config = options.config.ignore_cargo_config().unwrap_or_default();
docker_cwd(&mut docker, &paths, ignore_cargo_config)?;

// When running inside NixOS or using Nix packaging we need to add the Nix
// Store to the running container so it can load the needed binaries.
Expand Down
3 changes: 2 additions & 1 deletion src/docker/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,8 @@ symlink_recurse \"${{prefix}}\"
let mut docker = subcommand(engine, "exec");
docker_user_id(&mut docker, engine.kind);
docker_envvars(&mut docker, &options.config, target, msg_info)?;
docker_cwd(&mut docker, &paths)?;
let ignore_cargo_config = options.config.ignore_cargo_config().unwrap_or_default();
docker_cwd(&mut docker, &paths, ignore_cargo_config)?;
docker.arg(&container);
docker.args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)]);
bail_container_exited!();
Expand Down
51 changes: 50 additions & 1 deletion src/docker/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,18 @@ impl DockerPaths {
self.workspace_from_cwd().is_ok()
}

pub fn cargo_home(&self) -> &Path {
&self.directories.cargo
}

pub fn mount_cwd(&self) -> &str {
&self.directories.mount_cwd
}

pub fn mount_root(&self) -> &str {
&self.directories.mount_root
}

pub fn host_root(&self) -> &Path {
&self.directories.host_root
}
Expand Down Expand Up @@ -499,8 +507,49 @@ pub(crate) fn docker_envvars(
Ok(())
}

pub(crate) fn docker_cwd(docker: &mut Command, paths: &DockerPaths) -> Result<()> {
fn mount_to_ignore_cargo_config(
docker: &mut Command,
paths: &DockerPaths,
root: &str,
cwd: &str,
ignore_cargo_config: bool,
) -> Result<()> {
let is_cargo_home_parent = paths
.cargo_home()
.parent()
.map(|x| x == paths.host_root())
.unwrap_or_default();
if ignore_cargo_config && !is_cargo_home_parent {
let root_path = Path::new(root);
let cwd_path = Path::new(cwd);
docker.args(&["-v", &cwd_path.join(".cargo").as_posix()?]);
let mut relpath = Path::new(cwd).strip_prefix(root_path).wrap_err_with(|| {
eyre::eyre!("cwd \"{cwd}\" must be a subdirectory of root \"{root}\"")
})?;

while let Some(parent) = relpath.parent() {
let path = root_path.join(parent);
docker.args(&["-v", &path.join(".cargo").as_posix()?]);
relpath = parent;
}
}

Ok(())
}

pub(crate) fn docker_cwd(
docker: &mut Command,
paths: &DockerPaths,
ignore_cargo_config: bool,
) -> Result<()> {
docker.args(&["-w", paths.mount_cwd()]);
mount_to_ignore_cargo_config(
docker,
paths,
paths.mount_root(),
paths.mount_cwd(),
ignore_cargo_config,
)?;

Ok(())
}
Expand Down

0 comments on commit bb1f3d7

Please sign in to comment.