Skip to content

Commit 41f9ba9

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 c8bb213 commit 41f9ba9

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
@@ -54,7 +54,8 @@ pub(crate) fn run(
5454
docker
5555
.args(&["-v", &format!("{}:/rust:Z,ro", dirs.sysroot.to_utf8()?)])
5656
.args(&["-v", &format!("{}:/target:Z", dirs.target.to_utf8()?)]);
57-
docker_cwd(&mut docker, &paths, mount_volumes)?;
57+
let ignore_cargo_config = options.config.ignore_cargo_config().unwrap_or_default();
58+
docker_cwd(&mut docker, &paths, mount_volumes, ignore_cargo_config)?;
5859

5960
// When running inside NixOS or using Nix packaging we need to add the Nix
6061
// 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
@@ -1175,7 +1175,8 @@ symlink_recurse \"${{prefix}}\"
11751175
let mut docker = subcommand(engine, "exec");
11761176
docker_user_id(&mut docker, engine.kind);
11771177
docker_envvars(&mut docker, &options.config, target, msg_info)?;
1178-
docker_cwd(&mut docker, &paths, mount_volumes)?;
1178+
let ignore_cargo_config = options.config.ignore_cargo_config().unwrap_or_default();
1179+
docker_cwd(&mut docker, &paths, mount_volumes, ignore_cargo_config)?;
11791180
docker.arg(&container);
11801181
docker.args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)]);
11811182
bail_container_exited!();

src/docker/shared.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,18 @@ impl DockerPaths {
217217
self.workspace_from_cwd().is_ok()
218218
}
219219

220+
pub fn cargo_home(&self) -> &Path {
221+
&self.directories.cargo
222+
}
223+
220224
pub fn mount_cwd(&self) -> &str {
221225
&self.directories.mount_cwd
222226
}
223227

228+
pub fn mount_root(&self) -> &str {
229+
&self.directories.mount_root
230+
}
231+
224232
pub fn host_root(&self) -> &Path {
225233
&self.directories.host_root
226234
}
@@ -496,19 +504,60 @@ pub(crate) fn docker_envvars(
496504
Ok(())
497505
}
498506

507+
fn mount_to_ignore_cargo_config(
508+
docker: &mut Command,
509+
paths: &DockerPaths,
510+
root: &str,
511+
cwd: &str,
512+
ignore_cargo_config: bool,
513+
) -> Result<()> {
514+
let is_cargo_home_parent = paths
515+
.cargo_home()
516+
.parent()
517+
.map(|x| x == paths.host_root())
518+
.unwrap_or_default();
519+
if ignore_cargo_config && !is_cargo_home_parent {
520+
let root_path = Path::new(root);
521+
let cwd_path = Path::new(cwd);
522+
docker.args(&["-v", &cwd_path.join(".cargo").as_posix()?]);
523+
let mut relpath = Path::new(cwd).strip_prefix(root_path).wrap_err_with(|| {
524+
eyre::eyre!("cwd \"{cwd}\" must be a subdirectory of root \"{root}\"")
525+
})?;
526+
527+
while let Some(parent) = relpath.parent() {
528+
let path = root_path.join(parent);
529+
docker.args(&["-v", &path.join(".cargo").as_posix()?]);
530+
relpath = parent;
531+
}
532+
}
533+
534+
Ok(())
535+
}
536+
499537
pub(crate) fn docker_cwd(
500538
docker: &mut Command,
501539
paths: &DockerPaths,
502540
mount_volumes: bool,
541+
ignore_cargo_config: bool,
503542
) -> Result<()> {
504543
if mount_volumes {
505544
docker.args(&["-w", paths.mount_cwd()]);
545+
mount_to_ignore_cargo_config(
546+
docker,
547+
paths,
548+
paths.mount_root(),
549+
paths.mount_cwd(),
550+
ignore_cargo_config,
551+
)?;
506552
} else if paths.mount_cwd() == paths.workspace_root().to_utf8()? {
507553
docker.args(&["-w", "/project"]);
554+
mount_to_ignore_cargo_config(docker, paths, "/project", "/project", ignore_cargo_config)?;
508555
} else {
509556
// We do this to avoid clashes with path separators. Windows uses `\` as a path separator on Path::join
510557
let working_dir = Path::new("/project").join(paths.workspace_from_cwd()?);
511-
docker.args(&["-w", &working_dir.as_posix()?]);
558+
let cwd = working_dir.as_posix()?;
559+
docker.args(&["-w", &cwd]);
560+
mount_to_ignore_cargo_config(docker, paths, "/project", &cwd, ignore_cargo_config)?;
512561
}
513562

514563
Ok(())

0 commit comments

Comments
 (0)