Skip to content

Commit 15162ea

Browse files
authored
fix: mount PYTHONPATH on windows as unix paths (#51)
* Fix wizer generation for windows * Move components import to function
1 parent 3d14e8b commit 15162ea

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

bin/src/opt.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,34 @@ impl<'a> Optimizer<'a> {
6767
Self { wizen, ..self }
6868
}
6969

70+
#[cfg(target_os = "windows")]
71+
fn convert_windows_paths(&self, paths: Vec<(String, PathBuf)>) -> Vec<(String, PathBuf)> {
72+
use std::path::Component;
73+
let mut ret = vec![];
74+
for (_, path) in paths {
75+
let new_path =
76+
path.components()
77+
.filter_map(|comp| match comp {
78+
Component::Normal(part) => Some(part.to_string_lossy().to_string()),
79+
_ => None, // Skip root, prefix, or other non-normal components
80+
})
81+
.collect::<Vec<_>>()
82+
.join("/");
83+
let normalized = format!("/{}", new_path);
84+
ret.push((normalized, path));
85+
}
86+
ret
87+
}
88+
7089
pub fn write_optimized_wasm(self, dest: impl AsRef<Path>) -> Result<(), Error> {
7190
let python_path = std::env::var("PYTHONPATH").unwrap_or_else(|_| String::from("."));
72-
let paths: Vec<&str> = python_path.split(':').collect();
91+
let split_paths = std::env::split_paths(&python_path);
92+
let paths: Vec<(String, PathBuf)> = split_paths.map(|p| (p.to_string_lossy().to_string(), p)).collect();
93+
94+
#[cfg(target_os = "windows")]
95+
let paths = self.convert_windows_paths(paths);
96+
#[cfg(target_os = "windows")]
97+
std::env::set_var("PYTHONPATH", paths.iter().map(|p| p.0.clone()).collect::<Vec<_>>().join(":"));
7398

7499
// Ensure compatibility with old releases
75100
let mut deps = find_deps().join("usr");
@@ -89,11 +114,11 @@ impl<'a> Optimizer<'a> {
89114
.inherit_env(true)
90115
.wasm_bulk_memory(true)
91116
.map_dir("/usr", deps);
92-
for path in paths {
93-
if path.is_empty() {
117+
for (mapped, path) in paths {
118+
if !path.exists() {
94119
continue;
95120
}
96-
w.map_dir(path, path);
121+
w.map_dir(mapped, path);
97122
}
98123
let wasm = w.run(self.wasm)?;
99124
std::fs::write(&dest, wasm)?;

0 commit comments

Comments
 (0)