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 anyhow::ensure!(
646 args.dev_server_token.is_none(),
647 "Dev servers were removed in v0.157.x please upgrade to SSH remoting: https://zed.dev/docs/remote-development"
648 );
649
650 rayon::ThreadPoolBuilder::new()
651 .num_threads(4)
652 .stack_size(10 * 1024 * 1024)
653 .thread_name(|ix| format!("RayonWorker{}", ix))
654 .build_global()
655 .unwrap();
656
657 let sender: JoinHandle<anyhow::Result<()>> = thread::Builder::new()
658 .name("CliReceiver".to_string())
659 .spawn({
660 let exit_status = exit_status.clone();
661 let user_data_dir_for_thread = user_data_dir.clone();
662 move || {
663 let (_, handshake) = server.accept().context("Handshake after Zed spawn")?;
664 let (tx, rx) = (handshake.requests, handshake.responses);
665
666 #[cfg(target_os = "windows")]
667 let wsl = args.wsl;
668 #[cfg(not(target_os = "windows"))]
669 let wsl = None;
670
671 let open_request = CliRequest::Open {
672 paths,
673 urls,
674 diff_paths,
675 diff_all: diff_all_mode,
676 wsl,
677 wait: args.wait,
678 open_behavior,
679 env,
680 user_data_dir: user_data_dir_for_thread,
681 dev_container: args.dev_container,
682 };
683
684 tx.send(open_request)?;
685
686 while let Ok(response) = rx.recv() {
687 match response {
688 CliResponse::Ping => {}
689 CliResponse::Stdout { message } => println!("{message}"),
690 CliResponse::Stderr { message } => eprintln!("{message}"),
691 CliResponse::Exit { status } => {
692 exit_status.lock().replace(status);
693 return Ok(());
694 }
695 CliResponse::PromptOpenBehavior => {
696 let behavior = prompt_open_behavior()
697 .unwrap_or(cli::CliBehaviorSetting::ExistingWindow);
698 tx.send(CliRequest::SetOpenBehavior { behavior })?;
699 }
700 }
701 }
702
703 Ok(())
704 }
705 })
706 .unwrap();
707
708 let stdin_pipe_handle: Option<JoinHandle<anyhow::Result<()>>> =
709 stdin_tmp_file.map(|mut tmp_file| {
710 thread::Builder::new()
711 .name("CliStdin".to_string())
712 .spawn(move || {
713 let mut stdin = std::io::stdin().lock();
714 if !io::IsTerminal::is_terminal(&stdin) {
715 io::copy(&mut stdin, &mut tmp_file)?;
716 }
717 Ok(())
718 })
719 .unwrap()
720 });
721
722 let anonymous_fd_pipe_handles: Vec<_> = anonymous_fd_tmp_files
723 .into_iter()
724 .map(|(mut file, mut tmp_file)| {
725 thread::Builder::new()
726 .name("CliAnonymousFd".to_string())
727 .spawn(move || io::copy(&mut file, &mut tmp_file))
728 .unwrap()
729 })
730 .collect();
731
732 if args.foreground {
733 app.run_foreground(url, user_data_dir.as_deref())?;
734 } else {
735 app.launch(url, user_data_dir.as_deref())?;
736 sender.join().unwrap()?;
737 if let Some(handle) = stdin_pipe_handle {
738 handle.join().unwrap()?;
739 }
740 for handle in anonymous_fd_pipe_handles {
741 handle.join().unwrap()?;
742 }
743 }
744
745 if let Some(exit_status) = exit_status.lock().take() {
746 std::process::exit(exit_status);
747 }
748 Ok(())
749}
750
751fn anonymous_fd(path: &str) -> Option<fs::File> {
752 #[cfg(target_os = "linux")]
753 {
754 use std::os::fd::{self, FromRawFd};
755
756 let fd_str = path.strip_prefix("/proc/self/fd/")?;
757
758 let link = fs::read_link(path).ok()?;
759 if !link.starts_with("memfd:") {
760 return None;
761 }
762
763 let fd: fd::RawFd = fd_str.parse().ok()?;
764 let file = unsafe { fs::File::from_raw_fd(fd) };
765 Some(file)
766 }
767 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
768 {
769 use std::os::{
770 fd::{self, FromRawFd},
771 unix::fs::FileTypeExt,
772 };
773
774 let fd_str = path.strip_prefix("/dev/fd/")?;
775
776 let metadata = fs::metadata(path).ok()?;
777 let file_type = metadata.file_type();
778 if !file_type.is_fifo() && !file_type.is_socket() {
779 return None;
780 }
781 let fd: fd::RawFd = fd_str.parse().ok()?;
782 let file = unsafe { fs::File::from_raw_fd(fd) };
783 Some(file)
784 }
785 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "freebsd")))]
786 {
787 _ = path;
788 // not implemented for bsd, windows. Could be, but isn't yet
789 None
790 }
791}
792
793/// Shows an interactive prompt asking the user to choose the default open
794/// behavior for `zed <path>`. Returns `None` if the prompt cannot be shown
795/// (e.g. stdin is not a terminal) or the user cancels.
796fn prompt_open_behavior() -> Option<cli::CliBehaviorSetting> {
797 if !std::io::stdin().is_terminal() {
798 return None;
799 }
800
801 let blue = console::Style::new().blue();
802 let items = [
803 format!(
804 "Add to existing Zed window ({})",
805 blue.apply_to("zed --existing")
806 ),
807 format!("Open a new window ({})", blue.apply_to("zed --classic")),
808 ];
809
810 let prompt = format!(
811 "Configure default behavior for {}\n{}",
812 blue.apply_to("zed <path>"),
813 console::style("You can change this later in Zed settings"),
814 );
815
816 let selection = dialoguer::Select::new()
817 .with_prompt(&prompt)
818 .items(&items)
819 .default(0)
820 .interact()
821 .ok()?;
822
823 Some(if selection == 0 {
824 cli::CliBehaviorSetting::ExistingWindow
825 } else {
826 cli::CliBehaviorSetting::NewWindow
827 })
828}
829
830#[cfg(any(target_os = "linux", target_os = "freebsd"))]
831mod linux {
832 use std::{
833 env,
834 ffi::OsString,
835 io,
836 os::unix::net::{SocketAddr, UnixDatagram},
837 path::{Path, PathBuf},
838 process::{self, ExitStatus},
839 thread,
840 time::Duration,
841 };
842
843 use anyhow::{Context as _, anyhow};
844 use cli::FORCE_CLI_MODE_ENV_VAR_NAME;
845 use fork::Fork;
846
847 use crate::{Detect, InstalledApp};
848
849 struct App(PathBuf);
850
851 impl Detect {
852 pub fn detect(path: Option<&Path>) -> anyhow::Result<impl InstalledApp> {
853 let path = if let Some(path) = path {
854 path.to_path_buf().canonicalize()?
855 } else {
856 let cli = env::current_exe()?;
857 let dir = cli.parent().context("no parent path for cli")?;
858
859 // libexec is the standard, lib/zed is for Arch (and other non-libexec distros),
860 // ./zed is for the target directory in development builds.
861 let possible_locations =
862 ["../libexec/zed-editor", "../lib/zed/zed-editor", "./zed"];
863 possible_locations
864 .iter()
865 .find_map(|p| dir.join(p).canonicalize().ok().filter(|path| path != &cli))
866 .with_context(|| {
867 format!("could not find any of: {}", possible_locations.join(", "))
868 })?
869 };
870
871 Ok(App(path))
872 }
873 }
874
875 impl InstalledApp for App {
876 fn zed_version_string(&self) -> String {
877 format!(
878 "Zed {}{}{} – {}",
879 if *release_channel::RELEASE_CHANNEL_NAME == "stable" {
880 "".to_string()
881 } else {
882 format!("{} ", *release_channel::RELEASE_CHANNEL_NAME)
883 },
884 option_env!("RELEASE_VERSION").unwrap_or_default(),
885 match option_env!("ZED_COMMIT_SHA") {
886 Some(commit_sha) => format!(" {commit_sha} "),
887 None => "".to_string(),
888 },
889 self.0.display(),
890 )
891 }
892
893 fn launch(&self, ipc_url: String, user_data_dir: Option<&str>) -> anyhow::Result<()> {
894 let data_dir = user_data_dir
895 .map(PathBuf::from)
896 .unwrap_or_else(|| paths::data_dir().clone());
897
898 let sock_path = data_dir.join(format!(
899 "zed-{}.sock",
900 *release_channel::RELEASE_CHANNEL_NAME
901 ));
902 let sock = UnixDatagram::unbound()?;
903 if sock.connect(&sock_path).is_err() {
904 self.boot_background(ipc_url, user_data_dir)?;
905 } else {
906 sock.send(ipc_url.as_bytes())?;
907 }
908 Ok(())
909 }
910
911 fn run_foreground(
912 &self,
913 ipc_url: String,
914 user_data_dir: Option<&str>,
915 ) -> io::Result<ExitStatus> {
916 let mut cmd = std::process::Command::new(self.0.clone());
917 cmd.arg(ipc_url);
918 if let Some(dir) = user_data_dir {
919 cmd.arg("--user-data-dir").arg(dir);
920 }
921 cmd.status()
922 }
923
924 fn path(&self) -> PathBuf {
925 self.0.clone()
926 }
927 }
928
929 impl App {
930 fn boot_background(
931 &self,
932 ipc_url: String,
933 user_data_dir: Option<&str>,
934 ) -> anyhow::Result<()> {
935 let path = &self.0;
936
937 match fork::fork() {
938 Ok(Fork::Parent(_)) => Ok(()),
939 Ok(Fork::Child) => {
940 unsafe { std::env::set_var(FORCE_CLI_MODE_ENV_VAR_NAME, "") };
941 if fork::setsid().is_err() {
942 eprintln!("failed to setsid: {}", std::io::Error::last_os_error());
943 process::exit(1);
944 }
945 if fork::close_fd().is_err() {
946 eprintln!("failed to close_fd: {}", std::io::Error::last_os_error());
947 }
948 let mut args: Vec<OsString> =
949 vec![path.as_os_str().to_owned(), OsString::from(ipc_url)];
950 if let Some(dir) = user_data_dir {
951 args.push(OsString::from("--user-data-dir"));
952 args.push(OsString::from(dir));
953 }
954 let error = exec::execvp(path.clone(), &args);
955 // if exec succeeded, we never get here.
956 eprintln!("failed to exec {:?}: {}", path, error);
957 process::exit(1)
958 }
959 Err(_) => Err(anyhow!(io::Error::last_os_error())),
960 }
961 }
962
963 fn wait_for_socket(
964 &self,
965 sock_addr: &SocketAddr,
966 sock: &mut UnixDatagram,
967 ) -> Result<(), std::io::Error> {
968 for _ in 0..100 {
969 thread::sleep(Duration::from_millis(10));
970 if sock.connect_addr(sock_addr).is_ok() {
971 return Ok(());
972 }
973 }
974 sock.connect_addr(sock_addr)
975 }
976 }
977}
978
979#[cfg(target_os = "linux")]
980mod flatpak {
981 use std::ffi::OsString;
982 use std::path::PathBuf;
983 use std::process::Command;
984 use std::{env, process};
985
986 const EXTRA_LIB_ENV_NAME: &str = "ZED_FLATPAK_LIB_PATH";
987 const NO_ESCAPE_ENV_NAME: &str = "ZED_FLATPAK_NO_ESCAPE";
988
989 /// Adds bundled libraries to LD_LIBRARY_PATH if running under flatpak
990 pub fn ld_extra_libs() {
991 let mut paths = if let Ok(paths) = env::var("LD_LIBRARY_PATH") {
992 env::split_paths(&paths).collect()
993 } else {
994 Vec::new()
995 };
996
997 if let Ok(extra_path) = env::var(EXTRA_LIB_ENV_NAME) {
998 paths.push(extra_path.into());
999 }
1000
1001 unsafe { env::set_var("LD_LIBRARY_PATH", env::join_paths(paths).unwrap()) };
1002 }
1003
1004 /// Restarts outside of the sandbox if currently running within it
1005 pub fn try_restart_to_host() {
1006 if let Some(flatpak_dir) = get_flatpak_dir() {
1007 let mut args = vec!["/usr/bin/flatpak-spawn".into(), "--host".into()];
1008 args.append(&mut get_xdg_env_args());
1009 args.push("--env=ZED_UPDATE_EXPLANATION=Please use flatpak to update zed".into());
1010 args.push(
1011 format!(
1012 "--env={EXTRA_LIB_ENV_NAME}={}",
1013 flatpak_dir.join("lib").to_str().unwrap()
1014 )
1015 .into(),
1016 );
1017 args.push(flatpak_dir.join("bin").join("zed").into());
1018
1019 let mut is_app_location_set = false;
1020 for arg in &env::args_os().collect::<Vec<_>>()[1..] {
1021 args.push(arg.clone());
1022 is_app_location_set |= arg == "--zed";
1023 }
1024
1025 if !is_app_location_set {
1026 args.push("--zed".into());
1027 args.push(flatpak_dir.join("libexec").join("zed-editor").into());
1028 }
1029
1030 let error = exec::execvp("/usr/bin/flatpak-spawn", args);
1031 eprintln!("failed restart cli on host: {:?}", error);
1032 process::exit(1);
1033 }
1034 }
1035
1036 pub fn set_bin_if_no_escape(mut args: super::Args) -> super::Args {
1037 if env::var(NO_ESCAPE_ENV_NAME).is_ok()
1038 && env::var("FLATPAK_ID").is_ok_and(|id| id.starts_with("dev.zed.Zed"))
1039 && args.zed.is_none()
1040 {
1041 args.zed = Some("/app/libexec/zed-editor".into());
1042 unsafe { env::set_var("ZED_UPDATE_EXPLANATION", "Please use flatpak to update zed") };
1043 }
1044 args
1045 }
1046
1047 fn get_flatpak_dir() -> Option<PathBuf> {
1048 if env::var(NO_ESCAPE_ENV_NAME).is_ok() {
1049 return None;
1050 }
1051
1052 if let Ok(flatpak_id) = env::var("FLATPAK_ID") {
1053 if !flatpak_id.starts_with("dev.zed.Zed") {
1054 return None;
1055 }
1056
1057 let install_dir = Command::new("/usr/bin/flatpak-spawn")
1058 .arg("--host")
1059 .arg("flatpak")
1060 .arg("info")
1061 .arg("--show-location")
1062 .arg(flatpak_id)
1063 .output()
1064 .unwrap();
1065 let install_dir = PathBuf::from(String::from_utf8(install_dir.stdout).unwrap().trim());
1066 Some(install_dir.join("files"))
1067 } else {
1068 None
1069 }
1070 }
1071
1072 fn get_xdg_env_args() -> Vec<OsString> {
1073 let xdg_keys = [
1074 "XDG_DATA_HOME",
1075 "XDG_CONFIG_HOME",
1076 "XDG_CACHE_HOME",
1077 "XDG_STATE_HOME",
1078 ];
1079 env::vars()
1080 .filter(|(key, _)| xdg_keys.contains(&key.as_str()))
1081 .map(|(key, val)| format!("--env=FLATPAK_{}={}", key, val).into())
1082 .collect()
1083 }
1084}
1085
1086#[cfg(target_os = "windows")]
1087mod windows {
1088 use anyhow::Context;
1089 use release_channel::app_identifier;
1090 use windows::{
1091 Win32::{
1092 Foundation::{CloseHandle, ERROR_ALREADY_EXISTS, GENERIC_WRITE, GetLastError},
1093 Storage::FileSystem::{
1094 CreateFileW, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_MODE, OPEN_EXISTING, WriteFile,
1095 },
1096 System::Threading::CreateMutexW,
1097 },
1098 core::HSTRING,
1099 };
1100
1101 use crate::{Detect, InstalledApp};
1102 use std::io;
1103 use std::path::{Path, PathBuf};
1104 use std::process::ExitStatus;
1105
1106 fn check_single_instance() -> bool {
1107 let mutex = unsafe {
1108 CreateMutexW(
1109 None,
1110 false,
1111 &HSTRING::from(format!("{}-Instance-Mutex", app_identifier())),
1112 )
1113 .expect("Unable to create instance sync event")
1114 };
1115 let last_err = unsafe { GetLastError() };
1116 let _ = unsafe { CloseHandle(mutex) };
1117 last_err != ERROR_ALREADY_EXISTS
1118 }
1119
1120 struct App(PathBuf);
1121
1122 impl InstalledApp for App {
1123 fn zed_version_string(&self) -> String {
1124 format!(
1125 "Zed {}{}{} – {}",
1126 if *release_channel::RELEASE_CHANNEL_NAME == "stable" {
1127 "".to_string()
1128 } else {
1129 format!("{} ", *release_channel::RELEASE_CHANNEL_NAME)
1130 },
1131 option_env!("RELEASE_VERSION").unwrap_or_default(),
1132 match option_env!("ZED_COMMIT_SHA") {
1133 Some(commit_sha) => format!(" {commit_sha} "),
1134 None => "".to_string(),
1135 },
1136 self.0.display(),
1137 )
1138 }
1139
1140 fn launch(&self, ipc_url: String, user_data_dir: Option<&str>) -> anyhow::Result<()> {
1141 if check_single_instance() {
1142 let mut cmd = std::process::Command::new(self.0.clone());
1143 cmd.arg(ipc_url);
1144 if let Some(dir) = user_data_dir {
1145 cmd.arg("--user-data-dir").arg(dir);
1146 }
1147 cmd.spawn()?;
1148 } else {
1149 unsafe {
1150 let pipe = CreateFileW(
1151 &HSTRING::from(format!("\\\\.\\pipe\\{}-Named-Pipe", app_identifier())),
1152 GENERIC_WRITE.0,
1153 FILE_SHARE_MODE::default(),
1154 None,
1155 OPEN_EXISTING,
1156 FILE_FLAGS_AND_ATTRIBUTES::default(),
1157 None,
1158 )?;
1159 let message = ipc_url.as_bytes();
1160 let mut bytes_written = 0;
1161 WriteFile(pipe, Some(message), Some(&mut bytes_written), None)?;
1162 CloseHandle(pipe)?;
1163 }
1164 }
1165 Ok(())
1166 }
1167
1168 fn run_foreground(
1169 &self,
1170 ipc_url: String,
1171 user_data_dir: Option<&str>,
1172 ) -> io::Result<ExitStatus> {
1173 let mut cmd = std::process::Command::new(self.0.clone());
1174 cmd.arg(ipc_url).arg("--foreground");
1175 if let Some(dir) = user_data_dir {
1176 cmd.arg("--user-data-dir").arg(dir);
1177 }
1178 cmd.spawn()?.wait()
1179 }
1180
1181 fn path(&self) -> PathBuf {
1182 self.0.clone()
1183 }
1184 }
1185
1186 impl Detect {
1187 pub fn detect(path: Option<&Path>) -> anyhow::Result<impl InstalledApp> {
1188 let path = if let Some(path) = path {
1189 path.to_path_buf().canonicalize()?
1190 } else {
1191 let cli = std::env::current_exe()?;
1192 let dir = cli.parent().context("no parent path for cli")?;
1193
1194 // ../Zed.exe is the standard, lib/zed is for MSYS2, ./zed.exe is for the target
1195 // directory in development builds.
1196 let possible_locations = ["../Zed.exe", "../lib/zed/zed-editor.exe", "./zed.exe"];
1197 possible_locations
1198 .iter()
1199 .find_map(|p| dir.join(p).canonicalize().ok().filter(|path| path != &cli))
1200 .context(format!(
1201 "could not find any of: {}",
1202 possible_locations.join(", ")
1203 ))?
1204 };
1205
1206 Ok(App(path))
1207 }
1208 }
1209}
1210
1211#[cfg(target_os = "macos")]
1212mod mac_os {
1213 use anyhow::{Context as _, Result};
1214 use core_foundation::{
1215 array::{CFArray, CFIndex},
1216 base::TCFType as _,
1217 string::kCFStringEncodingUTF8,
1218 url::{CFURL, CFURLCreateWithBytes},
1219 };
1220 use core_services::{
1221 LSLaunchURLSpec, LSOpenFromURLSpec, kLSLaunchDefaults, kLSLaunchDontSwitch,
1222 };
1223 use serde::Deserialize;
1224 use std::{
1225 ffi::OsStr,
1226 fs, io,
1227 path::{Path, PathBuf},
1228 process::{Command, ExitStatus},
1229 ptr,
1230 };
1231
1232 use cli::FORCE_CLI_MODE_ENV_VAR_NAME;
1233
1234 use crate::{Detect, InstalledApp};
1235
1236 #[derive(Debug, Deserialize)]
1237 struct InfoPlist {
1238 #[serde(rename = "CFBundleShortVersionString")]
1239 bundle_short_version_string: String,
1240 }
1241
1242 enum Bundle {
1243 App {
1244 app_bundle: PathBuf,
1245 plist: InfoPlist,
1246 },
1247 LocalPath {
1248 executable: PathBuf,
1249 },
1250 }
1251
1252 fn locate_bundle() -> Result<PathBuf> {
1253 let cli_path = std::env::current_exe()?.canonicalize()?;
1254 let mut app_path = cli_path.clone();
1255 while app_path.extension() != Some(OsStr::new("app")) {
1256 anyhow::ensure!(
1257 app_path.pop(),
1258 "cannot find app bundle containing {cli_path:?}"
1259 );
1260 }
1261 Ok(app_path)
1262 }
1263
1264 impl Detect {
1265 pub fn detect(path: Option<&Path>) -> anyhow::Result<impl InstalledApp> {
1266 let bundle_path = if let Some(bundle_path) = path {
1267 bundle_path
1268 .canonicalize()
1269 .with_context(|| format!("Args bundle path {bundle_path:?} canonicalization"))?
1270 } else {
1271 locate_bundle().context("bundle autodiscovery")?
1272 };
1273
1274 match bundle_path.extension().and_then(|ext| ext.to_str()) {
1275 Some("app") => {
1276 let plist_path = bundle_path.join("Contents/Info.plist");
1277 let plist =
1278 plist::from_file::<_, InfoPlist>(&plist_path).with_context(|| {
1279 format!("Reading *.app bundle plist file at {plist_path:?}")
1280 })?;
1281 Ok(Bundle::App {
1282 app_bundle: bundle_path,
1283 plist,
1284 })
1285 }
1286 _ => Ok(Bundle::LocalPath {
1287 executable: bundle_path,
1288 }),
1289 }
1290 }
1291 }
1292
1293 impl InstalledApp for Bundle {
1294 fn zed_version_string(&self) -> String {
1295 format!("Zed {} – {}", self.version(), self.path().display(),)
1296 }
1297
1298 fn launch(&self, url: String, user_data_dir: Option<&str>) -> anyhow::Result<()> {
1299 match self {
1300 Self::App { app_bundle, .. } => {
1301 let app_path = app_bundle;
1302
1303 let status = unsafe {
1304 let app_url = CFURL::from_path(app_path, true)
1305 .with_context(|| format!("invalid app path {app_path:?}"))?;
1306 let url_to_open = CFURL::wrap_under_create_rule(CFURLCreateWithBytes(
1307 ptr::null(),
1308 url.as_ptr(),
1309 url.len() as CFIndex,
1310 kCFStringEncodingUTF8,
1311 ptr::null(),
1312 ));
1313 // equivalent to: open zed-cli:... -a /Applications/Zed\ Preview.app
1314 let urls_to_open =
1315 CFArray::from_copyable(&[url_to_open.as_concrete_TypeRef()]);
1316 LSOpenFromURLSpec(
1317 &LSLaunchURLSpec {
1318 appURL: app_url.as_concrete_TypeRef(),
1319 itemURLs: urls_to_open.as_concrete_TypeRef(),
1320 passThruParams: ptr::null(),
1321 launchFlags: kLSLaunchDefaults | kLSLaunchDontSwitch,
1322 asyncRefCon: ptr::null_mut(),
1323 },
1324 ptr::null_mut(),
1325 )
1326 };
1327
1328 anyhow::ensure!(
1329 status == 0,
1330 "cannot start app bundle {}",
1331 self.zed_version_string()
1332 );
1333 }
1334
1335 Self::LocalPath { executable, .. } => {
1336 let executable_parent = executable
1337 .parent()
1338 .with_context(|| format!("Executable {executable:?} path has no parent"))?;
1339 let subprocess_stdout_file = fs::File::create(
1340 executable_parent.join("zed_dev.log"),
1341 )
1342 .with_context(|| format!("Log file creation in {executable_parent:?}"))?;
1343 let subprocess_stdin_file =
1344 subprocess_stdout_file.try_clone().with_context(|| {
1345 format!("Cloning descriptor for file {subprocess_stdout_file:?}")
1346 })?;
1347 let mut command = std::process::Command::new(executable);
1348 command.env(FORCE_CLI_MODE_ENV_VAR_NAME, "");
1349 if let Some(dir) = user_data_dir {
1350 command.arg("--user-data-dir").arg(dir);
1351 }
1352 command
1353 .stderr(subprocess_stdout_file)
1354 .stdout(subprocess_stdin_file)
1355 .arg(url);
1356
1357 command
1358 .spawn()
1359 .with_context(|| format!("Spawning {command:?}"))?;
1360 }
1361 }
1362
1363 Ok(())
1364 }
1365
1366 fn run_foreground(
1367 &self,
1368 ipc_url: String,
1369 user_data_dir: Option<&str>,
1370 ) -> io::Result<ExitStatus> {
1371 let path = match self {
1372 Bundle::App { app_bundle, .. } => app_bundle.join("Contents/MacOS/zed"),
1373 Bundle::LocalPath { executable, .. } => executable.clone(),
1374 };
1375
1376 let mut cmd = std::process::Command::new(path);
1377 cmd.arg(ipc_url);
1378 if let Some(dir) = user_data_dir {
1379 cmd.arg("--user-data-dir").arg(dir);
1380 }
1381 cmd.status()
1382 }
1383
1384 fn path(&self) -> PathBuf {
1385 match self {
1386 Bundle::App { app_bundle, .. } => app_bundle.join("Contents/MacOS/zed"),
1387 Bundle::LocalPath { executable, .. } => executable.clone(),
1388 }
1389 }
1390 }
1391
1392 impl Bundle {
1393 fn version(&self) -> String {
1394 match self {
1395 Self::App { plist, .. } => plist.bundle_short_version_string.clone(),
1396 Self::LocalPath { .. } => "<development>".to_string(),
1397 }
1398 }
1399
1400 fn path(&self) -> &Path {
1401 match self {
1402 Self::App { app_bundle, .. } => app_bundle,
1403 Self::LocalPath { executable, .. } => executable,
1404 }
1405 }
1406 }
1407
1408 pub(super) fn spawn_channel_cli(
1409 channel: release_channel::ReleaseChannel,
1410 leftover_args: Vec<String>,
1411 ) -> Result<()> {
1412 use anyhow::bail;
1413
1414 let app_path_prompt = format!(
1415 "POSIX path of (path to application \"{}\")",
1416 channel.display_name()
1417 );
1418 let app_path_output = Command::new("osascript")
1419 .arg("-e")
1420 .arg(&app_path_prompt)
1421 .output()?;
1422 if !app_path_output.status.success() {
1423 bail!(
1424 "Could not determine app path for {}",
1425 channel.display_name()
1426 );
1427 }
1428 let app_path = String::from_utf8(app_path_output.stdout)?.trim().to_owned();
1429 let cli_path = format!("{app_path}/Contents/MacOS/cli");
1430 Command::new(cli_path).args(leftover_args).spawn()?;
1431 Ok(())
1432 }
1433}