transport.rs

  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        match result {
162            Ok(_) => Ok(status),
163            Err(error) => Err(error),
164        }
165    })
166}
167
168#[cfg(debug_assertions)]
169async fn build_remote_server_from_source(
170    platform: &crate::RemotePlatform,
171    delegate: &dyn crate::RemoteClientDelegate,
172    cx: &mut AsyncApp,
173) -> Result<Option<std::path::PathBuf>> {
174    use smol::process::{Command, Stdio};
175    use std::env::VarError;
176    use std::path::Path;
177    use util::command::new_smol_command;
178
179    // By default, we make building remote server from source opt-out and we do not force artifact compression
180    // for quicker builds.
181    let build_remote_server =
182        std::env::var("ZED_BUILD_REMOTE_SERVER").unwrap_or("nocompress".into());
183
184    if let "false" | "no" | "off" | "0" = &*build_remote_server {
185        return Ok(None);
186    }
187
188    async fn run_cmd(command: &mut Command) -> Result<()> {
189        let output = command
190            .kill_on_drop(true)
191            .stderr(Stdio::inherit())
192            .output()
193            .await?;
194        anyhow::ensure!(
195            output.status.success(),
196            "Failed to run command: {command:?}"
197        );
198        Ok(())
199    }
200
201    let use_musl = !build_remote_server.contains("nomusl");
202    let triple = format!(
203        "{}-{}",
204        platform.arch,
205        match platform.os {
206            "linux" =>
207                if use_musl {
208                    "unknown-linux-musl"
209                } else {
210                    "unknown-linux-gnu"
211                },
212            "macos" => "apple-darwin",
213            _ => anyhow::bail!("can't cross compile for: {:?}", platform),
214        }
215    );
216    let mut rust_flags = match std::env::var("RUSTFLAGS") {
217        Ok(val) => val,
218        Err(VarError::NotPresent) => String::new(),
219        Err(e) => {
220            log::error!("Failed to get env var `RUSTFLAGS` value: {e}");
221            String::new()
222        }
223    };
224    if platform.os == "linux" && use_musl {
225        rust_flags.push_str(" -C target-feature=+crt-static");
226
227        if let Ok(path) = std::env::var("ZED_ZSTD_MUSL_LIB") {
228            rust_flags.push_str(&format!(" -C link-arg=-L{path}"));
229        }
230    }
231    if build_remote_server.contains("mold") {
232        rust_flags.push_str(" -C link-arg=-fuse-ld=mold");
233    }
234
235    if platform.arch == std::env::consts::ARCH && platform.os == std::env::consts::OS {
236        delegate.set_status(Some("Building remote server binary from source"), cx);
237        log::info!("building remote server binary from source");
238        run_cmd(
239            new_smol_command("cargo")
240                .current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."))
241                .args([
242                    "build",
243                    "--package",
244                    "remote_server",
245                    "--features",
246                    "debug-embed",
247                    "--target-dir",
248                    "target/remote_server",
249                    "--target",
250                    &triple,
251                ])
252                .env("RUSTFLAGS", &rust_flags),
253        )
254        .await?;
255    } else {
256        if which("zig", cx).await?.is_none() {
257            anyhow::bail!(if cfg!(not(windows)) {
258                "zig not found on $PATH, install zig (see https://ziglang.org/learn/getting-started or use zigup)"
259            } else {
260                "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)"
261            });
262        }
263
264        let rustup = which("rustup", cx)
265            .await?
266            .context("rustup not found on $PATH, install rustup (see https://rustup.rs/)")?;
267        delegate.set_status(Some("Adding rustup target for cross-compilation"), cx);
268        log::info!("adding rustup target");
269        run_cmd(
270            new_smol_command(rustup)
271                .args(["target", "add"])
272                .arg(&triple),
273        )
274        .await?;
275
276        if which("cargo-zigbuild", cx).await?.is_none() {
277            delegate.set_status(Some("Installing cargo-zigbuild for cross-compilation"), cx);
278            log::info!("installing cargo-zigbuild");
279            run_cmd(new_smol_command("cargo").args(["install", "--locked", "cargo-zigbuild"]))
280                .await?;
281        }
282
283        delegate.set_status(
284            Some(&format!(
285                "Building remote binary from source for {triple} with Zig"
286            )),
287            cx,
288        );
289        log::info!("building remote binary from source for {triple} with Zig");
290        run_cmd(
291            new_smol_command("cargo")
292                .args([
293                    "zigbuild",
294                    "--package",
295                    "remote_server",
296                    "--features",
297                    "debug-embed",
298                    "--target-dir",
299                    "target/remote_server",
300                    "--target",
301                    &triple,
302                ])
303                .env("RUSTFLAGS", &rust_flags),
304        )
305        .await?;
306    };
307    let bin_path = Path::new("target")
308        .join("remote_server")
309        .join(&triple)
310        .join("debug")
311        .join("remote_server");
312
313    let path = if !build_remote_server.contains("nocompress") {
314        delegate.set_status(Some("Compressing binary"), cx);
315
316        #[cfg(not(target_os = "windows"))]
317        {
318            run_cmd(new_smol_command("gzip").args(["-f", &bin_path.to_string_lossy()])).await?;
319        }
320
321        #[cfg(target_os = "windows")]
322        {
323            // On Windows, we use 7z to compress the binary
324
325            let seven_zip = which("7z.exe",cx)
326                .await?
327                .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\"")?;
328            let gz_path = format!("target/remote_server/{}/debug/remote_server.gz", triple);
329            if smol::fs::metadata(&gz_path).await.is_ok() {
330                smol::fs::remove_file(&gz_path).await?;
331            }
332            run_cmd(new_smol_command(seven_zip).args([
333                "a",
334                "-tgzip",
335                &gz_path,
336                &bin_path.to_string_lossy(),
337            ]))
338            .await?;
339        }
340
341        let mut archive_path = bin_path;
342        archive_path.set_extension("gz");
343        std::env::current_dir()?.join(archive_path)
344    } else {
345        bin_path
346    };
347
348    Ok(Some(path))
349}
350
351#[cfg(debug_assertions)]
352async fn which(
353    binary_name: impl AsRef<str>,
354    cx: &mut AsyncApp,
355) -> Result<Option<std::path::PathBuf>> {
356    let binary_name = binary_name.as_ref().to_string();
357    let binary_name_cloned = binary_name.clone();
358    let res = cx
359        .background_spawn(async move { which::which(binary_name_cloned) })
360        .await;
361    match res {
362        Ok(path) => Ok(Some(path)),
363        Err(which::Error::CannotFindBinaryPath) => Ok(None),
364        Err(err) => Err(anyhow::anyhow!(
365            "Failed to run 'which' to find the binary '{binary_name}': {err}"
366        )),
367    }
368}
369
370#[cfg(test)]
371mod tests {
372    use super::*;
373
374    #[test]
375    fn test_parse_platform() {
376        let result = parse_platform("Linux x86_64\n").unwrap();
377        assert_eq!(result.os, "linux");
378        assert_eq!(result.arch, "x86_64");
379
380        let result = parse_platform("Darwin arm64\n").unwrap();
381        assert_eq!(result.os, "macos");
382        assert_eq!(result.arch, "aarch64");
383
384        let result = parse_platform("Linux x86_64").unwrap();
385        assert_eq!(result.os, "linux");
386        assert_eq!(result.arch, "x86_64");
387
388        let result = parse_platform("some shell init output\nLinux aarch64\n").unwrap();
389        assert_eq!(result.os, "linux");
390        assert_eq!(result.arch, "aarch64");
391
392        let result = parse_platform("some shell init output\nLinux aarch64").unwrap();
393        assert_eq!(result.os, "linux");
394        assert_eq!(result.arch, "aarch64");
395
396        assert_eq!(parse_platform("Linux armv8l\n").unwrap().arch, "aarch64");
397        assert_eq!(parse_platform("Linux aarch64\n").unwrap().arch, "aarch64");
398        assert_eq!(parse_platform("Linux x86_64\n").unwrap().arch, "x86_64");
399
400        let result = parse_platform(
401            r#"Linux x86_64 - What you're referring to as Linux, is in fact, GNU/Linux...\n"#,
402        )
403        .unwrap();
404        assert_eq!(result.os, "linux");
405        assert_eq!(result.arch, "x86_64");
406
407        assert!(parse_platform("Windows x86_64\n").is_err());
408        assert!(parse_platform("Linux armv7l\n").is_err());
409    }
410
411    #[test]
412    fn test_parse_shell() {
413        assert_eq!(parse_shell("/bin/bash\n", "sh"), "/bin/bash");
414        assert_eq!(parse_shell("/bin/zsh\n", "sh"), "/bin/zsh");
415
416        assert_eq!(parse_shell("/bin/bash", "sh"), "/bin/bash");
417        assert_eq!(
418            parse_shell("some shell init output\n/bin/bash\n", "sh"),
419            "/bin/bash"
420        );
421        assert_eq!(
422            parse_shell("some shell init output\n/bin/bash", "sh"),
423            "/bin/bash"
424        );
425        assert_eq!(parse_shell("", "sh"), "sh");
426        assert_eq!(parse_shell("\n", "sh"), "sh");
427    }
428}