Skip to content

Commit f162cdc

Browse files
authored
Merge pull request #30 from acornmakes/windows_path_issue
Adjust absolute paths on Windows for Docker
2 parents 273b2e4 + 68bf92f commit f162cdc

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/docker_client.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ impl DockerClient {
6565

6666
fn set_workdir(command: &mut Command) {
6767
let path = std::env::current_dir().expect("Failed to get current directory");
68-
let absolute_path = path.canonicalize().expect("Failed to get current directory");
69-
let current_dir = absolute_path.to_str().expect("Failed to get current directory");
68+
let absolute_path = canonicalize_os_path(&path).expect("Failed to build directory");
69+
let current_dir = absolute_path
70+
.to_str()
71+
.expect("Failed to get current directory");
7072

7173
command
7274
.arg("-v")
@@ -83,6 +85,30 @@ impl DockerClient {
8385
}
8486
}
8587

88+
fn canonicalize_os_path(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
89+
let canonicalized = std::fs::canonicalize(path)?;
90+
91+
if cfg!(windows) {
92+
let path_str = canonicalized.to_str().unwrap();
93+
// On Windows only, check if the path starts with the UNC prefix
94+
// example: \\?\C:\path\to\file
95+
if path_str.starts_with(r"\\?\") {
96+
// drop UNC prefix
97+
let path_str = &path_str[4..];
98+
// grab the drive letter
99+
let drive_letter = &path_str[0..1];
100+
// swap \ for /
101+
let rest_of_path = &path_str[2..].replace(r"\", "/");
102+
// rebuild as /C/path/to/file
103+
return Ok(std::path::PathBuf::from(format!(
104+
"/{}/{}",
105+
drive_letter, rest_of_path
106+
)));
107+
}
108+
}
109+
Ok(canonicalized)
110+
}
111+
86112
#[cfg(test)]
87113
mod tests {
88114
use super::*;
@@ -168,7 +194,7 @@ mod tests {
168194
assert_eq!(command.get_program(), "docker");
169195

170196
let binding = current_dir().unwrap();
171-
let absolute_path = binding.canonicalize().unwrap();
197+
let absolute_path = canonicalize_os_path(&binding).unwrap();
172198
let current_dir = absolute_path.to_str().unwrap();
173199

174200
let args: Vec<&OsStr> = command.get_args().collect();

0 commit comments

Comments
 (0)