Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix binary name resolution against custom PATH on macOS (#55672)
G36maidd1f500edf15b
diff --git a/crates/util/src/command/darwin.rs b/crates/util/src/command/darwin.rsindex cf99eada331..7b47795efa9 100644--- a/crates/util/src/command/darwin.rs+++ b/crates/util/src/command/darwin.rs@@ -8,7 +8,7 @@ use std::collections::BTreeMap;use std::ffi::{CString, OsStr, OsString};use std::io;use std::os::fd::AsRawFd;-use std::os::unix::ffi::OsStrExt;+use std::os::unix::ffi::{OsStrExt, OsStringExt};use std::os::unix::io::FromRawFd;use std::path::{Path, PathBuf};use std::process::{ExitStatus, Output};@@ -300,12 +300,30 @@ fn spawn_posix_spawn(stderr_cfg: Stdio,kill_on_drop: bool,) -> io::Result<Child> {- let program_cstr = CString::new(program.as_bytes()).map_err(|_| invalid_input_error())?;+ // posix_spawnp resolves programs against the parent's cwd/PATH, not the child's.+ let resolved_program = if program.as_bytes().contains(&b'/') {+ std::path::absolute(current_dir.join(program)).map_or_else(+ |_| program.as_bytes().to_vec(),+ |p| p.into_os_string().into_vec(),+ )+ } else {+ envs.and_then(|e| {+ e.iter()+ .find(|(k, _)| k.as_os_str() == OsStr::new("PATH"))+ .and_then(|(_, v)| which::which_in(program, Some(v.as_os_str()), current_dir).ok())+ })+ .map_or_else(+ || program.as_bytes().to_vec(),+ |path| path.into_os_string().into_vec(),+ )+ };+ let program_cstr = CString::new(resolved_program).map_err(|_| invalid_input_error())?;+ let argv0_cstr = CString::new(program.as_bytes()).map_err(|_| invalid_input_error())?;let current_dir_cstr =CString::new(current_dir.as_os_str().as_bytes()).map_err(|_| invalid_input_error())?;- let mut argv_cstrs = vec![program_cstr.clone()];+ let mut argv_cstrs = vec![argv0_cstr];for arg in args {let cstr = CString::new(arg.as_bytes()).map_err(|_| invalid_input_error())?;argv_cstrs.push(cstr);@@ -909,6 +927,114 @@ mod tests {});}+ #[test]+ fn test_bare_program_resolved_via_custom_path() {+ let temp_dir = tempfile::tempdir().expect("failed to create temp dir");++ let link_path = temp_dir.path().join("zed-test-echo");+ std::os::unix::fs::symlink("/bin/echo", &link_path).expect("failed to create symlink");++ let custom_path = temp_dir.path().to_string_lossy().into_owned();++ smol::block_on(async {+ let output = Command::new("zed-test-echo")+ .args(["-n", "from-custom-path"])+ .env("PATH", &custom_path)+ .output()+ .await+ .expect("failed to spawn with custom PATH");++ assert!(output.status.success());+ assert_eq!(output.stdout, b"from-custom-path");+ });+ }++ #[test]+ fn test_bare_program_preserves_argv0_when_resolved_via_custom_path() {+ let temp_dir = tempfile::tempdir().expect("failed to create temp dir");++ let link_path = temp_dir.path().join("zed-test-sh");+ std::os::unix::fs::symlink("/bin/sh", &link_path).expect("failed to create symlink");++ let custom_path = temp_dir.path().to_string_lossy().into_owned();++ smol::block_on(async {+ let output = Command::new("zed-test-sh")+ .args(["-c", "printf %s \"$0\""])+ .env("PATH", &custom_path)+ .output()+ .await+ .expect("failed to spawn with custom PATH");++ assert!(output.status.success());+ assert_eq!(output.stdout, b"zed-test-sh");+ });+ }++ #[test]+ fn test_bare_program_with_custom_path_falls_back_when_not_found() {+ smol::block_on(async {+ let result = Command::new("zed-nonexistent-binary-xyz")+ .env("PATH", "/nonexistent/path")+ .spawn();++ assert!(result.is_err());+ });+ }++ #[test]+ fn test_bare_program_with_custom_env_no_path_key() {+ smol::block_on(async {+ let output = Command::new("echo")+ .args(["-n", "from-inherited-path"])+ .env("ZED_TEST_VAR", "test")+ .output()+ .await+ .expect("failed to spawn with custom env but no PATH override");++ assert!(output.status.success());+ assert_eq!(output.stdout, b"from-inherited-path");+ });+ }++ #[test]+ fn test_relative_path_resolved_against_current_dir() {+ let temp_dir = tempfile::tempdir().expect("failed to create temp dir");++ let link_path = temp_dir.path().join("zed-test-echo");+ std::os::unix::fs::symlink("/bin/echo", &link_path).expect("failed to create symlink");++ let relative_path = "./zed-test-echo";++ smol::block_on(async {+ let output = Command::new(relative_path)+ .args(["-n", "from-relative-path"])+ .current_dir(temp_dir.path())+ .env("PATH", "/nonexistent/path")+ .output()+ .await+ .expect("failed to spawn with relative path");++ assert!(output.status.success());+ assert_eq!(output.stdout, b"from-relative-path");+ });+ }++ #[test]+ fn test_absolute_path_passes_through_unchanged() {+ smol::block_on(async {+ let output = Command::new("/bin/echo")+ .args(["-n", "from-absolute-path"])+ .env("PATH", "/nonexistent/path")+ .output()+ .await+ .expect("failed to spawn with absolute path");++ assert!(output.status.success());+ assert_eq!(output.stdout, b"from-absolute-path");+ });+ }+#[test]fn test_stdio_inherit_keeps_stdio_open() {smol::block_on(async {