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