Skip to content

Commit bb1f3d7

Browse files
committed
Allow users to ignore config files in the package.
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`.
1 parent d526484 commit bb1f3d7

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

.changes/936.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "added",
3+
"description": "allow users to ignore config files in the package.",
4+
"issues": [621]
5+
}

src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ impl Environment {
115115
.ok()
116116
}
117117

118+
fn ignore_cargo_config(&self) -> Option<bool> {
119+
env::var("CROSS_IGNORE_CARGO_CONFIG")
120+
.map(|s| bool_from_envvar(&s))
121+
.ok()
122+
}
123+
118124
fn custom_toolchain(&self) -> bool {
119125
std::env::var("CROSS_CUSTOM_TOOLCHAIN").is_ok()
120126
}
@@ -291,6 +297,10 @@ impl Config {
291297
self.env.custom_toolchain()
292298
}
293299

300+
pub fn ignore_cargo_config(&self) -> Option<bool> {
301+
self.env.ignore_cargo_config()
302+
}
303+
294304
pub fn env_passthrough(&self, target: &Target) -> Result<Option<Vec<String>>> {
295305
self.vec_from_config(
296306
target,

src/docker/local.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub(crate) fn run(
5050
docker
5151
.args(&["-v", &format!("{}:/rust:Z,ro", dirs.sysroot.to_utf8()?)])
5252
.args(&["-v", &format!("{}:/target:Z", dirs.target.to_utf8()?)]);
53-
docker_cwd(&mut docker, &paths)?;
53+
let ignore_cargo_config = options.config.ignore_cargo_config().unwrap_or_default();
54+
docker_cwd(&mut docker, &paths, ignore_cargo_config)?;
5455

5556
// When running inside NixOS or using Nix packaging we need to add the Nix
5657
// Store to the running container so it can load the needed binaries.

src/docker/remote.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,8 @@ symlink_recurse \"${{prefix}}\"
11701170
let mut docker = subcommand(engine, "exec");
11711171
docker_user_id(&mut docker, engine.kind);
11721172
docker_envvars(&mut docker, &options.config, target, msg_info)?;
1173-
docker_cwd(&mut docker, &paths)?;
1173+
let ignore_cargo_config = options.config.ignore_cargo_config().unwrap_or_default();
1174+
docker_cwd(&mut docker, &paths, ignore_cargo_config)?;
11741175
docker.arg(&container);
11751176
docker.args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)]);
11761177
bail_container_exited!();

src/docker/shared.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,18 @@ impl DockerPaths {
220220
self.workspace_from_cwd().is_ok()
221221
}
222222

223+
pub fn cargo_home(&self) -> &Path {
224+
&self.directories.cargo
225+
}
226+
223227
pub fn mount_cwd(&self) -> &str {
224228
&self.directories.mount_cwd
225229
}
226230

231+
pub fn mount_root(&self) -> &str {
232+
&self.directories.mount_root
233+
}
234+
227235
pub fn host_root(&self) -> &Path {
228236
&self.directories.host_root
229237
}
@@ -499,8 +507,49 @@ pub(crate) fn docker_envvars(
499507
Ok(())
500508
}
501509

502-
pub(crate) fn docker_cwd(docker: &mut Command, paths: &DockerPaths) -> Result<()> {
510+
fn mount_to_ignore_cargo_config(
511+
docker: &mut Command,
512+
paths: &DockerPaths,
513+
root: &str,
514+
cwd: &str,
515+
ignore_cargo_config: bool,
516+
) -> Result<()> {
517+
let is_cargo_home_parent = paths
518+
.cargo_home()
519+
.parent()
520+
.map(|x| x == paths.host_root())
521+
.unwrap_or_default();
522+
if ignore_cargo_config && !is_cargo_home_parent {
523+
let root_path = Path::new(root);
524+
let cwd_path = Path::new(cwd);
525+
docker.args(&["-v", &cwd_path.join(".cargo").as_posix()?]);
526+
let mut relpath = Path::new(cwd).strip_prefix(root_path).wrap_err_with(|| {
527+
eyre::eyre!("cwd \"{cwd}\" must be a subdirectory of root \"{root}\"")
528+
})?;
529+
530+
while let Some(parent) = relpath.parent() {
531+
let path = root_path.join(parent);
532+
docker.args(&["-v", &path.join(".cargo").as_posix()?]);
533+
relpath = parent;
534+
}
535+
}
536+
537+
Ok(())
538+
}
539+
540+
pub(crate) fn docker_cwd(
541+
docker: &mut Command,
542+
paths: &DockerPaths,
543+
ignore_cargo_config: bool,
544+
) -> Result<()> {
503545
docker.args(&["-w", paths.mount_cwd()]);
546+
mount_to_ignore_cargo_config(
547+
docker,
548+
paths,
549+
paths.mount_root(),
550+
paths.mount_cwd(),
551+
ignore_cargo_config,
552+
)?;
504553

505554
Ok(())
506555
}

0 commit comments

Comments
 (0)