diff --git a/pty/src/cmdbuilder.rs b/pty/src/cmdbuilder.rs index afdba7328..687873b8c 100644 --- a/pty/src/cmdbuilder.rs +++ b/pty/src/cmdbuilder.rs @@ -213,6 +213,15 @@ impl CommandBuilder { exe.to_owned() } + pub(crate) fn current_directory(&self) -> Option> { + self.cwd.as_ref().map(|c| { + let mut wide = vec![]; + wide.extend(c.encode_wide()); + wide.push(0); + wide + }) + } + /// Constructs an environment block for this spawn attempt. /// Uses the current process environment as the base and then /// adds/replaces the environment that was specified via the diff --git a/pty/src/win/psuedocon.rs b/pty/src/win/psuedocon.rs index 9f5971fd9..aa583c2d4 100644 --- a/pty/src/win/psuedocon.rs +++ b/pty/src/win/psuedocon.rs @@ -99,6 +99,9 @@ impl PsuedoCon { let (mut exe, mut cmdline) = cmd.cmdline()?; let cmd_os = OsString::from_wide(&cmdline); + + let cwd = cmd.current_directory(); + let res = unsafe { CreateProcessW( exe.as_mut_slice().as_mut_ptr(), @@ -108,14 +111,23 @@ impl PsuedoCon { 0, EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, cmd.environment_block().as_mut_slice().as_mut_ptr() as *mut _, - ptr::null_mut(), + cwd.as_ref() + .map(|c| c.as_slice().as_ptr()) + .unwrap_or(ptr::null()), &mut si.StartupInfo, &mut pi, ) }; if res == 0 { let err = IoError::last_os_error(); - bail!("CreateProcessW `{:?}` failed: {}", cmd_os, err); + let msg = format!( + "CreateProcessW `{:?}` in cwd `{:?}` failed: {}", + cmd_os, + cwd.as_ref().map(|c| OsString::from_wide(c)), + err + ); + log::error!("{}", msg); + bail!("{}", msg); } // Make sure we close out the thread handle so we don't leak it; diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index e3e1454e1..3662883d6 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -1130,9 +1130,20 @@ impl TermWindow { ), }; let cwd = match cwd { - Some(url) if url.scheme() == "file" => Some(url.path().to_string()), + Some(url) if url.scheme() == "file" => { + let path = url.path().to_string(); + // On Windows the file URI can produce a path like: + // `/C:\Users` which is valid in a file URI, but the leading slash + // is not liked by the windows file APIs, so we strip it off here. + if cfg!(windows) && path.starts_with('/') { + Some(path[1..].to_owned()) + } else { + Some(path) + } + } Some(_) | None => None, }; + let tab = domain.spawn(size, None, cwd, mux_window_id).await?; let tab_id = tab.tab_id();