diff --git a/apps/desktop-gpui/src/library.rs b/apps/desktop-gpui/src/library.rs index 19428742600..893b56bc75b 100644 --- a/apps/desktop-gpui/src/library.rs +++ b/apps/desktop-gpui/src/library.rs @@ -612,9 +612,7 @@ pub fn open_path(path: &Path) { } #[cfg(target_os = "windows")] { - std::process::Command::new("cmd") - .args(["/C", "start", "", &path.to_string_lossy()]) - .spawn() + shell_open(path) } #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] { @@ -626,6 +624,46 @@ pub fn open_path(path: &Path) { } } +/// `ShellExecuteW` rather than `cmd /C start`: `cmd` is a console program, so a +/// GUI-subsystem parent gets a console window, and the path would be parsed as +/// part of a command line instead of a single file argument. +#[cfg(target_os = "windows")] +fn shell_open(path: &Path) -> Result<(), String> { + use std::os::windows::ffi::OsStrExt as _; + use windows_sys::{ + Win32::UI::{Shell::ShellExecuteW, WindowsAndMessaging::SW_SHOWNORMAL}, + w, + }; + + let wide = |value: &std::ffi::OsStr| { + value + .encode_wide() + .chain(std::iter::once(0)) + .collect::>() + }; + let file = wide(path.as_os_str()); + // SAFETY: `file` is NUL-terminated and outlives the call; the remaining + // arguments are the documented nulls for the default verb. + let result = unsafe { + ShellExecuteW( + std::ptr::null_mut(), + w!("open"), + file.as_ptr(), + std::ptr::null(), + std::ptr::null(), + SW_SHOWNORMAL, + ) + }; + if result as isize > 32 { + Ok(()) + } else { + Err(format!( + "Windows could not open the path (code {})", + result as isize + )) + } +} + pub fn reveal_in_folder(path: &Path) { let result = { #[cfg(target_os = "macos")]