diff --git a/.changes/fix-space-in-path.md b/.changes/fix-space-in-path.md new file mode 100644 index 0000000..488a10f --- /dev/null +++ b/.changes/fix-space-in-path.md @@ -0,0 +1,6 @@ +--- +"nsis_process": patch +"nsis_tauri_utils": patch +--- + +Fix can't launch the app sometimes if the program path contains spaces diff --git a/crates/nsis-process/src/lib.rs b/crates/nsis-process/src/lib.rs index 51e9ba2..6293519 100644 --- a/crates/nsis-process/src/lib.rs +++ b/crates/nsis-process/src/lib.rs @@ -274,7 +274,7 @@ unsafe fn run_as_user(program: &str, arguments: &str) -> bool { lpAttributeList: attribute_list, }; let mut process_info: PROCESS_INFORMATION = mem::zeroed(); - let mut command_line = program.to_owned(); + let mut command_line = "\"".to_owned() + program + "\""; if !arguments.is_empty() { command_line.push(' '); command_line.push_str(arguments); @@ -337,6 +337,7 @@ impl DerefMut for OwnedHandle { #[cfg(test)] mod tests { + use super::*; #[test] @@ -357,4 +358,29 @@ mod tests { fn spawn_cmd() { unsafe { run_as_user("cmd", "/c timeout 3") }; } + + #[test] + #[cfg(feature = "test")] + fn spawn_with_spaces() { + extern crate std; + use alloc::format; + use alloc::string::ToString; + + let current = std::env::current_dir().unwrap(); + + let dir = current.join("dir space"); + std::fs::create_dir_all(&dir).unwrap(); + + let systemroot = std::env::var("SYSTEMROOT").unwrap_or_else(|_| "C:\\Windows".to_owned()); + + let cmd = format!("{systemroot}\\System32\\cmd.exe"); + let cmd_out = dir.join("cmdout.exe"); + + std::fs::copy(cmd, &cmd_out).unwrap(); + + assert!(unsafe { run_as_user(cmd_out.display().to_string().as_str(), "/c timeout 3") }); + + std::thread::sleep(std::time::Duration::from_secs(5)); + std::fs::remove_file(cmd_out).unwrap(); + } }