1use crate::{
2 RemotePlatform,
3 json_log::LogRecord,
4 protocol::{MESSAGE_LEN_SIZE, message_len_from_buffer, read_message_with_len, write_message},
5};
6use anyhow::{Context as _, Result};
7use futures::{
8 AsyncReadExt as _, FutureExt as _, StreamExt as _,
9 channel::mpsc::{Sender, UnboundedReceiver, UnboundedSender},
10};
11use gpui::{AppContext as _, AsyncApp, Task};
12use rpc::proto::Envelope;
13use smol::process::Child;
14
15pub mod docker;
16pub mod ssh;
17pub mod wsl;
18
19/// Parses the output of `uname -sm` to determine the remote platform.
20/// Takes the last line to skip possible shell initialization output.
21fn parse_platform(output: &str) -> Result<RemotePlatform> {
22 let output = output.trim();
23 let uname = output.rsplit_once('\n').map_or(output, |(_, last)| last);
24 let Some((os, arch)) = uname.split_once(" ") else {
25 anyhow::bail!("unknown uname: {uname:?}")
26 };
27
28 let os = match os {
29 "Darwin" => "macos",
30 "Linux" => "linux",
31 _ => anyhow::bail!(
32 "Prebuilt remote servers are not yet available for {os:?}. See https://zed.dev/docs/remote-development"
33 ),
34 };
35
36 // exclude armv5,6,7 as they are 32-bit.
37 let arch = if arch.starts_with("armv8")
38 || arch.starts_with("armv9")
39 || arch.starts_with("arm64")
40 || arch.starts_with("aarch64")
41 {
42 "aarch64"
43 } else if arch.starts_with("x86") {
44 "x86_64"
45 } else {
46 anyhow::bail!(
47 "Prebuilt remote servers are not yet available for {arch:?}. See https://zed.dev/docs/remote-development"
48 )
49 };
50
51 Ok(RemotePlatform { os, arch })
52}
53
54/// Parses the output of `echo $SHELL` to determine the remote shell.
55/// Takes the last line to skip possible shell initialization output.
56fn parse_shell(output: &str, fallback_shell: &str) -> String {
57 let output = output.trim();
58 let shell = output.rsplit_once('\n').map_or(output, |(_, last)| last);
59 if shell.is_empty() {
60 log::error!("$SHELL is not set, falling back to {fallback_shell}");
61 fallback_shell.to_owned()
62 } else {
63 shell.to_owned()
64 }
65}
66
67fn handle_rpc_messages_over_child_process_stdio(
68 mut remote_proxy_process: Child,
69 incoming_tx: UnboundedSender<Envelope>,
70 mut outgoing_rx: UnboundedReceiver<Envelope>,
71 mut connection_activity_tx: Sender<()>,
72 cx: &AsyncApp,
73) -> Task<Result<i32>> {
74 let mut child_stderr = remote_proxy_process.stderr.take().unwrap();
75 let mut child_stdout = remote_proxy_process.stdout.take().unwrap();
76 let mut child_stdin = remote_proxy_process.stdin.take().unwrap();
77
78 let mut stdin_buffer = Vec::new();
79 let mut stdout_buffer = Vec::new();
80 let mut stderr_buffer = Vec::new();
81 let mut stderr_offset = 0;
82
83 let stdin_task = cx.background_spawn(async move {
84 while let Some(outgoing) = outgoing_rx.next().await {
85 write_message(&mut child_stdin, &mut stdin_buffer, outgoing).await?;
86 }
87 anyhow::Ok(())
88 });
89
90 let stdout_task = cx.background_spawn({
91 let mut connection_activity_tx = connection_activity_tx.clone();
92 async move {
93 loop {
94 stdout_buffer.resize(MESSAGE_LEN_SIZE, 0);
95 let len = child_stdout.read(&mut stdout_buffer).await?;
96
97 if len == 0 {
98 return anyhow::Ok(());
99 }
100
101 if len < MESSAGE_LEN_SIZE {
102 child_stdout.read_exact(&mut stdout_buffer[len..]).await?;
103 }
104
105 let message_len = message_len_from_buffer(&stdout_buffer);
106 let envelope =
107 read_message_with_len(&mut child_stdout, &mut stdout_buffer, message_len)
108 .await?;
109 connection_activity_tx.try_send(()).ok();
110 incoming_tx.unbounded_send(envelope).ok();
111 }
112 }
113 });
114
115 let stderr_task: Task<anyhow::Result<()>> = cx.background_spawn(async move {
116 loop {
117 stderr_buffer.resize(stderr_offset + 1024, 0);
118
119 let len = child_stderr
120 .read(&mut stderr_buffer[stderr_offset..])
121 .await?;
122 if len == 0 {
123 return anyhow::Ok(());
124 }
125
126 stderr_offset += len;
127 let mut start_ix = 0;
128 while let Some(ix) = stderr_buffer[start_ix..stderr_offset]
129 .iter()
130 .position(|b| b == &b'\n')
131 {
132 let line_ix = start_ix + ix;
133 let content = &stderr_buffer[start_ix..line_ix];
134 start_ix = line_ix + 1;
135 if let Ok(record) = serde_json::from_slice::<LogRecord>(content) {
136 record.log(log::logger())
137 } else {
138 eprintln!("(remote) {}", String::from_utf8_lossy(content));
139 }
140 }
141 stderr_buffer.drain(0..start_ix);
142 stderr_offset -= start_ix;
143
144 connection_activity_tx.try_send(()).ok();
145 }
146 });
147
148 cx.background_spawn(async move {
149 let result = futures::select! {
150 result = stdin_task.fuse() => {
151 result.context("stdin")
152 }
153 result = stdout_task.fuse() => {
154 result.context("stdout")
155 }
156 result = stderr_task.fuse() => {
157 result.context("stderr")
158 }
159 };
160 let status = remote_proxy_process.status().await?.code().unwrap_or(1);
161 if status != 0 {
162 anyhow::bail!("Remote server exited with status {status}");
163 }
164 match result {
165 Ok(_) => Ok(status),
166 Err(error) => Err(error),
167 }
168 })
169}
170
171#[cfg(debug_assertions)]
172async fn build_remote_server_from_source(
173 platform: &crate::RemotePlatform,
174 delegate: &dyn crate::RemoteClientDelegate,
175 cx: &mut AsyncApp,
176) -> Result<Option<std::path::PathBuf>> {
177 use smol::process::{Command, Stdio};
178 use std::env::VarError;
179 use std::path::Path;
180 use util::command::new_smol_command;
181
182 // By default, we make building remote server from source opt-out and we do not force artifact compression
183 // for quicker builds.
184 let build_remote_server =
185 std::env::var("ZED_BUILD_REMOTE_SERVER").unwrap_or("nocompress".into());
186
187 if let "false" | "no" | "off" | "0" = &*build_remote_server {
188 return Ok(None);
189 }
190
191 async fn run_cmd(command: &mut Command) -> Result<()> {
192 let output = command
193 .kill_on_drop(true)
194 .stderr(Stdio::inherit())
195 .output()
196 .await?;
197 anyhow::ensure!(
198 output.status.success(),
199 "Failed to run command: {command:?}"
200 );
201 Ok(())
202 }
203
204 let use_musl = !build_remote_server.contains("nomusl");
205 let triple = format!(
206 "{}-{}",
207 platform.arch,
208 match platform.os {
209 "linux" =>
210 if use_musl {
211 "unknown-linux-musl"
212 } else {
213 "unknown-linux-gnu"
214 },
215 "macos" => "apple-darwin",
216 _ => anyhow::bail!("can't cross compile for: {:?}", platform),
217 }
218 );
219 let mut rust_flags = match std::env::var("RUSTFLAGS") {
220 Ok(val) => val,
221 Err(VarError::NotPresent) => String::new(),
222 Err(e) => {
223 log::error!("Failed to get env var `RUSTFLAGS` value: {e}");
224 String::new()
225 }
226 };
227 if platform.os == "linux" && use_musl {
228 rust_flags.push_str(" -C target-feature=+crt-static");
229
230 if let Ok(path) = std::env::var("ZED_ZSTD_MUSL_LIB") {
231 rust_flags.push_str(&format!(" -C link-arg=-L{path}"));
232 }
233 }
234 if build_remote_server.contains("mold") {
235 rust_flags.push_str(" -C link-arg=-fuse-ld=mold");
236 }
237
238 if platform.arch == std::env::consts::ARCH && platform.os == std::env::consts::OS {
239 delegate.set_status(Some("Building remote server binary from source"), cx);
240 log::info!("building remote server binary from source");
241 run_cmd(
242 new_smol_command("cargo")
243 .current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."))
244 .args([
245 "build",
246 "--package",
247 "remote_server",
248 "--features",
249 "debug-embed",
250 "--target-dir",
251 "target/remote_server",
252 "--target",
253 &triple,
254 ])
255 .env("RUSTFLAGS", &rust_flags),
256 )
257 .await?;
258 } else {
259 if which("zig", cx).await?.is_none() {
260 anyhow::bail!(if cfg!(not(windows)) {
261 "zig not found on $PATH, install zig (see https://ziglang.org/learn/getting-started or use zigup)"
262 } else {
263 "zig not found on $PATH, install zig (use `winget install -e --id zig.zig` or see https://ziglang.org/learn/getting-started or use zigup)"
264 });
265 }
266
267 let rustup = which("rustup", cx)
268 .await?
269 .context("rustup not found on $PATH, install rustup (see https://rustup.rs/)")?;
270 delegate.set_status(Some("Adding rustup target for cross-compilation"), cx);
271 log::info!("adding rustup target");
272 run_cmd(
273 new_smol_command(rustup)
274 .args(["target", "add"])
275 .arg(&triple),
276 )
277 .await?;
278
279 if which("cargo-zigbuild", cx).await?.is_none() {
280 delegate.set_status(Some("Installing cargo-zigbuild for cross-compilation"), cx);
281 log::info!("installing cargo-zigbuild");
282 run_cmd(new_smol_command("cargo").args(["install", "--locked", "cargo-zigbuild"]))
283 .await?;
284 }
285
286 delegate.set_status(
287 Some(&format!(
288 "Building remote binary from source for {triple} with Zig"
289 )),
290 cx,
291 );
292 log::info!("building remote binary from source for {triple} with Zig");
293 run_cmd(
294 new_smol_command("cargo")
295 .args([
296 "zigbuild",
297 "--package",
298 "remote_server",
299 "--features",
300 "debug-embed",
301 "--target-dir",
302 "target/remote_server",
303 "--target",
304 &triple,
305 ])
306 .env("RUSTFLAGS", &rust_flags),
307 )
308 .await?;
309 };
310 let bin_path = Path::new("target")
311 .join("remote_server")
312 .join(&triple)
313 .join("debug")
314 .join("remote_server");
315
316 let path = if !build_remote_server.contains("nocompress") {
317 delegate.set_status(Some("Compressing binary"), cx);
318
319 #[cfg(not(target_os = "windows"))]
320 {
321 run_cmd(new_smol_command("gzip").args(["-f", &bin_path.to_string_lossy()])).await?;
322 }
323
324 #[cfg(target_os = "windows")]
325 {
326 // On Windows, we use 7z to compress the binary
327
328 let seven_zip = which("7z.exe",cx)
329 .await?
330 .context("7z.exe not found on $PATH, install it (e.g. with `winget install -e --id 7zip.7zip`) or, if you don't want this behaviour, set $env:ZED_BUILD_REMOTE_SERVER=\"nocompress\"")?;
331 let gz_path = format!("target/remote_server/{}/debug/remote_server.gz", triple);
332 if smol::fs::metadata(&gz_path).await.is_ok() {
333 smol::fs::remove_file(&gz_path).await?;
334 }
335 run_cmd(new_smol_command(seven_zip).args([
336 "a",
337 "-tgzip",
338 &gz_path,
339 &bin_path.to_string_lossy(),
340 ]))
341 .await?;
342 }
343
344 let mut archive_path = bin_path;
345 archive_path.set_extension("gz");
346 std::env::current_dir()?.join(archive_path)
347 } else {
348 bin_path
349 };
350
351 Ok(Some(path))
352}
353
354#[cfg(debug_assertions)]
355async fn which(
356 binary_name: impl AsRef<str>,
357 cx: &mut AsyncApp,
358) -> Result<Option<std::path::PathBuf>> {
359 let binary_name = binary_name.as_ref().to_string();
360 let binary_name_cloned = binary_name.clone();
361 let res = cx
362 .background_spawn(async move { which::which(binary_name_cloned) })
363 .await;
364 match res {
365 Ok(path) => Ok(Some(path)),
366 Err(which::Error::CannotFindBinaryPath) => Ok(None),
367 Err(err) => Err(anyhow::anyhow!(
368 "Failed to run 'which' to find the binary '{binary_name}': {err}"
369 )),
370 }
371}
372
373#[cfg(test)]
374mod tests {
375 use super::*;
376
377 #[test]
378 fn test_parse_platform() {
379 let result = parse_platform("Linux x86_64\n").unwrap();
380 assert_eq!(result.os, "linux");
381 assert_eq!(result.arch, "x86_64");
382
383 let result = parse_platform("Darwin arm64\n").unwrap();
384 assert_eq!(result.os, "macos");
385 assert_eq!(result.arch, "aarch64");
386
387 let result = parse_platform("Linux x86_64").unwrap();
388 assert_eq!(result.os, "linux");
389 assert_eq!(result.arch, "x86_64");
390
391 let result = parse_platform("some shell init output\nLinux aarch64\n").unwrap();
392 assert_eq!(result.os, "linux");
393 assert_eq!(result.arch, "aarch64");
394
395 let result = parse_platform("some shell init output\nLinux aarch64").unwrap();
396 assert_eq!(result.os, "linux");
397 assert_eq!(result.arch, "aarch64");
398
399 assert_eq!(parse_platform("Linux armv8l\n").unwrap().arch, "aarch64");
400 assert_eq!(parse_platform("Linux aarch64\n").unwrap().arch, "aarch64");
401 assert_eq!(parse_platform("Linux x86_64\n").unwrap().arch, "x86_64");
402
403 let result = parse_platform(
404 r#"Linux x86_64 - What you're referring to as Linux, is in fact, GNU/Linux...\n"#,
405 )
406 .unwrap();
407 assert_eq!(result.os, "linux");
408 assert_eq!(result.arch, "x86_64");
409
410 assert!(parse_platform("Windows x86_64\n").is_err());
411 assert!(parse_platform("Linux armv7l\n").is_err());
412 }
413
414 #[test]
415 fn test_parse_shell() {
416 assert_eq!(parse_shell("/bin/bash\n", "sh"), "/bin/bash");
417 assert_eq!(parse_shell("/bin/zsh\n", "sh"), "/bin/zsh");
418
419 assert_eq!(parse_shell("/bin/bash", "sh"), "/bin/bash");
420 assert_eq!(
421 parse_shell("some shell init output\n/bin/bash\n", "sh"),
422 "/bin/bash"
423 );
424 assert_eq!(
425 parse_shell("some shell init output\n/bin/bash", "sh"),
426 "/bin/bash"
427 );
428 assert_eq!(parse_shell("", "sh"), "sh");
429 assert_eq!(parse_shell("\n", "sh"), "sh");
430 }
431}