Use ShellKind::try_quote whenever we need to quote shell args (#41104)

Re-reverts
https://github.com/zed-industries/zed/commit/8f4646d6c357409cfc286104088b4d21f2bea851
with fixes

Release Notes:

- N/A
This commit is contained in:
Jakub Konka
2025-10-24 18:19:53 +02:00
committed by GitHub
parent f213f4bcc8
commit bcbc6a330e
22 changed files with 310 additions and 228 deletions
+12 -22
View File
@@ -15,7 +15,7 @@ use std::{
sync::LazyLock,
};
use crate::rel_path::RelPath;
use crate::{rel_path::RelPath, shell::ShellKind};
static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
@@ -84,9 +84,7 @@ pub trait PathExt {
fn multiple_extensions(&self) -> Option<String>;
/// Try to make a shell-safe representation of the path.
///
/// For Unix, the path is escaped to be safe for POSIX shells
fn try_shell_safe(&self) -> anyhow::Result<String>;
fn try_shell_safe(&self, shell_kind: ShellKind) -> anyhow::Result<String>;
}
impl<T: AsRef<Path>> PathExt for T {
@@ -164,24 +162,16 @@ impl<T: AsRef<Path>> PathExt for T {
Some(parts.into_iter().join("."))
}
fn try_shell_safe(&self) -> anyhow::Result<String> {
#[cfg(target_os = "windows")]
{
Ok(self.as_ref().to_string_lossy().to_string())
}
#[cfg(not(target_os = "windows"))]
{
let path_str = self
.as_ref()
.to_str()
.with_context(|| "Path contains invalid UTF-8")?;
// As of writing, this can only be fail if the path contains a null byte, which shouldn't be possible
// but shlex has annotated the error as #[non_exhaustive] so we can't make it a compile error if other
// errors are introduced in the future :(
Ok(shlex::try_quote(path_str)?.into_owned())
}
fn try_shell_safe(&self, shell_kind: ShellKind) -> anyhow::Result<String> {
let path_str = self
.as_ref()
.to_str()
.with_context(|| "Path contains invalid UTF-8")?;
shell_kind
.try_quote(path_str)
.as_deref()
.map(ToOwned::to_owned)
.context("Failed to quote path")
}
}