Skip to content

Commit 5e82c75

Browse files
committed
fix(env): gate PowerShell/cmd.exe shell detection to Windows only
`detect_shell()` checked for `PSModulePath` to detect PowerShell, but on macOS this variable can be set by Homebrew pwsh even in bash/zsh, causing `vp env use` to output PowerShell-style commands. Gate both `PSModulePath` and `COMSPEC` checks behind `cfg!(windows)` so non-Windows shells default to POSIX after ruling out fish.
1 parent 45fe81e commit 5e82c75

1 file changed

Lines changed: 53 additions & 2 deletions

File tree

  • crates/vite_global_cli/src/commands/env

crates/vite_global_cli/src/commands/env/use.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ enum Shell {
3131
fn detect_shell() -> Shell {
3232
if std::env::var("FISH_VERSION").is_ok() {
3333
Shell::Fish
34-
} else if std::env::var("PSModulePath").is_ok() {
34+
} else if cfg!(windows) && std::env::var("PSModulePath").is_ok() {
3535
Shell::PowerShell
36-
} else if std::env::var("COMSPEC").is_ok() {
36+
} else if cfg!(windows) {
3737
Shell::Cmd
3838
} else {
3939
Shell::Posix
@@ -136,8 +136,59 @@ pub async fn execute(
136136

137137
#[cfg(test)]
138138
mod tests {
139+
use serial_test::serial;
140+
139141
use super::*;
140142

143+
#[test]
144+
#[serial]
145+
fn test_detect_shell_posix_even_with_psmodulepath() {
146+
// SAFETY: This test runs in isolation with serial_test
147+
unsafe {
148+
std::env::remove_var("FISH_VERSION");
149+
std::env::set_var("PSModulePath", "/some/path");
150+
}
151+
let shell = detect_shell();
152+
#[cfg(not(windows))]
153+
assert!(matches!(shell, Shell::Posix));
154+
#[cfg(windows)]
155+
assert!(matches!(shell, Shell::PowerShell));
156+
// Cleanup
157+
unsafe {
158+
std::env::remove_var("PSModulePath");
159+
}
160+
}
161+
162+
#[test]
163+
#[serial]
164+
fn test_detect_shell_fish() {
165+
// SAFETY: This test runs in isolation with serial_test
166+
unsafe {
167+
std::env::set_var("FISH_VERSION", "3.7.0");
168+
std::env::remove_var("PSModulePath");
169+
}
170+
let shell = detect_shell();
171+
assert!(matches!(shell, Shell::Fish));
172+
// Cleanup
173+
unsafe {
174+
std::env::remove_var("FISH_VERSION");
175+
}
176+
}
177+
178+
#[test]
179+
#[serial]
180+
fn test_detect_shell_posix_default() {
181+
// SAFETY: This test runs in isolation with serial_test
182+
unsafe {
183+
std::env::remove_var("FISH_VERSION");
184+
std::env::remove_var("PSModulePath");
185+
std::env::remove_var("COMSPEC");
186+
}
187+
let shell = detect_shell();
188+
#[cfg(not(windows))]
189+
assert!(matches!(shell, Shell::Posix));
190+
}
191+
141192
#[test]
142193
fn test_format_export_posix() {
143194
let result = format_export(&Shell::Posix, "20.18.0");

0 commit comments

Comments
 (0)