From b8c720b4bf52245287d671ab0fb038456addc557 Mon Sep 17 00:00:00 2001 From: Sahil Gupte Date: Wed, 22 Jan 2025 14:35:20 -0800 Subject: [PATCH 1/2] Fix wizer generation for windows --- bin/src/opt.rs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/bin/src/opt.rs b/bin/src/opt.rs index d0fc3b6..80cd73e 100644 --- a/bin/src/opt.rs +++ b/bin/src/opt.rs @@ -1,6 +1,6 @@ use anyhow::{Error, Result}; use std::{ - path::{Path, PathBuf}, + path::{Component, Path, PathBuf}, process::{Command, Stdio}, }; use wizer::Wizer; @@ -67,9 +67,33 @@ impl<'a> Optimizer<'a> { Self { wizen, ..self } } + #[cfg(target_os = "windows")] + fn convert_windows_paths(&self, paths: Vec<(String, PathBuf)>) -> Vec<(String, PathBuf)> { + let mut ret = vec![]; + for (_, path) in paths { + let new_path = + path.components() + .filter_map(|comp| match comp { + Component::Normal(part) => Some(part.to_string_lossy().to_string()), + _ => None, // Skip root, prefix, or other non-normal components + }) + .collect::>() + .join("/"); + let normalized = format!("/{}", new_path); + ret.push((normalized, path)); + } + ret + } + pub fn write_optimized_wasm(self, dest: impl AsRef) -> Result<(), Error> { let python_path = std::env::var("PYTHONPATH").unwrap_or_else(|_| String::from(".")); - let paths: Vec<&str> = python_path.split(':').collect(); + let split_paths = std::env::split_paths(&python_path); + let paths: Vec<(String, PathBuf)> = split_paths.map(|p| (p.to_string_lossy().to_string(), p)).collect(); + + #[cfg(target_os = "windows")] + let paths = self.convert_windows_paths(paths); + #[cfg(target_os = "windows")] + std::env::set_var("PYTHONPATH", paths.iter().map(|p| p.0.clone()).collect::>().join(":")); // Ensure compatibility with old releases let mut deps = find_deps().join("usr"); @@ -89,11 +113,11 @@ impl<'a> Optimizer<'a> { .inherit_env(true) .wasm_bulk_memory(true) .map_dir("/usr", deps); - for path in paths { - if path.is_empty() { + for (mapped, path) in paths { + if !path.exists() { continue; } - w.map_dir(path, path); + w.map_dir(mapped, path); } let wasm = w.run(self.wasm)?; std::fs::write(&dest, wasm)?; From 4c4373291f1f49b18954bb6130f00488260ce08a Mon Sep 17 00:00:00 2001 From: Sahil Gupte Date: Wed, 22 Jan 2025 14:41:25 -0800 Subject: [PATCH 2/2] Move components import to function --- bin/src/opt.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/src/opt.rs b/bin/src/opt.rs index 80cd73e..86cd17a 100644 --- a/bin/src/opt.rs +++ b/bin/src/opt.rs @@ -1,6 +1,6 @@ use anyhow::{Error, Result}; use std::{ - path::{Component, Path, PathBuf}, + path::{Path, PathBuf}, process::{Command, Stdio}, }; use wizer::Wizer; @@ -69,6 +69,7 @@ impl<'a> Optimizer<'a> { #[cfg(target_os = "windows")] fn convert_windows_paths(&self, paths: Vec<(String, PathBuf)>) -> Vec<(String, PathBuf)> { + use std::path::Component; let mut ret = vec![]; for (_, path) in paths { let new_path =