Skip to content

Commit d526484

Browse files
Merge #939
939: Fix cross when using absolute paths to the current project. r=Emilgardis a=Alexhuszagh Closes #938. Co-authored-by: Alex Huszagh <ahuszagh@gmail.com>
2 parents de661ef + 30e76ca commit d526484

File tree

4 files changed

+41
-60
lines changed

4 files changed

+41
-60
lines changed

.changes/939.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "changed",
3+
"description": "Remove `/project` mounting, instead always mount in the same path as on the host",
4+
"issues": [938]
5+
}

src/docker/local.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) fn run(
2424
docker_userns(&mut docker);
2525
docker_envvars(&mut docker, &options.config, &options.target, msg_info)?;
2626

27-
let mount_volumes = docker_mount(
27+
docker_mount(
2828
&mut docker,
2929
&options,
3030
&paths,
@@ -43,18 +43,14 @@ pub(crate) fn run(
4343
.args(&["-v", &format!("{}:/cargo:Z", dirs.cargo.to_utf8()?)])
4444
// Prevent `bin` from being mounted inside the Docker container.
4545
.args(&["-v", "/cargo/bin"]);
46-
if mount_volumes {
47-
docker.args(&[
48-
"-v",
49-
&format!("{}:{}:Z", dirs.host_root.to_utf8()?, dirs.mount_root),
50-
]);
51-
} else {
52-
docker.args(&["-v", &format!("{}:/project:Z", dirs.host_root.to_utf8()?)]);
53-
}
46+
docker.args(&[
47+
"-v",
48+
&format!("{}:{}:Z", dirs.host_root.to_utf8()?, dirs.mount_root),
49+
]);
5450
docker
5551
.args(&["-v", &format!("{}:/rust:Z,ro", dirs.sysroot.to_utf8()?)])
5652
.args(&["-v", &format!("{}:/target:Z", dirs.target.to_utf8()?)]);
57-
docker_cwd(&mut docker, &paths, mount_volumes)?;
53+
docker_cwd(&mut docker, &paths)?;
5854

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

src/docker/remote.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ pub(crate) fn run(
915915
docker.args(&["-v", &volume_mount]);
916916

917917
let mut volumes = vec![];
918-
let mount_volumes = docker_mount(
918+
docker_mount(
919919
&mut docker,
920920
&options,
921921
&paths,
@@ -1018,28 +1018,23 @@ pub(crate) fn run(
10181018
)
10191019
.wrap_err("when copying rust target files")?;
10201020
}
1021-
let mount_root = if mount_volumes {
1022-
// cannot panic: absolute unix path, must have root
1023-
let rel_mount_root = dirs
1024-
.mount_root
1025-
.strip_prefix('/')
1026-
.expect("mount root should be absolute");
1027-
let mount_root = mount_prefix_path.join(rel_mount_root);
1028-
if !rel_mount_root.is_empty() {
1029-
create_volume_dir(
1030-
engine,
1031-
&container,
1032-
mount_root
1033-
.parent()
1034-
.expect("mount root should have a parent directory"),
1035-
msg_info,
1036-
)
1037-
.wrap_err("when creating mount root")?;
1038-
}
1039-
mount_root
1040-
} else {
1041-
mount_prefix_path.join("project")
1042-
};
1021+
// cannot panic: absolute unix path, must have root
1022+
let rel_mount_root = dirs
1023+
.mount_root
1024+
.strip_prefix('/')
1025+
.expect("mount root should be absolute");
1026+
let mount_root = mount_prefix_path.join(rel_mount_root);
1027+
if !rel_mount_root.is_empty() {
1028+
create_volume_dir(
1029+
engine,
1030+
&container,
1031+
mount_root
1032+
.parent()
1033+
.expect("mount root should have a parent directory"),
1034+
msg_info,
1035+
)
1036+
.wrap_err("when creating mount root")?;
1037+
}
10431038
copy_volume_container_project(
10441039
engine,
10451040
&container,
@@ -1175,7 +1170,7 @@ symlink_recurse \"${{prefix}}\"
11751170
let mut docker = subcommand(engine, "exec");
11761171
docker_user_id(&mut docker, engine.kind);
11771172
docker_envvars(&mut docker, &options.config, target, msg_info)?;
1178-
docker_cwd(&mut docker, &paths, mount_volumes)?;
1173+
docker_cwd(&mut docker, &paths)?;
11791174
docker.arg(&container);
11801175
docker.args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)]);
11811176
bail_container_exited!();

src/docker/shared.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ use crate::cargo::{cargo_metadata_with_args, CargoMetadata};
99
use crate::config::{bool_from_envvar, Config};
1010
use crate::errors::*;
1111
use crate::extensions::{CommandExt, SafeCommand};
12-
use crate::file::{self, write_file, PathExt, ToUtf8};
12+
use crate::file::{self, write_file, ToUtf8};
1313
use crate::id;
1414
use crate::rustc::{self, VersionMetaExt};
1515
use crate::shell::{MessageInfo, Verbosity};
1616
use crate::Target;
1717

18+
#[cfg(target_os = "windows")]
19+
use crate::file::PathExt;
20+
1821
pub use super::custom::CROSS_CUSTOM_DOCKERFILE_IMAGE_PREFIX;
1922

2023
pub const CROSS_IMAGE: &str = "ghcr.io/cross-rs";
@@ -496,20 +499,8 @@ pub(crate) fn docker_envvars(
496499
Ok(())
497500
}
498501

499-
pub(crate) fn docker_cwd(
500-
docker: &mut Command,
501-
paths: &DockerPaths,
502-
mount_volumes: bool,
503-
) -> Result<()> {
504-
if mount_volumes {
505-
docker.args(&["-w", paths.mount_cwd()]);
506-
} else if paths.mount_cwd() == paths.workspace_root().to_utf8()? {
507-
docker.args(&["-w", "/project"]);
508-
} else {
509-
// We do this to avoid clashes with path separators. Windows uses `\` as a path separator on Path::join
510-
let working_dir = Path::new("/project").join(paths.workspace_from_cwd()?);
511-
docker.args(&["-w", &working_dir.as_posix()?]);
512-
}
502+
pub(crate) fn docker_cwd(docker: &mut Command, paths: &DockerPaths) -> Result<()> {
503+
docker.args(&["-w", paths.mount_cwd()]);
513504

514505
Ok(())
515506
}
@@ -520,14 +511,7 @@ pub(crate) fn docker_mount(
520511
paths: &DockerPaths,
521512
mount_cb: impl Fn(&mut Command, &Path) -> Result<String>,
522513
mut store_cb: impl FnMut((String, String)),
523-
) -> Result<bool> {
524-
let mut mount_volumes = false;
525-
// FIXME(emilgardis 2022-04-07): This is a fallback so that if it's hard for us to do mounting logic, make it simple(r)
526-
// Preferably we would not have to do this.
527-
if !paths.in_workspace() {
528-
mount_volumes = true;
529-
}
530-
514+
) -> Result<()> {
531515
for ref var in options
532516
.config
533517
.env_volumes(&options.target)?
@@ -545,7 +529,6 @@ pub(crate) fn docker_mount(
545529
let mount_path = mount_cb(docker, host_path.as_ref())?;
546530
docker.args(&["-e", &format!("{}={}", host_path, mount_path)]);
547531
store_cb((val, mount_path));
548-
mount_volumes = true;
549532
}
550533
}
551534

@@ -554,10 +537,9 @@ pub(crate) fn docker_mount(
554537
let host_path = paths.mount_finder.find_path(&canonical_path, true)?;
555538
let mount_path = mount_cb(docker, host_path.as_ref())?;
556539
store_cb((path.to_utf8()?.to_owned(), mount_path));
557-
mount_volumes = true;
558540
}
559541

560-
Ok(mount_volumes)
542+
Ok(())
561543
}
562544

563545
pub(crate) fn canonicalize_mount_path(path: &Path) -> Result<String> {
@@ -806,6 +788,9 @@ mod tests {
806788
use super::*;
807789
use crate::id;
808790

791+
#[cfg(not(target_os = "windows"))]
792+
use crate::file::PathExt;
793+
809794
#[test]
810795
fn test_docker_user_id() {
811796
let var = "CROSS_ROOTLESS_CONTAINER_ENGINE";

0 commit comments

Comments
 (0)