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
This commit is contained in:
Wez Furlong committed 2021-07-18 14:57:33 -07:00
1 parent f050659543
commit 7cd3dcaee4
2 files changed
+18 -35

No files matched your search

+17 -19
View File
@@ -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<Vec<u16>> {
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
})
+1 -16
View File
@@ -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();