Skip to content

Commit 5917e2e

Browse files
committed
fix: handle Windows paths
1 parent a6c1a8b commit 5917e2e

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

test/unit/extra_outdirs/proc_macro.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use proc_macro::TokenStream;
55
use std::env;
66
use std::fs;
7+
use std::fs::OpenOptions;
8+
use std::io::Write;
79
use std::path::PathBuf;
810

911
#[proc_macro]
@@ -16,15 +18,44 @@ pub fn write_to_outdirs(_item: TokenStream) -> TokenStream {
1618
// Write to the output directories declared by Bazel
1719
for entry in outdirs_paths.split(',') {
1820
if let Some((_dir, path)) = entry.split_once(':') {
19-
let dir_path = PathBuf::from(path.trim());
21+
let path_str = path.trim();
22+
// PathBuf will normalize the path correctly for the current platform
23+
// On Windows, ensure forward slashes are converted to backslashes
24+
let dir_path = if cfg!(windows) {
25+
// Convert forward slashes to backslashes for Windows
26+
let mut path_buf = PathBuf::from(path_str.replace('/', "\\"));
27+
// On Windows, ensure the path is absolute if it's not already
28+
if path_buf.is_relative() {
29+
// Try to make it absolute by joining with current directory
30+
if let Ok(current_dir) = std::env::current_dir() {
31+
path_buf = current_dir.join(&path_buf);
32+
}
33+
}
34+
path_buf
35+
} else {
36+
PathBuf::from(path_str)
37+
};
2038

2139
// Create the directory if it doesn't exist
40+
// create_dir_all creates all parent directories as needed
2241
if let Err(e) = fs::create_dir_all(&dir_path) {
2342
panic!("Failed to create directory {}: {:?}", dir_path.display(), e);
2443
}
2544
// Write a marker file to ensure the directory is created
45+
// Use OpenOptions on Windows to ensure proper file creation
2646
let marker_file = dir_path.join("marker.txt");
27-
if let Err(e) = fs::write(&marker_file, "created by proc-macro") {
47+
let result = if cfg!(windows) {
48+
// On Windows, use OpenOptions for more explicit control
49+
OpenOptions::new()
50+
.create(true)
51+
.write(true)
52+
.truncate(true)
53+
.open(&marker_file)
54+
.and_then(|mut file| file.write_all(b"created by proc-macro"))
55+
} else {
56+
fs::write(&marker_file, "created by proc-macro")
57+
};
58+
if let Err(e) = result {
2859
panic!(
2960
"Failed to write marker file to {}: {:?}",
3061
marker_file.display(),

0 commit comments

Comments
 (0)