Skip to content

Commit f253bd0

Browse files
Merge #1157
1157: Fix mount paths on Windows. r=Emilgardis a=Alexhuszagh Fixes mount paths outside of the workspace mount directory on Windows and those provided as a WSL path. This also adds unittests for Windows outside of WSL2. Closes #1145. Closes #1156. Co-authored-by: Alex Huszagh <[email protected]>
2 parents 0d2b8b5 + d77fbe0 commit f253bd0

10 files changed

+84
-15
lines changed

.changes/1057.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"description": "fix mount paths outside of the workspace mount directory on Windows and those provided as a WSL path.",
3+
"type": "fixed",
4+
"issues": [1145, 1156]
5+
}

ci/shared.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,20 @@ function mkcargotemp {
4848
' > "$CARGO_TMP_DIR"/Cargo.toml
4949
echo "$td"
5050
}
51+
52+
function binary_path() {
53+
local binary="${1}"
54+
local home="${2}"
55+
local build_mode="${3}"
56+
local cross="${home}/target/${build_mode}/${binary}"
57+
58+
case "$OSTYPE" in
59+
msys*|cygwin*)
60+
cross="${cross}.exe"
61+
;;
62+
*)
63+
;;
64+
esac
65+
66+
echo "${cross}"
67+
}

ci/test-bisect.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ main() {
2222
retry cargo fetch
2323
cargo build
2424
cargo install cargo-bisect-rustc --debug
25-
export CROSS="${PROJECT_HOME}/target/debug/cross"
25+
CROSS=$(binary_path cross "${PROJECT_HOME}" debug)
26+
export CROSS
2627

2728
td="$(mktemp -d)"
2829
git clone --depth 1 https://github.com/cross-rs/rust-cpp-hello-word "${td}"

ci/test-foreign-toolchain.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ main() {
1515

1616
retry cargo fetch
1717
cargo build
18-
export CROSS="${PROJECT_HOME}/target/debug/cross"
18+
CROSS=$(binary_path cross "${PROJECT_HOME}" debug)
19+
export CROSS
1920

2021
td="$(mkcargotemp -d)"
2122

@@ -31,7 +32,7 @@ image.name = "alpine:edge"
3132
image.toolchain = ["x86_64-unknown-linux-musl"]
3233
pre-build = ["apk add --no-cache gcc musl-dev"]' >"${CARGO_TMP_DIR}"/Cross.toml
3334

34-
"$CROSS" run -v
35+
"${CROSS}" run -v
3536

3637
local tmp_basename
3738
tmp_basename=$(basename "${CARGO_TMP_DIR}")
@@ -59,7 +60,7 @@ name = "ubuntu:20.04"
5960
toolchain = ["aarch64-unknown-linux-gnu"]
6061
' >"${CARGO_TMP_DIR}"/Cross.toml
6162

62-
"$CROSS" build -v
63+
"${CROSS}" build -v
6364

6465
"${CROSS_ENGINE}" images --format '{{.Repository}}:{{.Tag}}' --filter 'label=org.cross-rs.for-cross-target' | grep "cross-custom-${tmp_basename}" | xargs "${CROSS_ENGINE}" rmi
6566

ci/test-podman.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ main() {
2222

2323
retry cargo fetch
2424
cargo build
25-
export CROSS="${PROJECT_HOME}/target/debug/cross"
25+
CROSS=$(binary_path cross "${PROJECT_HOME}" debug)
26+
export CROSS
2627

2728
td="$(mkcargotemp -d)"
2829
parent=$(dirname "${td}")

ci/test-remote.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ main() {
2020

2121
retry cargo fetch
2222
cargo build
23-
export CROSS="${PROJECT_HOME}/target/debug/cross"
24-
export CROSS_UTIL="${PROJECT_HOME}/target/debug/cross-util"
23+
CROSS=$(binary_path cross "${PROJECT_HOME}" debug)
24+
export CROSS
25+
CROSS_UTIL=$(binary_path cross-util "${PROJECT_HOME}" debug)
26+
export CROSS_UTIL
2527

2628
# if the create volume fails, ensure it exists.
2729
if ! err=$("${CROSS_UTIL}" volumes create 2>&1 >/dev/null); then

ci/test-zig-image.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ main() {
3434

3535
retry cargo fetch
3636
cargo build
37-
export CROSS="${PROJECT_HOME}/target/debug/cross"
37+
CROSS=$(binary_path cross "${PROJECT_HOME}" debug)
38+
export CROSS
3839

3940
td="$(mktemp -d)"
4041
git clone --depth 1 https://github.com/cross-rs/rust-cpp-hello-word "${td}"

ci/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ main() {
3131
export QEMU_STRACE=1
3232

3333
# ensure we have the proper toolchain and optional rust flags
34-
export CROSS=("${PROJECT_HOME}/target/debug/cross")
34+
CROSS=$(binary_path cross "${PROJECT_HOME}" debug)
35+
export CROSS=("${CROSS}")
3536
export CROSS_FLAGS="-v"
3637
if (( ${BUILD_STD:-0} )); then
3738
# use build-std instead of xargo, due to xargo being

src/docker/local.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub(crate) fn run(
7878
])
7979
// Prevent `bin` from being mounted inside the Docker container.
8080
.args(["-v", &format!("{}/bin", toolchain_dirs.cargo_mount_path())]);
81+
8182
docker.args([
8283
"-v",
8384
&format!(

src/file.rs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub trait PathExt {
3434
#[cfg(target_family = "windows")]
3535
fn format_prefix(prefix: &str) -> Result<String> {
3636
match prefix {
37-
"" => eyre::bail!("Error: got empty windows prefix"),
37+
"" => Ok("".to_owned()),
3838
_ => Ok(format!("/mnt/{}", prefix.to_lowercase())),
3939
}
4040
}
@@ -196,13 +196,28 @@ fn _canonicalize(path: &Path) -> Result<PathBuf> {
196196
}
197197
}
198198

199+
fn is_wsl_absolute(path: &str) -> bool {
200+
if !cfg!(target_os = "windows") {
201+
return false;
202+
}
203+
let path = path.strip_prefix("/mnt/").unwrap_or(path);
204+
let maybe_drive = path.split_once('/').map_or(path, |x| x.0);
205+
206+
maybe_drive.len() == 1 && matches!(maybe_drive.chars().next(), Some('a'..='z'))
207+
}
208+
199209
// Fix for issue #581. target_dir must be absolute.
200210
pub fn absolute_path(path: impl AsRef<Path>) -> Result<PathBuf> {
201-
Ok(if path.as_ref().is_absolute() {
202-
path.as_ref().to_path_buf()
203-
} else {
204-
env::current_dir()?.join(path)
205-
})
211+
let as_ref = path.as_ref();
212+
Ok(
213+
if as_ref.is_absolute()
214+
|| cfg!(target_family = "windows") && is_wsl_absolute(as_ref.to_utf8()?)
215+
{
216+
as_ref.to_path_buf()
217+
} else {
218+
env::current_dir()?.join(path)
219+
},
220+
)
206221
}
207222

208223
/// Pretty format a file path. Also removes the path prefix from a command if wanted
@@ -319,9 +334,22 @@ mod tests {
319334
assert!(p!("C:").join("..").as_posix_relative().is_err());
320335
}
321336

337+
#[test]
338+
#[cfg(target_family = "windows")]
339+
fn is_absolute_wslpath() {
340+
assert!(is_wsl_absolute("/mnt/c/Users"));
341+
assert!(is_wsl_absolute("/mnt/c"));
342+
assert!(is_wsl_absolute("/mnt/z/Users"));
343+
assert!(!is_wsl_absolute("/mnt"));
344+
assert!(!is_wsl_absolute("/mnt/cc"));
345+
assert!(!is_wsl_absolute("/mnt/zc"));
346+
}
347+
322348
#[test]
323349
#[cfg(target_family = "windows")]
324350
fn as_posix_with_drive() {
351+
use regex::Regex;
352+
325353
result_eq(p!(r"C:\").as_posix_absolute(), Ok("/mnt/c".to_owned()));
326354
result_eq(
327355
p!(r"C:\Users").as_posix_absolute(),
@@ -336,6 +364,17 @@ mod tests {
336364
p!(r"\\.\C:\Users").as_posix_absolute(),
337365
Ok("/mnt/c/Users".to_owned()),
338366
);
367+
368+
result_eq(
369+
p!(r"/mnt/c/Users").as_posix_absolute(),
370+
Ok("/mnt/c/Users".to_owned()),
371+
);
372+
result_eq(p!(r"/mnt/c").as_posix_absolute(), Ok("/mnt/c".to_owned()));
373+
374+
let regex = Regex::new("/mnt/[A-Za-z]/mnt").unwrap();
375+
let result = p!(r"/mnt").as_posix_absolute();
376+
assert!(result.is_ok());
377+
assert!(regex.is_match(&result.unwrap()));
339378
}
340379

341380
#[test]

0 commit comments

Comments
 (0)