Skip to content

Commit 8b27866

Browse files
authored
Fix paths when using CROSS_CONTAINER_IN_CONTAINER (#1483)
Currently, `MountFinder` is used for almost all paths, rather than only for the host path in bind/volume mounts. Because of this, `docker build` invocations use the wrong working directory (`/var/lib/docker/.../workdir-path` instead of `/workdir-path`), causing failures for builds that use `pre-build` with a relative file path, and `docker run` invocations use incorrect `-v` arguments (e.g. `/var/lib/docker/.../toolchain-dir:/var/lib/docker/.../toolchain-dir` instead of `/var/lib/docker/.../toolchain-dir:/toolchain-dir`). This PR fixes those issues by only using `find_path` and `find_mount_path` when necessary.
2 parents 22fcf3d + 109737f commit 8b27866

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

.changes/1483.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"description": "Fix paths when using `CROSS_CONTAINER_IN_CONTAINER`",
3+
"type": "fixed"
4+
}

src/docker/local.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,25 @@ pub(crate) fn run(
9595
// Prevent `bin` from being mounted inside the Docker container.
9696
.args(["-v", &format!("{}/bin", toolchain_dirs.cargo_mount_path())]);
9797

98+
let host_root = paths.mount_finder.find_mount_path(package_dirs.host_root());
9899
docker.args([
99100
"-v",
100101
&format!(
101102
"{}:{}{selinux}",
102-
package_dirs.host_root().to_utf8()?,
103+
host_root.to_utf8()?,
103104
package_dirs.mount_root()
104105
),
105106
]);
107+
108+
let sysroot = paths
109+
.mount_finder
110+
.find_mount_path(toolchain_dirs.get_sysroot());
106111
docker
107112
.args([
108113
"-v",
109114
&format!(
110115
"{}:{}{selinux_ro}",
111-
toolchain_dirs.get_sysroot().to_utf8()?,
116+
sysroot.to_utf8()?,
112117
toolchain_dirs.sysroot_mount_path()
113118
),
114119
])

src/docker/shared.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ pub struct ToolchainDirectories {
263263
}
264264

265265
impl ToolchainDirectories {
266-
pub fn assemble(mount_finder: &MountFinder, mut toolchain: QualifiedToolchain) -> Result<Self> {
266+
pub fn assemble(mount_finder: &MountFinder, toolchain: QualifiedToolchain) -> Result<Self> {
267267
let home_dir =
268268
home::home_dir().ok_or_else(|| eyre::eyre!("could not find home directory"))?;
269269
let cargo = home::cargo_home()?;
@@ -311,8 +311,6 @@ impl ToolchainDirectories {
311311
let cargo = mount_finder.find_mount_path(cargo);
312312
let xargo = mount_finder.find_mount_path(xargo);
313313

314-
toolchain.set_sysroot(|p| mount_finder.find_mount_path(p));
315-
316314
// canonicalize these once to avoid syscalls
317315
let sysroot_mount_path = toolchain.get_sysroot().as_posix_absolute()?;
318316

@@ -413,30 +411,29 @@ pub struct PackageDirectories {
413411
impl PackageDirectories {
414412
pub fn assemble(
415413
mount_finder: &MountFinder,
416-
mut metadata: CargoMetadata,
414+
metadata: CargoMetadata,
417415
cwd: &Path,
418416
) -> Result<(Self, CargoMetadata)> {
419417
let target = &metadata.target_directory;
420418
// see ToolchainDirectories::assemble for creating directories
421419
create_target_dir(target)?;
422420

423-
metadata.target_directory = mount_finder.find_mount_path(target);
424-
425421
// root is either workspace_root, or, if we're outside the workspace root, the current directory
426-
let host_root = mount_finder.find_mount_path(if metadata.workspace_root.starts_with(cwd) {
422+
let host_root = if metadata.workspace_root.starts_with(cwd) {
427423
cwd
428424
} else {
429425
&metadata.workspace_root
430-
});
426+
}
427+
.to_path_buf();
431428

432429
// on Windows, we can not mount the directory name directly. Instead, we use wslpath to convert the path to a linux compatible path.
433430
// NOTE: on unix, host root has already found the mount path
434431
let mount_root = host_root.as_posix_absolute()?;
435-
let mount_cwd = mount_finder.find_path(cwd, false)?;
432+
let mount_cwd = cwd.as_posix_absolute()?;
436433

437434
Ok((
438435
PackageDirectories {
439-
target: metadata.target_directory.clone(),
436+
target: mount_finder.find_mount_path(target),
440437
host_root,
441438
mount_root,
442439
mount_cwd,
@@ -1193,10 +1190,7 @@ impl DockerCommandExt for Command {
11931190
if let Ok(val) = value {
11941191
let canonical_path = file::canonicalize(&val)?;
11951192
let host_path = paths.mount_finder.find_path(&canonical_path, true)?;
1196-
let absolute_path = Path::new(&val).as_posix_absolute()?;
1197-
let mount_path = paths
1198-
.mount_finder
1199-
.find_path(Path::new(&absolute_path), true)?;
1193+
let mount_path = Path::new(&val).as_posix_absolute()?;
12001194
mount_cb(self, host_path.as_ref(), mount_path.as_ref())?;
12011195
self.args(["-e", &format!("{}={}", var, mount_path)]);
12021196
store_cb((val, mount_path));
@@ -1209,10 +1203,7 @@ impl DockerCommandExt for Command {
12091203
// to the mounted project directory.
12101204
let canonical_path = file::canonicalize(path)?;
12111205
let host_path = paths.mount_finder.find_path(&canonical_path, true)?;
1212-
let absolute_path = Path::new(path).as_posix_absolute()?;
1213-
let mount_path = paths
1214-
.mount_finder
1215-
.find_path(Path::new(&absolute_path), true)?;
1206+
let mount_path = path.as_posix_absolute()?;
12161207
mount_cb(self, host_path.as_ref(), mount_path.as_ref())?;
12171208
store_cb((path.to_utf8()?.to_owned(), mount_path));
12181209
}
@@ -1716,15 +1707,9 @@ mod tests {
17161707

17171708
paths_equal(toolchain_dirs.cargo(), &mount_path(home()?.join(".cargo")))?;
17181709
paths_equal(toolchain_dirs.xargo(), &mount_path(home()?.join(".xargo")))?;
1719-
paths_equal(package_dirs.host_root(), &mount_path(get_cwd()?))?;
1720-
assert_eq!(
1721-
package_dirs.mount_root(),
1722-
&mount_path(get_cwd()?).as_posix_absolute()?
1723-
);
1724-
assert_eq!(
1725-
package_dirs.mount_cwd(),
1726-
&mount_path(get_cwd()?).as_posix_absolute()?
1727-
);
1710+
paths_equal(package_dirs.host_root(), &get_cwd()?)?;
1711+
assert_eq!(package_dirs.mount_root(), &get_cwd()?.as_posix_absolute()?);
1712+
assert_eq!(package_dirs.mount_cwd(), &get_cwd()?.as_posix_absolute()?);
17281713

17291714
reset_env(vars);
17301715
Ok(())

0 commit comments

Comments
 (0)