Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust absolute paths on Windows for Docker #30

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/docker_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -83,6 +85,30 @@ impl DockerClient {
}
}

fn canonicalize_os_path(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
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::*;
Expand Down Expand Up @@ -168,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();
Expand Down
Loading