transport.rs

  1use crate::{
  2    json_log::LogRecord,
  3    protocol::{MESSAGE_LEN_SIZE, message_len_from_buffer, read_message_with_len, write_message},
  4};
  5use anyhow::{Context as _, Result};
  6use futures::{
  7    AsyncReadExt as _, FutureExt as _, StreamExt as _,
  8    channel::mpsc::{Sender, UnboundedReceiver, UnboundedSender},
  9};
 10use gpui::{AppContext as _, AsyncApp, Task};
 11use rpc::proto::Envelope;
 12use smol::process::Child;
 13
 14pub mod ssh;
 15pub mod wsl;
 16
 17fn handle_rpc_messages_over_child_process_stdio(
 18    mut ssh_proxy_process: Child,
 19    incoming_tx: UnboundedSender<Envelope>,
 20    mut outgoing_rx: UnboundedReceiver<Envelope>,
 21    mut connection_activity_tx: Sender<()>,
 22    cx: &AsyncApp,
 23) -> Task<Result<i32>> {
 24    let mut child_stderr = ssh_proxy_process.stderr.take().unwrap();
 25    let mut child_stdout = ssh_proxy_process.stdout.take().unwrap();
 26    let mut child_stdin = ssh_proxy_process.stdin.take().unwrap();
 27
 28    let mut stdin_buffer = Vec::new();
 29    let mut stdout_buffer = Vec::new();
 30    let mut stderr_buffer = Vec::new();
 31    let mut stderr_offset = 0;
 32
 33    let stdin_task = cx.background_spawn(async move {
 34        while let Some(outgoing) = outgoing_rx.next().await {
 35            write_message(&mut child_stdin, &mut stdin_buffer, outgoing).await?;
 36        }
 37        anyhow::Ok(())
 38    });
 39
 40    let stdout_task = cx.background_spawn({
 41        let mut connection_activity_tx = connection_activity_tx.clone();
 42        async move {
 43            loop {
 44                stdout_buffer.resize(MESSAGE_LEN_SIZE, 0);
 45                let len = child_stdout.read(&mut stdout_buffer).await?;
 46
 47                if len == 0 {
 48                    return anyhow::Ok(());
 49                }
 50
 51                if len < MESSAGE_LEN_SIZE {
 52                    child_stdout.read_exact(&mut stdout_buffer[len..]).await?;
 53                }
 54
 55                let message_len = message_len_from_buffer(&stdout_buffer);
 56                let envelope =
 57                    read_message_with_len(&mut child_stdout, &mut stdout_buffer, message_len)
 58                        .await?;
 59                connection_activity_tx.try_send(()).ok();
 60                incoming_tx.unbounded_send(envelope).ok();
 61            }
 62        }
 63    });
 64
 65    let stderr_task: Task<anyhow::Result<()>> = cx.background_spawn(async move {
 66        loop {
 67            stderr_buffer.resize(stderr_offset + 1024, 0);
 68
 69            let len = child_stderr
 70                .read(&mut stderr_buffer[stderr_offset..])
 71                .await?;
 72            if len == 0 {
 73                return anyhow::Ok(());
 74            }
 75
 76            stderr_offset += len;
 77            let mut start_ix = 0;
 78            while let Some(ix) = stderr_buffer[start_ix..stderr_offset]
 79                .iter()
 80                .position(|b| b == &b'\n')
 81            {
 82                let line_ix = start_ix + ix;
 83                let content = &stderr_buffer[start_ix..line_ix];
 84                start_ix = line_ix + 1;
 85                if let Ok(record) = serde_json::from_slice::<LogRecord>(content) {
 86                    record.log(log::logger())
 87                } else {
 88                    eprintln!("(remote) {}", String::from_utf8_lossy(content));
 89                }
 90            }
 91            stderr_buffer.drain(0..start_ix);
 92            stderr_offset -= start_ix;
 93
 94            connection_activity_tx.try_send(()).ok();
 95        }
 96    });
 97
 98    cx.background_spawn(async move {
 99        let result = futures::select! {
100            result = stdin_task.fuse() => {
101                result.context("stdin")
102            }
103            result = stdout_task.fuse() => {
104                result.context("stdout")
105            }
106            result = stderr_task.fuse() => {
107                result.context("stderr")
108            }
109        };
110        let status = ssh_proxy_process.status().await?.code().unwrap_or(1);
111        match result {
112            Ok(_) => Ok(status),
113            Err(error) => Err(error),
114        }
115    })
116}
117
118#[cfg(debug_assertions)]
119async fn build_remote_server_from_source(
120    platform: &crate::RemotePlatform,
121    delegate: &dyn crate::RemoteClientDelegate,
122    cx: &mut AsyncApp,
123) -> Result<Option<std::path::PathBuf>> {
124    use smol::process::{Command, Stdio};
125    use std::env::VarError;
126    use std::path::Path;
127    use util::command::new_smol_command;
128
129    // By default, we make building remote server from source opt-out and we do not force artifact compression
130    // for quicker builds.
131    let build_remote_server =
132        std::env::var("ZED_BUILD_REMOTE_SERVER").unwrap_or("nocompress".into());
133
134    if build_remote_server == "false"
135        || build_remote_server == "no"
136        || build_remote_server == "off"
137        || build_remote_server == "0"
138    {
139        return Ok(None);
140    }
141
142    async fn run_cmd(command: &mut Command) -> Result<()> {
143        let output = command
144            .kill_on_drop(true)
145            .stderr(Stdio::inherit())
146            .output()
147            .await?;
148        anyhow::ensure!(
149            output.status.success(),
150            "Failed to run command: {command:?}"
151        );
152        Ok(())
153    }
154
155    let use_musl = !build_remote_server.contains("nomusl");
156    let triple = format!(
157        "{}-{}",
158        platform.arch,
159        match platform.os {
160            "linux" =>
161                if use_musl {
162                    "unknown-linux-musl"
163                } else {
164                    "unknown-linux-gnu"
165                },
166            "macos" => "apple-darwin",
167            _ => anyhow::bail!("can't cross compile for: {:?}", platform),
168        }
169    );
170    let mut rust_flags = match std::env::var("RUSTFLAGS") {
171        Ok(val) => val,
172        Err(VarError::NotPresent) => String::new(),
173        Err(e) => {
174            log::error!("Failed to get env var `RUSTFLAGS` value: {e}");
175            String::new()
176        }
177    };
178    if platform.os == "linux" && use_musl {
179        rust_flags.push_str(" -C target-feature=+crt-static");
180
181        if let Ok(path) = std::env::var("ZED_ZSTD_MUSL_LIB") {
182            rust_flags.push_str(&format!(" -C link-arg=-L{path}"));
183        }
184    }
185    if build_remote_server.contains("mold") {
186        rust_flags.push_str(" -C link-arg=-fuse-ld=mold");
187    }
188
189    if platform.arch == std::env::consts::ARCH && platform.os == std::env::consts::OS {
190        delegate.set_status(Some("Building remote server binary from source"), cx);
191        log::info!("building remote server binary from source");
192        run_cmd(
193            new_smol_command("cargo")
194                .current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."))
195                .args([
196                    "build",
197                    "--package",
198                    "remote_server",
199                    "--features",
200                    "debug-embed",
201                    "--target-dir",
202                    "target/remote_server",
203                    "--target",
204                    &triple,
205                ])
206                .env("RUSTFLAGS", &rust_flags),
207        )
208        .await?;
209    } else {
210        if which("zig", cx).await?.is_none() {
211            anyhow::bail!(if cfg!(not(windows)) {
212                "zig not found on $PATH, install zig (see https://ziglang.org/learn/getting-started or use zigup)"
213            } else {
214                "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)"
215            });
216        }
217
218        let rustup = which("rustup", cx)
219            .await?
220            .context("rustup not found on $PATH, install rustup (see https://rustup.rs/)")?;
221        delegate.set_status(Some("Adding rustup target for cross-compilation"), cx);
222        log::info!("adding rustup target");
223        run_cmd(
224            new_smol_command(rustup)
225                .args(["target", "add"])
226                .arg(&triple),
227        )
228        .await?;
229
230        if which("cargo-zigbuild", cx).await?.is_none() {
231            delegate.set_status(Some("Installing cargo-zigbuild for cross-compilation"), cx);
232            log::info!("installing cargo-zigbuild");
233            run_cmd(new_smol_command("cargo").args(["install", "--locked", "cargo-zigbuild"]))
234                .await?;
235        }
236
237        delegate.set_status(
238            Some(&format!(
239                "Building remote binary from source for {triple} with Zig"
240            )),
241            cx,
242        );
243        log::info!("building remote binary from source for {triple} with Zig");
244        run_cmd(
245            new_smol_command("cargo")
246                .args([
247                    "zigbuild",
248                    "--package",
249                    "remote_server",
250                    "--features",
251                    "debug-embed",
252                    "--target-dir",
253                    "target/remote_server",
254                    "--target",
255                    &triple,
256                ])
257                .env("RUSTFLAGS", &rust_flags),
258        )
259        .await?;
260    };
261    let bin_path = Path::new("target")
262        .join("remote_server")
263        .join(&triple)
264        .join("debug")
265        .join("remote_server");
266
267    let path = if !build_remote_server.contains("nocompress") {
268        delegate.set_status(Some("Compressing binary"), cx);
269
270        #[cfg(not(target_os = "windows"))]
271        {
272            run_cmd(new_smol_command("gzip").args(["-f", &bin_path.to_string_lossy()])).await?;
273        }
274
275        #[cfg(target_os = "windows")]
276        {
277            // On Windows, we use 7z to compress the binary
278
279            let seven_zip = which("7z.exe",cx)
280                .await?
281                .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\"")?;
282            let gz_path = format!("target/remote_server/{}/debug/remote_server.gz", triple);
283            if smol::fs::metadata(&gz_path).await.is_ok() {
284                smol::fs::remove_file(&gz_path).await?;
285            }
286            run_cmd(new_smol_command(seven_zip).args([
287                "a",
288                "-tgzip",
289                &gz_path,
290                &bin_path.to_string_lossy(),
291            ]))
292            .await?;
293        }
294
295        let mut archive_path = bin_path;
296        archive_path.set_extension("gz");
297        std::env::current_dir()?.join(archive_path)
298    } else {
299        bin_path
300    };
301
302    Ok(Some(path))
303}
304
305#[cfg(debug_assertions)]
306async fn which(
307    binary_name: impl AsRef<str>,
308    cx: &mut AsyncApp,
309) -> Result<Option<std::path::PathBuf>> {
310    let binary_name = binary_name.as_ref().to_string();
311    let binary_name_cloned = binary_name.clone();
312    let res = cx
313        .background_spawn(async move { which::which(binary_name_cloned) })
314        .await;
315    match res {
316        Ok(path) => Ok(Some(path)),
317        Err(which::Error::CannotFindBinaryPath) => Ok(None),
318        Err(err) => Err(anyhow::anyhow!(
319            "Failed to run 'which' to find the binary '{binary_name}': {err}"
320        )),
321    }
322}