From c1c3d475b83c950d0c61faedf1eb68f7105e932e Mon Sep 17 00:00:00 2001 From: Aaron Cornelius Date: Sat, 21 Sep 2024 10:43:22 -0400 Subject: [PATCH 1/2] Adjust absolute paths on Windows for Docker --- src/docker_client.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/docker_client.rs b/src/docker_client.rs index a458c81..4017aaf 100644 --- a/src/docker_client.rs +++ b/src/docker_client.rs @@ -65,8 +65,10 @@ impl DockerClient { fn set_workdir(command: &mut Command) { let path = std::env::current_dir().expect("Failed to get current directory"); - let absolute_path = path.canonicalize().expect("Failed to get current directory"); - let current_dir = absolute_path.to_str().expect("Failed to get current directory"); + let absolute_path = canonicalize_os_path(&path).expect("Failed to build directory"); + let current_dir = absolute_path + .to_str() + .expect("Failed to get current directory"); command .arg("-v") @@ -83,6 +85,30 @@ impl DockerClient { } } +fn canonicalize_os_path(path: &std::path::Path) -> std::io::Result { + let canonicalized = std::fs::canonicalize(path)?; + + if cfg!(windows) { + let path_str = canonicalized.to_str().unwrap(); + // On Windows only, check if the path starts with the UNC prefix + // example: \\?\C:\path\to\file + if path_str.starts_with(r"\\?\") { + // drop UNC prefix + let path_str = &path_str[4..]; + // grab the drive letter + let drive_letter = &path_str[0..1]; + // swap \ for / + let rest_of_path = &path_str[2..].replace(r"\", "/"); + // rebuild as /C/path/to/file + return Ok(std::path::PathBuf::from(format!( + "/{}/{}", + drive_letter, rest_of_path + ))); + } + } + Ok(canonicalized) +} + #[cfg(test)] mod tests { use super::*; From 68bf92fe4059f2bac31bd07af169a9a0e4424b20 Mon Sep 17 00:00:00 2001 From: Aaron Cornelius Date: Sat, 21 Sep 2024 11:01:03 -0400 Subject: [PATCH 2/2] Adjust test using new function --- src/docker_client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/docker_client.rs b/src/docker_client.rs index 4017aaf..5f6c635 100644 --- a/src/docker_client.rs +++ b/src/docker_client.rs @@ -194,7 +194,7 @@ mod tests { assert_eq!(command.get_program(), "docker"); let binding = current_dir().unwrap(); - let absolute_path = binding.canonicalize().unwrap(); + let absolute_path = canonicalize_os_path(&binding).unwrap(); let current_dir = absolute_path.to_str().unwrap(); let args: Vec<&OsStr> = command.get_args().collect();