From 7cd3dcaee45e852ec752eadedf1ecc5c95825141 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 18 Jul 2021 14:57:33 -0700 Subject: [PATCH] tidy up cwd vs. home dir on Windows Looking at #900; the unconditional directory change on startup is "bad" because it only happens on Windows. This commit removes it and instead puts the logic into the pty layer to match the unix behavior. The behavior is: * If the command specifies the cwd, use that. * Otherwise, use the home directory --- pty/src/cmdbuilder.rs | 36 +++++++++++++++++------------------- wezterm-gui/src/main.rs | 17 +---------------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/pty/src/cmdbuilder.rs b/pty/src/cmdbuilder.rs index 83f66b490..ec45802e3 100644 --- a/pty/src/cmdbuilder.rs +++ b/pty/src/cmdbuilder.rs @@ -142,30 +142,22 @@ impl CommandBuilder { // basename with `-` and setting that as argv0 let basename = shell.rsplit('/').next().unwrap_or(&shell); cmd.arg0(&format!("-{}", basename)); - - let home = Self::get_home_dir()?; - let dir: &OsStr = self - .cwd - .as_ref() - .map(|dir| dir.as_os_str()) - .filter(|dir| std::path::Path::new(dir).is_dir()) - .unwrap_or(home.as_ref()); - cmd.current_dir(dir); cmd } else { let mut cmd = std::process::Command::new(&self.args[0]); cmd.args(&self.args[1..]); - let home = Self::get_home_dir()?; - let dir: &OsStr = self - .cwd - .as_ref() - .map(|dir| dir.as_os_str()) - .filter(|dir| std::path::Path::new(dir).is_dir()) - .unwrap_or(home.as_ref()); - cmd.current_dir(dir); cmd }; + let home = Self::get_home_dir()?; + let dir: &OsStr = self + .cwd + .as_ref() + .map(|dir| dir.as_os_str()) + .filter(|dir| std::path::Path::new(dir).is_dir()) + .unwrap_or(home.as_ref()); + cmd.current_dir(dir); + for (key, val) in &self.envs { cmd.env(key, val); } @@ -243,9 +235,15 @@ impl CommandBuilder { } pub(crate) fn current_directory(&self) -> Option> { - self.cwd.as_ref().map(|c| { + use std::path::Path; + + let home = std::env::var_os("USERPROFILE").filter(|path| Path::new(path).is_dir()); + let cwd = self.cwd.as_ref().filter(|path| Path::new(path).is_dir()); + let dir = cwd.or_else(|| home.as_ref()); + + dir.map(|dir| { let mut wide = vec![]; - wide.extend(c.encode_wide()); + wide.extend(dir.encode_wide()); wide.push(0); wide }) diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index 50ae3861f..9c21e7e04 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -564,22 +564,7 @@ fn run() -> anyhow::Result<()> { // input but didn't know to re-draw the prompt. #[cfg(windows)] unsafe { - if winapi::um::wincon::AttachConsole(winapi::um::wincon::ATTACH_PARENT_PROCESS) == 0 { - /* - // If we failed to attach the console then we're running in - // a gui only context. To aid in troubleshooting, let's redirect - // the stdio streams to a log file - let stdout = config.daemon_options.open_stdout()?; - let stderr = config.daemon_options.open_stderr()?; - use filedescriptor::IntoRawFileDescriptor; - use winapi::um::processenv::SetStdHandle; - use winapi::um::winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE}; - SetStdHandle(STD_OUTPUT_HANDLE, stdout.into_raw_file_descriptor()); - SetStdHandle(STD_ERROR_HANDLE, stderr.into_raw_file_descriptor()); - */ - - std::env::set_current_dir(config::HOME_DIR.as_path())?; - } + winapi::um::wincon::AttachConsole(winapi::um::wincon::ATTACH_PARENT_PROCESS); }; env_bootstrap::bootstrap();