diff --git a/.changes/936.json b/.changes/936.json new file mode 100644 index 000000000..27fc3d223 --- /dev/null +++ b/.changes/936.json @@ -0,0 +1,5 @@ +{ + "type": "added", + "description": "allow users to ignore config files in the package.", + "issues": [621] +} diff --git a/src/config.rs b/src/config.rs index a5d583016..5448cb78b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -115,6 +115,12 @@ impl Environment { .ok() } + fn ignore_cargo_config(&self) -> Option { + 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() } @@ -291,6 +297,10 @@ impl Config { self.env.custom_toolchain() } + pub fn ignore_cargo_config(&self) -> Option { + self.env.ignore_cargo_config() + } + pub fn env_passthrough(&self, target: &Target) -> Result>> { self.vec_from_config( target, diff --git a/src/docker/local.rs b/src/docker/local.rs index 72ecd9c49..10a9bc6b3 100644 --- a/src/docker/local.rs +++ b/src/docker/local.rs @@ -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. diff --git a/src/docker/remote.rs b/src/docker/remote.rs index 88f5c8a3a..f8bacab15 100644 --- a/src/docker/remote.rs +++ b/src/docker/remote.rs @@ -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!(); diff --git a/src/docker/shared.rs b/src/docker/shared.rs index 573ba2458..5a3063310 100644 --- a/src/docker/shared.rs +++ b/src/docker/shared.rs @@ -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 } @@ -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(()) }