Skip to content

Commit 7922b80

Browse files
authored
fix: use platform-appropriate shell on Windows (#6)
On Windows, detect PowerShell via PSModulePath (bundled since Win10), fall back to COMSPEC / cmd.exe. On Unix, use $SHELL with /bin/bash fallback (changed from /bin/zsh per issue #5 spec). Also resolves home directory from USERPROFILE on Windows and inherits platform-specific env vars (SystemRoot, APPDATA, TEMP, etc.). Closes #5
1 parent eada495 commit 7922b80

1 file changed

Lines changed: 44 additions & 5 deletions

File tree

src-tauri/src/terminal.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,52 @@ pub fn create_terminal(
5757

5858
let pair = pty_system.openpty(size).map_err(|e| e.to_string())?;
5959

60-
let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/zsh".to_string());
60+
// Platform-appropriate shell detection
61+
#[cfg(target_os = "windows")]
62+
let (shell, login_args): (String, Vec<String>) = {
63+
if std::env::var("PSModulePath").is_ok() {
64+
("powershell.exe".to_string(), vec!["-NoLogo".to_string()])
65+
} else {
66+
let s = std::env::var("COMSPEC").unwrap_or_else(|_| "cmd.exe".to_string());
67+
(s, vec![])
68+
}
69+
};
70+
#[cfg(not(target_os = "windows"))]
71+
let (shell, login_args): (String, Vec<String>) = {
72+
let s = std::env::var("SHELL").unwrap_or_else(|_| "/bin/bash".to_string());
73+
(s, vec!["-l".to_string()]) // login shell for proper PATH
74+
};
75+
6176
let mut cmd = CommandBuilder::new(&shell);
62-
cmd.arg("-l"); // login shell for proper PATH
77+
for arg in &login_args {
78+
cmd.arg(arg);
79+
}
6380

81+
// Platform-appropriate home directory
82+
#[cfg(target_os = "windows")]
83+
let home = std::env::var("USERPROFILE").unwrap_or_else(|_| "C:\\".to_string());
84+
#[cfg(not(target_os = "windows"))]
6485
let home = std::env::var("HOME").unwrap_or_else(|_| "/".to_string());
86+
6587
cmd.cwd(cwd.unwrap_or(home));
6688

67-
// Inherit common env vars
68-
for key in &[
89+
// Inherit platform-specific env vars
90+
#[cfg(target_os = "windows")]
91+
let env_keys: &[&str] = &[
92+
"USERPROFILE",
93+
"USERNAME",
94+
"COMSPEC",
95+
"PATH",
96+
"SystemRoot",
97+
"APPDATA",
98+
"LOCALAPPDATA",
99+
"TEMP",
100+
"TMP",
101+
"CARGO_HOME",
102+
"RUSTUP_HOME",
103+
];
104+
#[cfg(not(target_os = "windows"))]
105+
let env_keys: &[&str] = &[
69106
"HOME",
70107
"USER",
71108
"SHELL",
@@ -76,7 +113,9 @@ pub fn create_terminal(
76113
"NVM_DIR",
77114
"CARGO_HOME",
78115
"RUSTUP_HOME",
79-
] {
116+
];
117+
118+
for key in env_keys {
80119
if let Ok(val) = std::env::var(key) {
81120
cmd.env(key, val);
82121
}

0 commit comments

Comments
 (0)