Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Improve Windows shell discovery (#63339)

John Tur4c6c4750d3f8
diff --git a/crates/gpui_util/src/lib.rs b/crates/gpui_util/src/lib.rs
index 44637123912..109ef6f16b7 100644
--- a/crates/gpui_util/src/lib.rs
+++ b/crates/gpui_util/src/lib.rs
@@ -32,7 +32,7 @@ pub fn new_std_command(program: impl AsRef<OsStr>) -> std::process::Command {
}
#[cfg(target_os = "windows")]
-pub fn get_windows_system_shell() -> String {
+pub fn get_powershell() -> Option<String> {
use std::path::PathBuf;
fn find_pwsh_in_programfiles(find_alternate: bool, find_preview: bool) -> Option<PathBuf> {
@@ -71,7 +71,7 @@ pub fn get_windows_system_shell() -> String {
};
let exe_path = entry.path().join("pwsh.exe");
- if exe_path.exists() {
+ if exe_path.is_file() {
Some((version, exe_path))
} else {
None
@@ -84,41 +84,34 @@ pub fn get_windows_system_shell() -> String {
fn find_pwsh_in_msix(find_preview: bool) -> Option<PathBuf> {
let msix_app_dir =
PathBuf::from(std::env::var_os("LOCALAPPDATA")?).join("Microsoft\\WindowsApps");
- if !msix_app_dir.exists() {
- return None;
- }
-
- let prefix = if find_preview {
- "Microsoft.PowerShellPreview_"
+ let package_family_name = if find_preview {
+ "Microsoft.PowerShellPreview_8wekyb3d8bbwe"
} else {
- "Microsoft.PowerShell_"
+ "Microsoft.PowerShell_8wekyb3d8bbwe"
};
- msix_app_dir
- .read_dir()
- .ok()?
- .filter_map(|entry| {
- let entry = entry.ok()?;
- if !matches!(entry.file_type(), Ok(ft) if ft.is_dir()) {
- return None;
- }
-
- if !entry.file_name().to_string_lossy().starts_with(prefix) {
- return None;
- }
-
- let exe_path = entry.path().join("pwsh.exe");
- exe_path.exists().then_some(exe_path)
- })
- .next()
+ let pwsh_exe = msix_app_dir.join(package_family_name).join("pwsh.exe");
+ pwsh_exe.exists().then_some(pwsh_exe)
}
fn find_pwsh_in_scoop() -> Option<PathBuf> {
let pwsh_exe =
PathBuf::from(std::env::var_os("USERPROFILE")?).join("scoop\\shims\\pwsh.exe");
- pwsh_exe.exists().then_some(pwsh_exe)
+ pwsh_exe.is_file().then_some(pwsh_exe)
+ }
+
+ fn find_pwsh_in_dotnet_tools() -> Option<PathBuf> {
+ let pwsh_exe =
+ PathBuf::from(std::env::var_os("USERPROFILE")?).join(".dotnet\\tools\\pwsh.exe");
+ pwsh_exe.is_file().then_some(pwsh_exe)
+ }
+
+ fn find_windows_powershell() -> Option<PathBuf> {
+ let system_root = PathBuf::from(std::env::var_os("SystemRoot")?);
+ let powershell = system_root.join("System32\\WindowsPowerShell\\v1.0\\powershell.exe");
+ powershell.is_file().then_some(powershell)
}
- static SYSTEM_SHELL: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
+ static POWERSHELL: std::sync::LazyLock<Option<String>> = std::sync::LazyLock::new(|| {
let locations = [
|| find_pwsh_in_programfiles(false, false),
|| find_pwsh_in_programfiles(true, false),
@@ -127,8 +120,10 @@ pub fn get_windows_system_shell() -> String {
|| find_pwsh_in_msix(true),
|| find_pwsh_in_programfiles(true, true),
|| find_pwsh_in_scoop(),
+ || find_pwsh_in_dotnet_tools(),
|| which::which_global("pwsh.exe").ok(),
|| which::which_global("powershell.exe").ok(),
+ || find_windows_powershell(),
];
locations
@@ -136,13 +131,22 @@ pub fn get_windows_system_shell() -> String {
.find_map(|f| f())
.map(|p| p.to_string_lossy().trim().to_owned())
.inspect(|shell| log::info!("Found powershell in: {}", shell))
- .unwrap_or_else(|| {
- log::warn!("Powershell not found, falling back to `cmd`");
- "cmd.exe".to_string()
- })
});
- (*SYSTEM_SHELL).clone()
+ (*POWERSHELL).clone()
+}
+
+#[cfg(target_os = "windows")]
+pub fn get_windows_system_shell() -> String {
+ static CMD: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
+ log::warn!("Powershell not found, falling back to `cmd`");
+ let system_root = std::env::var_os("SystemRoot").unwrap_or_else(|| "C:\\Windows".into());
+ std::path::PathBuf::from(system_root)
+ .join("System32\\cmd.exe")
+ .to_string_lossy()
+ .into_owned()
+ });
+ get_powershell().unwrap_or_else(|| (*CMD).clone())
}
pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
diff --git a/crates/gpui_windows/src/platform.rs b/crates/gpui_windows/src/platform.rs
index 9147ce51f2b..d0ae3c764fe 100644
--- a/crates/gpui_windows/src/platform.rs
+++ b/crates/gpui_windows/src/platform.rs
@@ -12,7 +12,7 @@ use std::{
use anyhow::{Context as _, Result, anyhow};
use futures::channel::oneshot::{self, Receiver};
-use gpui_util::{ResultExt, get_windows_system_shell, new_std_command};
+use gpui_util::{ResultExt, get_powershell, new_std_command};
use itertools::Itertools;
use parking_lot::RwLock;
use smallvec::SmallVec;
@@ -507,9 +507,13 @@ impl Platform for WindowsPlatform {
// can pump the Win32 message loop (via `CreateProcessW`), which
// re-enters message handling possibly resulting in another mutable
// borrow of the `AppCell` ending up with a double borrow panic
+ let Some(powershell) = get_powershell() else {
+ log::error!("failed to restart: PowerShell is unavailable");
+ return;
+ };
self.foreground_executor
.spawn(async move {
- let mut command = new_std_command(get_windows_system_shell());
+ let mut command = new_std_command(powershell);
let arguments = encode_restart_arguments(&arguments);
command
.arg("-command")
diff --git a/crates/util/src/shell.rs b/crates/util/src/shell.rs
index e9b6d054996..985d352b656 100644
--- a/crates/util/src/shell.rs
+++ b/crates/util/src/shell.rs
@@ -1,6 +1,8 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
-use std::{borrow::Cow, fmt, path::Path, sync::LazyLock};
+use std::{borrow::Cow, fmt, path::Path};
+#[cfg(windows)]
+use std::{path::PathBuf, sync::LazyLock};
/// Shell configuration to open the terminal with.
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema, Hash)]
@@ -84,33 +86,42 @@ pub fn get_default_system_shell() -> String {
/// Get the default system shell, preferring bash on Windows.
pub fn get_default_system_shell_preferring_bash() -> String {
- if cfg!(windows) {
+ #[cfg(windows)]
+ {
get_windows_bash().unwrap_or_else(|| get_windows_system_shell())
- } else {
+ }
+
+ #[cfg(not(windows))]
+ {
"/bin/sh".to_string()
}
}
+#[cfg(windows)]
pub fn get_windows_bash() -> Option<String> {
- use std::path::PathBuf;
-
- fn find_bash_in_scoop() -> Option<PathBuf> {
- let bash_exe =
- PathBuf::from(std::env::var_os("USERPROFILE")?).join("scoop\\shims\\bash.exe");
- bash_exe.exists().then_some(bash_exe)
+ fn find_bash_in_installation(install_root: &Path) -> Option<PathBuf> {
+ if !install_root.join("git-bash.exe").is_file() {
+ return None;
+ }
+ let bash = install_root.join("bin").join("bash.exe");
+ bash.is_file().then_some(bash)
}
fn find_bash_in_git() -> Option<PathBuf> {
- // /path/to/git/cmd/git.exe/../../bin/bash.exe
+ if let Some(bash) = std::env::var_os("GIT_INSTALL_ROOT")
+ .map(PathBuf::from)
+ .and_then(|path| find_bash_in_installation(&path))
+ {
+ return Some(bash);
+ }
let git = which::which("git").ok()?;
- let git_bash = git.parent()?.parent()?.join("bin").join("bash.exe");
- git_bash.exists().then_some(git_bash)
+ let binary_directory = git.parent()?;
+ let parent = binary_directory.parent()?;
+ find_bash_in_installation(parent).or_else(|| find_bash_in_installation(parent.parent()?))
}
static BASH: LazyLock<Option<String>> = LazyLock::new(|| {
- let bash = find_bash_in_scoop()
- .or_else(|| find_bash_in_git())
- .map(|p| p.to_string_lossy().into_owned());
+ let bash = find_bash_in_git().map(|p| p.to_string_lossy().into_owned());
if let Some(ref path) = bash {
log::info!("Found bash at {}", path);
}