1#![allow(
2 clippy::disallowed_methods,
3 reason = "We are not in an async environment, so std::process::Command is fine"
4)]
5#![cfg_attr(
6 any(target_os = "linux", target_os = "freebsd", target_os = "windows"),
7 allow(dead_code)
8)]
9
10use anyhow::{Context as _, Result};
11use clap::Parser;
12use cli::{CliRequest, CliResponse, IpcHandshake, ipc::IpcOneShotServer};
13use parking_lot::Mutex;
14use std::{
15 collections::{BTreeMap, BTreeSet},
16 env,
17 ffi::OsStr,
18 fs, io,
19 path::{Path, PathBuf},
20 process::ExitStatus,
21 sync::Arc,
22 thread::{self, JoinHandle},
23};
24use tempfile::{NamedTempFile, TempDir};
25use util::paths::PathWithPosition;
26use walkdir::WalkDir;
27
28#[cfg(any(target_os = "linux", target_os = "freebsd"))]
29use std::io::IsTerminal;
30
31const URL_PREFIX: [&'static str; 5] = ["zed://", "http://", "https://", "file://", "ssh://"];
32
33struct Detect;
34
35trait InstalledApp {
36 fn zed_version_string(&self) -> String;
37 fn launch(&self, ipc_url: String, user_data_dir: Option<&str>) -> anyhow::Result<()>;
38 fn run_foreground(
39 &self,
40 ipc_url: String,
41 user_data_dir: Option<&str>,
42 ) -> io::Result<ExitStatus>;
43 fn path(&self) -> PathBuf;
44}
45
46#[derive(Parser, Debug)]
47#[command(
48 name = "zed",
49 disable_version_flag = true,
50 before_help = "The Zed CLI binary.
51This CLI is a separate binary that invokes Zed.
52
53Examples:
54 `zed`
55 Simply opens Zed
56 `zed --foreground`
57 Runs in foreground (shows all logs)
58 `zed path-to-your-project`
59 Open your project in Zed
60 `zed -n path-to-file `
61 Open file/folder in a new window",
62 after_help = "To read from stdin, append '-', e.g. 'ps axf | zed -'"
63)]
64struct Args {
65 /// Wait for all of the given paths to be opened/closed before exiting.
66 ///
67 /// When opening a directory, waits until the created window is closed.
68 #[arg(short, long)]
69 wait: bool,
70 /// Add files to the currently open workspace
71 #[arg(short, long, overrides_with_all = ["new", "reuse"])]
72 add: bool,
73 /// Create a new workspace
74 #[arg(short, long, overrides_with_all = ["add", "reuse"])]
75 new: bool,
76 /// Reuse an existing window, replacing its workspace
77 #[arg(short, long, overrides_with_all = ["add", "new"])]
78 reuse: bool,
79 /// Sets a custom directory for all user data (e.g., database, extensions, logs).
80 /// This overrides the default platform-specific data directory location:
81 #[cfg_attr(target_os = "macos", doc = "`~/Library/Application Support/Zed`.")]
82 #[cfg_attr(target_os = "windows", doc = "`%LOCALAPPDATA%\\Zed`.")]
83 #[cfg_attr(
84 not(any(target_os = "windows", target_os = "macos")),
85 doc = "`$XDG_DATA_HOME/zed`."
86 )]
87 #[arg(long, value_name = "DIR")]
88 user_data_dir: Option<String>,
89 /// The paths to open in Zed (space-separated).
90 ///
91 /// Use `path:line:column` syntax to open a file at the given line and column.
92 paths_with_position: Vec<String>,
93 /// Print Zed's version and the app path.
94 #[arg(short, long)]
95 version: bool,
96 /// Run zed in the foreground (useful for debugging)
97 #[arg(long)]
98 foreground: bool,
99 /// Custom path to Zed.app or the zed binary
100 #[arg(long)]
101 zed: Option<PathBuf>,
102 /// Run zed in dev-server mode
103 #[arg(long)]
104 dev_server_token: Option<String>,
105 /// The username and WSL distribution to use when opening paths. If not specified,
106 /// Zed will attempt to open the paths directly.
107 ///
108 /// The username is optional, and if not specified, the default user for the distribution
109 /// will be used.
110 ///
111 /// Example: `me@Ubuntu` or `Ubuntu`.
112 ///
113 /// WARN: You should not fill in this field by hand.
114 #[cfg(target_os = "windows")]
115 #[arg(long, value_name = "USER@DISTRO")]
116 wsl: Option<String>,
117 /// Not supported in Zed CLI, only supported on Zed binary
118 /// Will attempt to give the correct command to run
119 #[arg(long)]
120 system_specs: bool,
121 /// Open the project in a dev container.
122 ///
123 /// Automatically triggers "Reopen in Dev Container" if a `.devcontainer/`
124 /// configuration is found in the project directory.
125 #[arg(long)]
126 dev_container: bool,
127 /// Pairs of file paths to diff. Can be specified multiple times.
128 /// When directories are provided, recurses into them and shows all changed files in a single multi-diff view.
129 #[arg(long, action = clap::ArgAction::Append, num_args = 2, value_names = ["OLD_PATH", "NEW_PATH"])]
130 diff: Vec<String>,
131 /// Uninstall Zed from user system
132 #[cfg(all(
133 any(target_os = "linux", target_os = "macos"),
134 not(feature = "no-bundled-uninstall")
135 ))]
136 #[arg(long)]
137 uninstall: bool,
138
139 /// Used for SSH/Git password authentication, to remove the need for netcat as a dependency,
140 /// by having Zed act like netcat communicating over a Unix socket.
141 #[arg(long, hide = true)]
142 askpass: Option<String>,
143}
144
145/// Parses a path containing a position (e.g. `path:line:column`)
146/// and returns its canonicalized string representation.
147///
148/// If a part of path doesn't exist, it will canonicalize the
149/// existing part and append the non-existing part.
150///
151/// This method must return an absolute path, as many zed
152/// crates assume absolute paths.
153fn parse_path_with_position(argument_str: &str) -> anyhow::Result<String> {
154 match Path::new(argument_str).canonicalize() {
155 Ok(existing_path) => Ok(PathWithPosition::from_path(existing_path)),
156 Err(_) => PathWithPosition::parse_str(argument_str).map_path(|mut path| {
157 let curdir = env::current_dir().context("retrieving current directory")?;
158 let mut children = Vec::new();
159 let root;
160 loop {
161 // canonicalize handles './', and '/'.
162 if let Ok(canonicalized) = fs::canonicalize(&path) {
163 root = canonicalized;
164 break;
165 }
166 // The comparison to `curdir` is just a shortcut
167 // since we know it is canonical. The other one
168 // is if `argument_str` is a string that starts
169 // with a name (e.g. "foo/bar").
170 if path == curdir || path == Path::new("") {
171 root = curdir;
172 break;
173 }
174 children.push(
175 path.file_name()
176 .with_context(|| format!("parsing as path with position {argument_str}"))?
177 .to_owned(),
178 );
179 if !path.pop() {
180 unreachable!("parsing as path with position {argument_str}");
181 }
182 }
183 Ok(children.iter().rev().fold(root, |mut path, child| {
184 path.push(child);
185 path
186 }))
187 }),
188 }
189 .map(|path_with_pos| path_with_pos.to_string(&|path| path.to_string_lossy().into_owned()))
190}
191
192fn expand_directory_diff_pairs(
193 diff_pairs: Vec<[String; 2]>,
194) -> anyhow::Result<(Vec<[String; 2]>, Vec<TempDir>)> {
195 let mut expanded = Vec::new();
196 let mut temp_dirs = Vec::new();
197
198 for pair in diff_pairs {
199 let left = PathBuf::from(&pair[0]);
200 let right = PathBuf::from(&pair[1]);
201
202 if left.is_dir() && right.is_dir() {
203 let (mut pairs, temp_dir) = expand_directory_pair(&left, &right)?;
204 expanded.append(&mut pairs);
205 if let Some(temp_dir) = temp_dir {
206 temp_dirs.push(temp_dir);
207 }
208 } else {
209 expanded.push(pair);
210 }
211 }
212
213 Ok((expanded, temp_dirs))
214}
215
216fn expand_directory_pair(
217 left: &Path,
218 right: &Path,
219) -> anyhow::Result<(Vec<[String; 2]>, Option<TempDir>)> {
220 let left_files = collect_files(left)?;
221 let right_files = collect_files(right)?;
222
223 let mut rel_paths = BTreeSet::new();
224 rel_paths.extend(left_files.keys().cloned());
225 rel_paths.extend(right_files.keys().cloned());
226
227 let mut temp_dir = TempDir::new()?;
228 let mut temp_dir_used = false;
229 let mut pairs = Vec::new();
230
231 for rel in rel_paths {
232 match (left_files.get(&rel), right_files.get(&rel)) {
233 (Some(left_path), Some(right_path)) => {
234 pairs.push([
235 left_path.to_string_lossy().into_owned(),
236 right_path.to_string_lossy().into_owned(),
237 ]);
238 }
239 (Some(left_path), None) => {
240 let stub = create_empty_stub(&mut temp_dir, &rel)?;
241 temp_dir_used = true;
242 pairs.push([
243 left_path.to_string_lossy().into_owned(),
244 stub.to_string_lossy().into_owned(),
245 ]);
246 }
247 (None, Some(right_path)) => {
248 let stub = create_empty_stub(&mut temp_dir, &rel)?;
249 temp_dir_used = true;
250 pairs.push([
251 stub.to_string_lossy().into_owned(),
252 right_path.to_string_lossy().into_owned(),
253 ]);
254 }
255 (None, None) => {}
256 }
257 }
258
259 let temp_dir = if temp_dir_used { Some(temp_dir) } else { None };
260 Ok((pairs, temp_dir))
261}
262
263fn collect_files(root: &Path) -> anyhow::Result<BTreeMap<PathBuf, PathBuf>> {
264 let mut files = BTreeMap::new();
265
266 for entry in WalkDir::new(root) {
267 let entry = entry?;
268 if entry.file_type().is_file() {
269 let rel = entry
270 .path()
271 .strip_prefix(root)
272 .context("stripping directory prefix")?
273 .to_path_buf();
274 files.insert(rel, entry.into_path());
275 }
276 }
277
278 Ok(files)
279}
280
281fn create_empty_stub(temp_dir: &mut TempDir, rel: &Path) -> anyhow::Result<PathBuf> {
282 let stub_path = temp_dir.path().join(rel);
283 if let Some(parent) = stub_path.parent() {
284 fs::create_dir_all(parent)?;
285 }
286 fs::File::create(&stub_path)?;
287 Ok(stub_path)
288}
289
290#[cfg(test)]
291mod tests {
292 use super::*;
293 use serde_json::json;
294 use util::path;
295 use util::paths::SanitizedPath;
296 use util::test::TempTree;
297
298 macro_rules! assert_path_eq {
299 ($left:expr, $right:expr) => {
300 assert_eq!(
301 SanitizedPath::new(Path::new(&$left)),
302 SanitizedPath::new(Path::new(&$right))
303 )
304 };
305 }
306
307 fn cwd() -> PathBuf {
308 env::current_dir().unwrap()
309 }
310
311 static CWD_LOCK: Mutex<()> = Mutex::new(());
312
313 fn with_cwd<T>(path: &Path, f: impl FnOnce() -> anyhow::Result<T>) -> anyhow::Result<T> {
314 let _lock = CWD_LOCK.lock();
315 let old_cwd = cwd();
316 env::set_current_dir(path)?;
317 let result = f();
318 env::set_current_dir(old_cwd)?;
319 result
320 }
321
322 #[test]
323 fn test_parse_non_existing_path() {
324 // Absolute path
325 let result = parse_path_with_position(path!("/non/existing/path.txt")).unwrap();
326 assert_path_eq!(result, path!("/non/existing/path.txt"));
327
328 // Absolute path in cwd
329 let path = cwd().join(path!("non/existing/path.txt"));
330 let expected = path.to_string_lossy().to_string();
331 let result = parse_path_with_position(&expected).unwrap();
332 assert_path_eq!(result, expected);
333
334 // Relative path
335 let result = parse_path_with_position(path!("non/existing/path.txt")).unwrap();
336 assert_path_eq!(result, expected)
337 }
338
339 #[test]
340 fn test_parse_existing_path() {
341 let temp_tree = TempTree::new(json!({
342 "file.txt": "",
343 }));
344 let file_path = temp_tree.path().join("file.txt");
345 let expected = file_path.to_string_lossy().to_string();
346
347 // Absolute path
348 let result = parse_path_with_position(file_path.to_str().unwrap()).unwrap();
349 assert_path_eq!(result, expected);
350
351 // Relative path
352 let result = with_cwd(temp_tree.path(), || parse_path_with_position("file.txt")).unwrap();
353 assert_path_eq!(result, expected);
354 }
355
356 // NOTE:
357 // While POSIX symbolic links are somewhat supported on Windows, they are an opt in by the user, and thus
358 // we assume that they are not supported out of the box.
359 #[cfg(not(windows))]
360 #[test]
361 fn test_parse_symlink_file() {
362 let temp_tree = TempTree::new(json!({
363 "target.txt": "",
364 }));
365 let target_path = temp_tree.path().join("target.txt");
366 let symlink_path = temp_tree.path().join("symlink.txt");
367 std::os::unix::fs::symlink(&target_path, &symlink_path).unwrap();
368
369 // Absolute path
370 let result = parse_path_with_position(symlink_path.to_str().unwrap()).unwrap();
371 assert_eq!(result, target_path.to_string_lossy());
372
373 // Relative path
374 let result =
375 with_cwd(temp_tree.path(), || parse_path_with_position("symlink.txt")).unwrap();
376 assert_eq!(result, target_path.to_string_lossy());
377 }
378
379 #[cfg(not(windows))]
380 #[test]
381 fn test_parse_symlink_dir() {
382 let temp_tree = TempTree::new(json!({
383 "some": {
384 "dir": { // symlink target
385 "ec": {
386 "tory": {
387 "file.txt": "",
388 }}}}}));
389
390 let target_file_path = temp_tree.path().join("some/dir/ec/tory/file.txt");
391 let expected = target_file_path.to_string_lossy();
392
393 let dir_path = temp_tree.path().join("some/dir");
394 let symlink_path = temp_tree.path().join("symlink");
395 std::os::unix::fs::symlink(&dir_path, &symlink_path).unwrap();
396
397 // Absolute path
398 let result =
399 parse_path_with_position(symlink_path.join("ec/tory/file.txt").to_str().unwrap())
400 .unwrap();
401 assert_eq!(result, expected);
402
403 // Relative path
404 let result = with_cwd(temp_tree.path(), || {
405 parse_path_with_position("symlink/ec/tory/file.txt")
406 })
407 .unwrap();
408 assert_eq!(result, expected);
409 }
410}
411
412fn parse_path_in_wsl(source: &str, wsl: &str) -> Result<String> {
413 let mut source = PathWithPosition::parse_str(source);
414
415 let (user, distro_name) = if let Some((user, distro)) = wsl.split_once('@') {
416 if user.is_empty() {
417 anyhow::bail!("user is empty in wsl argument");
418 }
419 (Some(user), distro)
420 } else {
421 (None, wsl)
422 };
423
424 let mut args = vec!["--distribution", distro_name];
425 if let Some(user) = user {
426 args.push("--user");
427 args.push(user);
428 }
429
430 let command = [
431 OsStr::new("realpath"),
432 OsStr::new("-s"),
433 source.path.as_ref(),
434 ];
435
436 let output = util::command::new_std_command("wsl.exe")
437 .args(&args)
438 .arg("--exec")
439 .args(&command)
440 .output()?;
441 let result = if output.status.success() {
442 String::from_utf8_lossy(&output.stdout).to_string()
443 } else {
444 let fallback = util::command::new_std_command("wsl.exe")
445 .args(&args)
446 .arg("--")
447 .args(&command)
448 .output()?;
449 String::from_utf8_lossy(&fallback.stdout).to_string()
450 };
451
452 source.path = Path::new(result.trim()).to_owned();
453
454 Ok(source.to_string(&|path| path.to_string_lossy().into_owned()))
455}
456
457fn main() -> Result<()> {
458 #[cfg(unix)]
459 util::prevent_root_execution();
460
461 // Exit flatpak sandbox if needed
462 #[cfg(target_os = "linux")]
463 {
464 flatpak::try_restart_to_host();
465 flatpak::ld_extra_libs();
466 }
467
468 // Intercept version designators
469 #[cfg(target_os = "macos")]
470 if let Some(channel) = std::env::args().nth(1).filter(|arg| arg.starts_with("--")) {
471 // When the first argument is a name of a release channel, we're going to spawn off the CLI of that version, with trailing args passed along.
472 use std::str::FromStr as _;
473
474 if let Ok(channel) = release_channel::ReleaseChannel::from_str(&channel[2..]) {
475 return mac_os::spawn_channel_cli(channel, std::env::args().skip(2).collect());
476 }
477 }
478 let args = Args::parse();
479
480 // `zed --askpass` Makes zed operate in nc/netcat mode for use with askpass
481 if let Some(socket) = &args.askpass {
482 askpass::main(socket);
483 return Ok(());
484 }
485
486 // Set custom data directory before any path operations
487 let user_data_dir = args.user_data_dir.clone();
488 if let Some(dir) = &user_data_dir {
489 paths::set_custom_data_dir(dir);
490 }
491
492 #[cfg(target_os = "linux")]
493 let args = flatpak::set_bin_if_no_escape(args);
494
495 let app = Detect::detect(args.zed.as_deref()).context("Bundle detection")?;
496
497 if args.version {
498 println!("{}", app.zed_version_string());
499 return Ok(());
500 }
501
502 if args.system_specs {
503 let path = app.path();
504 let msg = [
505 "The `--system-specs` argument is not supported in the Zed CLI, only on Zed binary.",
506 "To retrieve the system specs on the command line, run the following command:",
507 &format!("{} --system-specs", path.display()),
508 ];
509 anyhow::bail!(msg.join("\n"));
510 }
511
512 #[cfg(all(
513 any(target_os = "linux", target_os = "macos"),
514 not(feature = "no-bundled-uninstall")
515 ))]
516 if args.uninstall {
517 static UNINSTALL_SCRIPT: &[u8] = include_bytes!("../../../script/uninstall.sh");
518
519 let tmp_dir = tempfile::tempdir()?;
520 let script_path = tmp_dir.path().join("uninstall.sh");
521 fs::write(&script_path, UNINSTALL_SCRIPT)?;
522
523 use std::os::unix::fs::PermissionsExt as _;
524 fs::set_permissions(&script_path, fs::Permissions::from_mode(0o755))?;
525
526 let status = std::process::Command::new("sh")
527 .arg(&script_path)
528 .env("ZED_CHANNEL", &*release_channel::RELEASE_CHANNEL_NAME)
529 .status()
530 .context("Failed to execute uninstall script")?;
531
532 std::process::exit(status.code().unwrap_or(1));
533 }
534
535 let (server, server_name) =
536 IpcOneShotServer::<IpcHandshake>::new().context("Handshake before Zed spawn")?;
537 let url = format!("zed-cli://{server_name}");
538
539 let open_new_workspace = if args.new {
540 Some(true)
541 } else if args.add {
542 Some(false)
543 } else {
544 None
545 };
546
547 let env = {
548 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
549 {
550 use collections::HashMap;
551
552 // On Linux, the desktop entry uses `cli` to spawn `zed`.
553 // We need to handle env vars correctly since std::env::vars() may not contain
554 // project-specific vars (e.g. those set by direnv).
555 // By setting env to None here, the LSP will use worktree env vars instead,
556 // which is what we want.
557 if !std::io::stdout().is_terminal() {
558 None
559 } else {
560 Some(std::env::vars().collect::<HashMap<_, _>>())
561 }
562 }
563
564 #[cfg(target_os = "windows")]
565 {
566 // On Windows, by default, a child process inherits a copy of the environment block of the parent process.
567 // So we don't need to pass env vars explicitly.
568 None
569 }
570
571 #[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "windows")))]
572 {
573 use collections::HashMap;
574
575 Some(std::env::vars().collect::<HashMap<_, _>>())
576 }
577 };
578
579 let exit_status = Arc::new(Mutex::new(None));
580 let mut paths = vec![];
581 let mut urls = vec![];
582 let mut diff_paths = vec![];
583 let mut stdin_tmp_file: Option<fs::File> = None;
584 let mut anonymous_fd_tmp_files = vec![];
585
586 // Check if any diff paths are directories to determine diff_all mode
587 let diff_all_mode = args
588 .diff
589 .chunks(2)
590 .any(|pair| Path::new(&pair[0]).is_dir() || Path::new(&pair[1]).is_dir());
591
592 for path in args.diff.chunks(2) {
593 diff_paths.push([
594 parse_path_with_position(&path[0])?,
595 parse_path_with_position(&path[1])?,
596 ]);
597 }
598
599 let (expanded_diff_paths, temp_dirs) = expand_directory_diff_pairs(diff_paths)?;
600 diff_paths = expanded_diff_paths;
601 // Prevent automatic cleanup of temp directories containing empty stub files
602 // for directory diffs. The CLI process may exit before Zed has read these
603 // files (e.g., when RPC-ing into an already-running instance). The files
604 // live in the OS temp directory and will be cleaned up on reboot.
605 for temp_dir in temp_dirs {
606 let _ = temp_dir.keep();
607 }
608
609 #[cfg(target_os = "windows")]
610 let wsl = args.wsl.as_ref();
611 #[cfg(not(target_os = "windows"))]
612 let wsl = None;
613
614 for path in args.paths_with_position.iter() {
615 if URL_PREFIX.iter().any(|&prefix| path.starts_with(prefix)) {
616 urls.push(path.to_string());
617 } else if path == "-" && args.paths_with_position.len() == 1 {
618 let file = NamedTempFile::new()?;
619 paths.push(file.path().to_string_lossy().into_owned());
620 let (file, _) = file.keep()?;
621 stdin_tmp_file = Some(file);
622 } else if let Some(file) = anonymous_fd(path) {
623 let tmp_file = NamedTempFile::new()?;
624 paths.push(tmp_file.path().to_string_lossy().into_owned());
625 let (tmp_file, _) = tmp_file.keep()?;
626 anonymous_fd_tmp_files.push((file, tmp_file));
627 } else if let Some(wsl) = wsl {
628 urls.push(format!("file://{}", parse_path_in_wsl(path, wsl)?));
629 } else {
630 paths.push(parse_path_with_position(path)?);
631 }
632 }
633
634 // When only diff paths are provided (no regular paths), add the current
635 // working directory so the workspace opens with the right context.
636 if paths.is_empty() && urls.is_empty() && !diff_paths.is_empty() {
637 if let Ok(cwd) = env::current_dir() {
638 paths.push(cwd.to_string_lossy().into_owned());
639 }
640 }
641
642 anyhow::ensure!(
643 args.dev_server_token.is_none(),
644 "Dev servers were removed in v0.157.x please upgrade to SSH remoting: https://zed.dev/docs/remote-development"
645 );
646
647 rayon::ThreadPoolBuilder::new()
648 .num_threads(4)
649 .stack_size(10 * 1024 * 1024)
650 .thread_name(|ix| format!("RayonWorker{}", ix))
651 .build_global()
652 .unwrap();
653
654 let sender: JoinHandle<anyhow::Result<()>> = thread::Builder::new()
655 .name("CliReceiver".to_string())
656 .spawn({
657 let exit_status = exit_status.clone();
658 let user_data_dir_for_thread = user_data_dir.clone();
659 move || {
660 let (_, handshake) = server.accept().context("Handshake after Zed spawn")?;
661 let (tx, rx) = (handshake.requests, handshake.responses);
662
663 #[cfg(target_os = "windows")]
664 let wsl = args.wsl;
665 #[cfg(not(target_os = "windows"))]
666 let wsl = None;
667
668 tx.send(CliRequest::Open {
669 paths,
670 urls,
671 diff_paths,
672 diff_all: diff_all_mode,
673 wsl,
674 wait: args.wait,
675 open_new_workspace,
676 reuse: args.reuse,
677 env,
678 user_data_dir: user_data_dir_for_thread,
679 dev_container: args.dev_container,
680 })?;
681
682 while let Ok(response) = rx.recv() {
683 match response {
684 CliResponse::Ping => {}
685 CliResponse::Stdout { message } => println!("{message}"),
686 CliResponse::Stderr { message } => eprintln!("{message}"),
687 CliResponse::Exit { status } => {
688 exit_status.lock().replace(status);
689 return Ok(());
690 }
691 }
692 }
693
694 Ok(())
695 }
696 })
697 .unwrap();
698
699 let stdin_pipe_handle: Option<JoinHandle<anyhow::Result<()>>> =
700 stdin_tmp_file.map(|mut tmp_file| {
701 thread::Builder::new()
702 .name("CliStdin".to_string())
703 .spawn(move || {
704 let mut stdin = std::io::stdin().lock();
705 if !io::IsTerminal::is_terminal(&stdin) {
706 io::copy(&mut stdin, &mut tmp_file)?;
707 }
708 Ok(())
709 })
710 .unwrap()
711 });
712
713 let anonymous_fd_pipe_handles: Vec<_> = anonymous_fd_tmp_files
714 .into_iter()
715 .map(|(mut file, mut tmp_file)| {
716 thread::Builder::new()
717 .name("CliAnonymousFd".to_string())
718 .spawn(move || io::copy(&mut file, &mut tmp_file))
719 .unwrap()
720 })
721 .collect();
722
723 if args.foreground {
724 app.run_foreground(url, user_data_dir.as_deref())?;
725 } else {
726 app.launch(url, user_data_dir.as_deref())?;
727 sender.join().unwrap()?;
728 if let Some(handle) = stdin_pipe_handle {
729 handle.join().unwrap()?;
730 }
731 for handle in anonymous_fd_pipe_handles {
732 handle.join().unwrap()?;
733 }
734 }
735
736 if let Some(exit_status) = exit_status.lock().take() {
737 std::process::exit(exit_status);
738 }
739 Ok(())
740}
741
742fn anonymous_fd(path: &str) -> Option<fs::File> {
743 #[cfg(target_os = "linux")]
744 {
745 use std::os::fd::{self, FromRawFd};
746
747 let fd_str = path.strip_prefix("/proc/self/fd/")?;
748
749 let link = fs::read_link(path).ok()?;
750 if !link.starts_with("memfd:") {
751 return None;
752 }
753
754 let fd: fd::RawFd = fd_str.parse().ok()?;
755 let file = unsafe { fs::File::from_raw_fd(fd) };
756 Some(file)
757 }
758 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
759 {
760 use std::os::{
761 fd::{self, FromRawFd},
762 unix::fs::FileTypeExt,
763 };
764
765 let fd_str = path.strip_prefix("/dev/fd/")?;
766
767 let metadata = fs::metadata(path).ok()?;
768 let file_type = metadata.file_type();
769 if !file_type.is_fifo() && !file_type.is_socket() {
770 return None;
771 }
772 let fd: fd::RawFd = fd_str.parse().ok()?;
773 let file = unsafe { fs::File::from_raw_fd(fd) };
774 Some(file)
775 }
776 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "freebsd")))]
777 {
778 _ = path;
779 // not implemented for bsd, windows. Could be, but isn't yet
780 None
781 }
782}
783
784#[cfg(any(target_os = "linux", target_os = "freebsd"))]
785mod linux {
786 use std::{
787 env,
788 ffi::OsString,
789 io,
790 os::unix::net::{SocketAddr, UnixDatagram},
791 path::{Path, PathBuf},
792 process::{self, ExitStatus},
793 thread,
794 time::Duration,
795 };
796
797 use anyhow::{Context as _, anyhow};
798 use cli::FORCE_CLI_MODE_ENV_VAR_NAME;
799 use fork::Fork;
800
801 use crate::{Detect, InstalledApp};
802
803 struct App(PathBuf);
804
805 impl Detect {
806 pub fn detect(path: Option<&Path>) -> anyhow::Result<impl InstalledApp> {
807 let path = if let Some(path) = path {
808 path.to_path_buf().canonicalize()?
809 } else {
810 let cli = env::current_exe()?;
811 let dir = cli.parent().context("no parent path for cli")?;
812
813 // libexec is the standard, lib/zed is for Arch (and other non-libexec distros),
814 // ./zed is for the target directory in development builds.
815 let possible_locations =
816 ["../libexec/zed-editor", "../lib/zed/zed-editor", "./zed"];
817 possible_locations
818 .iter()
819 .find_map(|p| dir.join(p).canonicalize().ok().filter(|path| path != &cli))
820 .with_context(|| {
821 format!("could not find any of: {}", possible_locations.join(", "))
822 })?
823 };
824
825 Ok(App(path))
826 }
827 }
828
829 impl InstalledApp for App {
830 fn zed_version_string(&self) -> String {
831 format!(
832 "Zed {}{}{} – {}",
833 if *release_channel::RELEASE_CHANNEL_NAME == "stable" {
834 "".to_string()
835 } else {
836 format!("{} ", *release_channel::RELEASE_CHANNEL_NAME)
837 },
838 option_env!("RELEASE_VERSION").unwrap_or_default(),
839 match option_env!("ZED_COMMIT_SHA") {
840 Some(commit_sha) => format!(" {commit_sha} "),
841 None => "".to_string(),
842 },
843 self.0.display(),
844 )
845 }
846
847 fn launch(&self, ipc_url: String, user_data_dir: Option<&str>) -> anyhow::Result<()> {
848 let data_dir = user_data_dir
849 .map(PathBuf::from)
850 .unwrap_or_else(|| paths::data_dir().clone());
851
852 let sock_path = data_dir.join(format!(
853 "zed-{}.sock",
854 *release_channel::RELEASE_CHANNEL_NAME
855 ));
856 let sock = UnixDatagram::unbound()?;
857 if sock.connect(&sock_path).is_err() {
858 self.boot_background(ipc_url, user_data_dir)?;
859 } else {
860 sock.send(ipc_url.as_bytes())?;
861 }
862 Ok(())
863 }
864
865 fn run_foreground(
866 &self,
867 ipc_url: String,
868 user_data_dir: Option<&str>,
869 ) -> io::Result<ExitStatus> {
870 let mut cmd = std::process::Command::new(self.0.clone());
871 cmd.arg(ipc_url);
872 if let Some(dir) = user_data_dir {
873 cmd.arg("--user-data-dir").arg(dir);
874 }
875 cmd.status()
876 }
877
878 fn path(&self) -> PathBuf {
879 self.0.clone()
880 }
881 }
882
883 impl App {
884 fn boot_background(
885 &self,
886 ipc_url: String,
887 user_data_dir: Option<&str>,
888 ) -> anyhow::Result<()> {
889 let path = &self.0;
890
891 match fork::fork() {
892 Ok(Fork::Parent(_)) => Ok(()),
893 Ok(Fork::Child) => {
894 unsafe { std::env::set_var(FORCE_CLI_MODE_ENV_VAR_NAME, "") };
895 if fork::setsid().is_err() {
896 eprintln!("failed to setsid: {}", std::io::Error::last_os_error());
897 process::exit(1);
898 }
899 if fork::close_fd().is_err() {
900 eprintln!("failed to close_fd: {}", std::io::Error::last_os_error());
901 }
902 let mut args: Vec<OsString> =
903 vec![path.as_os_str().to_owned(), OsString::from(ipc_url)];
904 if let Some(dir) = user_data_dir {
905 args.push(OsString::from("--user-data-dir"));
906 args.push(OsString::from(dir));
907 }
908 let error = exec::execvp(path.clone(), &args);
909 // if exec succeeded, we never get here.
910 eprintln!("failed to exec {:?}: {}", path, error);
911 process::exit(1)
912 }
913 Err(_) => Err(anyhow!(io::Error::last_os_error())),
914 }
915 }
916
917 fn wait_for_socket(
918 &self,
919 sock_addr: &SocketAddr,
920 sock: &mut UnixDatagram,
921 ) -> Result<(), std::io::Error> {
922 for _ in 0..100 {
923 thread::sleep(Duration::from_millis(10));
924 if sock.connect_addr(sock_addr).is_ok() {
925 return Ok(());
926 }
927 }
928 sock.connect_addr(sock_addr)
929 }
930 }
931}
932
933#[cfg(target_os = "linux")]
934mod flatpak {
935 use std::ffi::OsString;
936 use std::path::PathBuf;
937 use std::process::Command;
938 use std::{env, process};
939
940 const EXTRA_LIB_ENV_NAME: &str = "ZED_FLATPAK_LIB_PATH";
941 const NO_ESCAPE_ENV_NAME: &str = "ZED_FLATPAK_NO_ESCAPE";
942
943 /// Adds bundled libraries to LD_LIBRARY_PATH if running under flatpak
944 pub fn ld_extra_libs() {
945 let mut paths = if let Ok(paths) = env::var("LD_LIBRARY_PATH") {
946 env::split_paths(&paths).collect()
947 } else {
948 Vec::new()
949 };
950
951 if let Ok(extra_path) = env::var(EXTRA_LIB_ENV_NAME) {
952 paths.push(extra_path.into());
953 }
954
955 unsafe { env::set_var("LD_LIBRARY_PATH", env::join_paths(paths).unwrap()) };
956 }
957
958 /// Restarts outside of the sandbox if currently running within it
959 pub fn try_restart_to_host() {
960 if let Some(flatpak_dir) = get_flatpak_dir() {
961 let mut args = vec!["/usr/bin/flatpak-spawn".into(), "--host".into()];
962 args.append(&mut get_xdg_env_args());
963 args.push("--env=ZED_UPDATE_EXPLANATION=Please use flatpak to update zed".into());
964 args.push(
965 format!(
966 "--env={EXTRA_LIB_ENV_NAME}={}",
967 flatpak_dir.join("lib").to_str().unwrap()
968 )
969 .into(),
970 );
971 args.push(flatpak_dir.join("bin").join("zed").into());
972
973 let mut is_app_location_set = false;
974 for arg in &env::args_os().collect::<Vec<_>>()[1..] {
975 args.push(arg.clone());
976 is_app_location_set |= arg == "--zed";
977 }
978
979 if !is_app_location_set {
980 args.push("--zed".into());
981 args.push(flatpak_dir.join("libexec").join("zed-editor").into());
982 }
983
984 let error = exec::execvp("/usr/bin/flatpak-spawn", args);
985 eprintln!("failed restart cli on host: {:?}", error);
986 process::exit(1);
987 }
988 }
989
990 pub fn set_bin_if_no_escape(mut args: super::Args) -> super::Args {
991 if env::var(NO_ESCAPE_ENV_NAME).is_ok()
992 && env::var("FLATPAK_ID").is_ok_and(|id| id.starts_with("dev.zed.Zed"))
993 && args.zed.is_none()
994 {
995 args.zed = Some("/app/libexec/zed-editor".into());
996 unsafe { env::set_var("ZED_UPDATE_EXPLANATION", "Please use flatpak to update zed") };
997 }
998 args
999 }
1000
1001 fn get_flatpak_dir() -> Option<PathBuf> {
1002 if env::var(NO_ESCAPE_ENV_NAME).is_ok() {
1003 return None;
1004 }
1005
1006 if let Ok(flatpak_id) = env::var("FLATPAK_ID") {
1007 if !flatpak_id.starts_with("dev.zed.Zed") {
1008 return None;
1009 }
1010
1011 let install_dir = Command::new("/usr/bin/flatpak-spawn")
1012 .arg("--host")
1013 .arg("flatpak")
1014 .arg("info")
1015 .arg("--show-location")
1016 .arg(flatpak_id)
1017 .output()
1018 .unwrap();
1019 let install_dir = PathBuf::from(String::from_utf8(install_dir.stdout).unwrap().trim());
1020 Some(install_dir.join("files"))
1021 } else {
1022 None
1023 }
1024 }
1025
1026 fn get_xdg_env_args() -> Vec<OsString> {
1027 let xdg_keys = [
1028 "XDG_DATA_HOME",
1029 "XDG_CONFIG_HOME",
1030 "XDG_CACHE_HOME",
1031 "XDG_STATE_HOME",
1032 ];
1033 env::vars()
1034 .filter(|(key, _)| xdg_keys.contains(&key.as_str()))
1035 .map(|(key, val)| format!("--env=FLATPAK_{}={}", key, val).into())
1036 .collect()
1037 }
1038}
1039
1040#[cfg(target_os = "windows")]
1041mod windows {
1042 use anyhow::Context;
1043 use release_channel::app_identifier;
1044 use windows::{
1045 Win32::{
1046 Foundation::{CloseHandle, ERROR_ALREADY_EXISTS, GENERIC_WRITE, GetLastError},
1047 Storage::FileSystem::{
1048 CreateFileW, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_MODE, OPEN_EXISTING, WriteFile,
1049 },
1050 System::Threading::CreateMutexW,
1051 },
1052 core::HSTRING,
1053 };
1054
1055 use crate::{Detect, InstalledApp};
1056 use std::io;
1057 use std::path::{Path, PathBuf};
1058 use std::process::ExitStatus;
1059
1060 fn check_single_instance() -> bool {
1061 let mutex = unsafe {
1062 CreateMutexW(
1063 None,
1064 false,
1065 &HSTRING::from(format!("{}-Instance-Mutex", app_identifier())),
1066 )
1067 .expect("Unable to create instance sync event")
1068 };
1069 let last_err = unsafe { GetLastError() };
1070 let _ = unsafe { CloseHandle(mutex) };
1071 last_err != ERROR_ALREADY_EXISTS
1072 }
1073
1074 struct App(PathBuf);
1075
1076 impl InstalledApp for App {
1077 fn zed_version_string(&self) -> String {
1078 format!(
1079 "Zed {}{}{} – {}",
1080 if *release_channel::RELEASE_CHANNEL_NAME == "stable" {
1081 "".to_string()
1082 } else {
1083 format!("{} ", *release_channel::RELEASE_CHANNEL_NAME)
1084 },
1085 option_env!("RELEASE_VERSION").unwrap_or_default(),
1086 match option_env!("ZED_COMMIT_SHA") {
1087 Some(commit_sha) => format!(" {commit_sha} "),
1088 None => "".to_string(),
1089 },
1090 self.0.display(),
1091 )
1092 }
1093
1094 fn launch(&self, ipc_url: String, user_data_dir: Option<&str>) -> anyhow::Result<()> {
1095 if check_single_instance() {
1096 let mut cmd = std::process::Command::new(self.0.clone());
1097 cmd.arg(ipc_url);
1098 if let Some(dir) = user_data_dir {
1099 cmd.arg("--user-data-dir").arg(dir);
1100 }
1101 cmd.spawn()?;
1102 } else {
1103 unsafe {
1104 let pipe = CreateFileW(
1105 &HSTRING::from(format!("\\\\.\\pipe\\{}-Named-Pipe", app_identifier())),
1106 GENERIC_WRITE.0,
1107 FILE_SHARE_MODE::default(),
1108 None,
1109 OPEN_EXISTING,
1110 FILE_FLAGS_AND_ATTRIBUTES::default(),
1111 None,
1112 )?;
1113 let message = ipc_url.as_bytes();
1114 let mut bytes_written = 0;
1115 WriteFile(pipe, Some(message), Some(&mut bytes_written), None)?;
1116 CloseHandle(pipe)?;
1117 }
1118 }
1119 Ok(())
1120 }
1121
1122 fn run_foreground(
1123 &self,
1124 ipc_url: String,
1125 user_data_dir: Option<&str>,
1126 ) -> io::Result<ExitStatus> {
1127 let mut cmd = std::process::Command::new(self.0.clone());
1128 cmd.arg(ipc_url).arg("--foreground");
1129 if let Some(dir) = user_data_dir {
1130 cmd.arg("--user-data-dir").arg(dir);
1131 }
1132 cmd.spawn()?.wait()
1133 }
1134
1135 fn path(&self) -> PathBuf {
1136 self.0.clone()
1137 }
1138 }
1139
1140 impl Detect {
1141 pub fn detect(path: Option<&Path>) -> anyhow::Result<impl InstalledApp> {
1142 let path = if let Some(path) = path {
1143 path.to_path_buf().canonicalize()?
1144 } else {
1145 let cli = std::env::current_exe()?;
1146 let dir = cli.parent().context("no parent path for cli")?;
1147
1148 // ../Zed.exe is the standard, lib/zed is for MSYS2, ./zed.exe is for the target
1149 // directory in development builds.
1150 let possible_locations = ["../Zed.exe", "../lib/zed/zed-editor.exe", "./zed.exe"];
1151 possible_locations
1152 .iter()
1153 .find_map(|p| dir.join(p).canonicalize().ok().filter(|path| path != &cli))
1154 .context(format!(
1155 "could not find any of: {}",
1156 possible_locations.join(", ")
1157 ))?
1158 };
1159
1160 Ok(App(path))
1161 }
1162 }
1163}
1164
1165#[cfg(target_os = "macos")]
1166mod mac_os {
1167 use anyhow::{Context as _, Result};
1168 use core_foundation::{
1169 array::{CFArray, CFIndex},
1170 base::TCFType as _,
1171 string::kCFStringEncodingUTF8,
1172 url::{CFURL, CFURLCreateWithBytes},
1173 };
1174 use core_services::{LSLaunchURLSpec, LSOpenFromURLSpec, kLSLaunchDefaults};
1175 use serde::Deserialize;
1176 use std::{
1177 ffi::OsStr,
1178 fs, io,
1179 path::{Path, PathBuf},
1180 process::{Command, ExitStatus},
1181 ptr,
1182 };
1183
1184 use cli::FORCE_CLI_MODE_ENV_VAR_NAME;
1185
1186 use crate::{Detect, InstalledApp};
1187
1188 #[derive(Debug, Deserialize)]
1189 struct InfoPlist {
1190 #[serde(rename = "CFBundleShortVersionString")]
1191 bundle_short_version_string: String,
1192 }
1193
1194 enum Bundle {
1195 App {
1196 app_bundle: PathBuf,
1197 plist: InfoPlist,
1198 },
1199 LocalPath {
1200 executable: PathBuf,
1201 },
1202 }
1203
1204 fn locate_bundle() -> Result<PathBuf> {
1205 let cli_path = std::env::current_exe()?.canonicalize()?;
1206 let mut app_path = cli_path.clone();
1207 while app_path.extension() != Some(OsStr::new("app")) {
1208 anyhow::ensure!(
1209 app_path.pop(),
1210 "cannot find app bundle containing {cli_path:?}"
1211 );
1212 }
1213 Ok(app_path)
1214 }
1215
1216 impl Detect {
1217 pub fn detect(path: Option<&Path>) -> anyhow::Result<impl InstalledApp> {
1218 let bundle_path = if let Some(bundle_path) = path {
1219 bundle_path
1220 .canonicalize()
1221 .with_context(|| format!("Args bundle path {bundle_path:?} canonicalization"))?
1222 } else {
1223 locate_bundle().context("bundle autodiscovery")?
1224 };
1225
1226 match bundle_path.extension().and_then(|ext| ext.to_str()) {
1227 Some("app") => {
1228 let plist_path = bundle_path.join("Contents/Info.plist");
1229 let plist =
1230 plist::from_file::<_, InfoPlist>(&plist_path).with_context(|| {
1231 format!("Reading *.app bundle plist file at {plist_path:?}")
1232 })?;
1233 Ok(Bundle::App {
1234 app_bundle: bundle_path,
1235 plist,
1236 })
1237 }
1238 _ => Ok(Bundle::LocalPath {
1239 executable: bundle_path,
1240 }),
1241 }
1242 }
1243 }
1244
1245 impl InstalledApp for Bundle {
1246 fn zed_version_string(&self) -> String {
1247 format!("Zed {} – {}", self.version(), self.path().display(),)
1248 }
1249
1250 fn launch(&self, url: String, user_data_dir: Option<&str>) -> anyhow::Result<()> {
1251 match self {
1252 Self::App { app_bundle, .. } => {
1253 let app_path = app_bundle;
1254
1255 let status = unsafe {
1256 let app_url = CFURL::from_path(app_path, true)
1257 .with_context(|| format!("invalid app path {app_path:?}"))?;
1258 let url_to_open = CFURL::wrap_under_create_rule(CFURLCreateWithBytes(
1259 ptr::null(),
1260 url.as_ptr(),
1261 url.len() as CFIndex,
1262 kCFStringEncodingUTF8,
1263 ptr::null(),
1264 ));
1265 // equivalent to: open zed-cli:... -a /Applications/Zed\ Preview.app
1266 let urls_to_open =
1267 CFArray::from_copyable(&[url_to_open.as_concrete_TypeRef()]);
1268 LSOpenFromURLSpec(
1269 &LSLaunchURLSpec {
1270 appURL: app_url.as_concrete_TypeRef(),
1271 itemURLs: urls_to_open.as_concrete_TypeRef(),
1272 passThruParams: ptr::null(),
1273 launchFlags: kLSLaunchDefaults,
1274 asyncRefCon: ptr::null_mut(),
1275 },
1276 ptr::null_mut(),
1277 )
1278 };
1279
1280 anyhow::ensure!(
1281 status == 0,
1282 "cannot start app bundle {}",
1283 self.zed_version_string()
1284 );
1285 }
1286
1287 Self::LocalPath { executable, .. } => {
1288 let executable_parent = executable
1289 .parent()
1290 .with_context(|| format!("Executable {executable:?} path has no parent"))?;
1291 let subprocess_stdout_file = fs::File::create(
1292 executable_parent.join("zed_dev.log"),
1293 )
1294 .with_context(|| format!("Log file creation in {executable_parent:?}"))?;
1295 let subprocess_stdin_file =
1296 subprocess_stdout_file.try_clone().with_context(|| {
1297 format!("Cloning descriptor for file {subprocess_stdout_file:?}")
1298 })?;
1299 let mut command = std::process::Command::new(executable);
1300 command.env(FORCE_CLI_MODE_ENV_VAR_NAME, "");
1301 if let Some(dir) = user_data_dir {
1302 command.arg("--user-data-dir").arg(dir);
1303 }
1304 command
1305 .stderr(subprocess_stdout_file)
1306 .stdout(subprocess_stdin_file)
1307 .arg(url);
1308
1309 command
1310 .spawn()
1311 .with_context(|| format!("Spawning {command:?}"))?;
1312 }
1313 }
1314
1315 Ok(())
1316 }
1317
1318 fn run_foreground(
1319 &self,
1320 ipc_url: String,
1321 user_data_dir: Option<&str>,
1322 ) -> io::Result<ExitStatus> {
1323 let path = match self {
1324 Bundle::App { app_bundle, .. } => app_bundle.join("Contents/MacOS/zed"),
1325 Bundle::LocalPath { executable, .. } => executable.clone(),
1326 };
1327
1328 let mut cmd = std::process::Command::new(path);
1329 cmd.arg(ipc_url);
1330 if let Some(dir) = user_data_dir {
1331 cmd.arg("--user-data-dir").arg(dir);
1332 }
1333 cmd.status()
1334 }
1335
1336 fn path(&self) -> PathBuf {
1337 match self {
1338 Bundle::App { app_bundle, .. } => app_bundle.join("Contents/MacOS/zed"),
1339 Bundle::LocalPath { executable, .. } => executable.clone(),
1340 }
1341 }
1342 }
1343
1344 impl Bundle {
1345 fn version(&self) -> String {
1346 match self {
1347 Self::App { plist, .. } => plist.bundle_short_version_string.clone(),
1348 Self::LocalPath { .. } => "<development>".to_string(),
1349 }
1350 }
1351
1352 fn path(&self) -> &Path {
1353 match self {
1354 Self::App { app_bundle, .. } => app_bundle,
1355 Self::LocalPath { executable, .. } => executable,
1356 }
1357 }
1358 }
1359
1360 pub(super) fn spawn_channel_cli(
1361 channel: release_channel::ReleaseChannel,
1362 leftover_args: Vec<String>,
1363 ) -> Result<()> {
1364 use anyhow::bail;
1365
1366 let app_path_prompt = format!(
1367 "POSIX path of (path to application \"{}\")",
1368 channel.display_name()
1369 );
1370 let app_path_output = Command::new("osascript")
1371 .arg("-e")
1372 .arg(&app_path_prompt)
1373 .output()?;
1374 if !app_path_output.status.success() {
1375 bail!(
1376 "Could not determine app path for {}",
1377 channel.display_name()
1378 );
1379 }
1380 let app_path = String::from_utf8(app_path_output.stdout)?.trim().to_owned();
1381 let cli_path = format!("{app_path}/Contents/MacOS/cli");
1382 Command::new(cli_path).args(leftover_args).spawn()?;
1383 Ok(())
1384 }
1385}