wsl.rs

  1use crate::{
  2    RemoteArch, RemoteClientDelegate, RemoteOs, RemotePlatform,
  3    remote_client::{CommandTemplate, Interactive, RemoteConnection, RemoteConnectionOptions},
  4    transport::{parse_platform, parse_shell},
  5};
  6use anyhow::{Context, Result, anyhow, bail};
  7use async_trait::async_trait;
  8use collections::HashMap;
  9use futures::channel::mpsc::{Sender, UnboundedReceiver, UnboundedSender};
 10use gpui::{App, AppContext as _, AsyncApp, Task};
 11use release_channel::{AppVersion, ReleaseChannel};
 12use rpc::proto::Envelope;
 13use semver::Version;
 14use smol::fs;
 15use std::{
 16    ffi::OsStr,
 17    fmt::Write as _,
 18    path::{Path, PathBuf},
 19    sync::Arc,
 20    time::Instant,
 21};
 22
 23use util::{
 24    command::Stdio,
 25    paths::{PathStyle, RemotePathBuf},
 26    rel_path::RelPath,
 27    shell::{Shell, ShellKind},
 28    shell_builder::ShellBuilder,
 29};
 30
 31#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Deserialize, schemars::JsonSchema)]
 32pub struct WslConnectionOptions {
 33    pub distro_name: String,
 34    pub user: Option<String>,
 35}
 36
 37impl From<settings::WslConnection> for WslConnectionOptions {
 38    fn from(val: settings::WslConnection) -> Self {
 39        WslConnectionOptions {
 40            distro_name: val.distro_name,
 41            user: val.user,
 42        }
 43    }
 44}
 45
 46#[derive(Debug)]
 47pub(crate) struct WslRemoteConnection {
 48    remote_binary_path: Option<Arc<RelPath>>,
 49    platform: RemotePlatform,
 50    shell: String,
 51    shell_kind: ShellKind,
 52    default_system_shell: String,
 53    has_wsl_interop: bool,
 54    connection_options: WslConnectionOptions,
 55}
 56
 57impl WslRemoteConnection {
 58    pub(crate) async fn new(
 59        connection_options: WslConnectionOptions,
 60        delegate: Arc<dyn RemoteClientDelegate>,
 61        cx: &mut AsyncApp,
 62    ) -> Result<Self> {
 63        log::info!(
 64            "Connecting to WSL distro {} with user {:?}",
 65            connection_options.distro_name,
 66            connection_options.user
 67        );
 68        let (release_channel, version) =
 69            cx.update(|cx| (ReleaseChannel::global(cx), AppVersion::global(cx)));
 70
 71        let mut this = Self {
 72            connection_options,
 73            remote_binary_path: None,
 74            platform: RemotePlatform {
 75                os: RemoteOs::Linux,
 76                arch: RemoteArch::X86_64,
 77            },
 78            shell: String::new(),
 79            shell_kind: ShellKind::Posix,
 80            default_system_shell: String::from("/bin/sh"),
 81            has_wsl_interop: false,
 82        };
 83        delegate.set_status(Some("Detecting WSL environment"), cx);
 84        this.shell = this
 85            .detect_shell()
 86            .await
 87            .context("failed detecting shell")?;
 88        log::info!("Remote shell discovered: {}", this.shell);
 89        this.shell_kind = ShellKind::new(&this.shell, false);
 90        this.has_wsl_interop = this.detect_has_wsl_interop().await.unwrap_or_default();
 91        log::info!(
 92            "Remote has wsl interop {}",
 93            if this.has_wsl_interop {
 94                "enabled"
 95            } else {
 96                "disabled"
 97            }
 98        );
 99        this.platform = this
100            .detect_platform()
101            .await
102            .context("failed detecting platform")?;
103        log::info!("Remote platform discovered: {:?}", this.platform);
104        this.remote_binary_path = Some(
105            this.ensure_server_binary(&delegate, release_channel, version, cx)
106                .await
107                .context("failed ensuring server binary")?,
108        );
109        log::debug!("Detected WSL environment: {this:#?}");
110
111        Ok(this)
112    }
113
114    async fn detect_platform(&self) -> Result<RemotePlatform> {
115        let program = self.shell_kind.prepend_command_prefix("uname");
116        let output = self.run_wsl_command_with_output(&program, &["-sm"]).await?;
117        parse_platform(&output)
118    }
119
120    async fn detect_shell(&self) -> Result<String> {
121        const DEFAULT_SHELL: &str = "sh";
122        match self
123            .run_wsl_command_with_output("sh", &["-c", "echo $SHELL"])
124            .await
125        {
126            Ok(output) => Ok(parse_shell(&output, DEFAULT_SHELL)),
127            Err(e) => {
128                log::error!("Failed to detect remote shell: {e}");
129                Ok(DEFAULT_SHELL.to_owned())
130            }
131        }
132    }
133
134    async fn detect_has_wsl_interop(&self) -> Result<bool> {
135        let interop = match self
136            .run_wsl_command_with_output("cat", &["/proc/sys/fs/binfmt_misc/WSLInterop"])
137            .await
138        {
139            Ok(interop) => interop,
140            Err(err) => self
141                .run_wsl_command_with_output("cat", &["/proc/sys/fs/binfmt_misc/WSLInterop-late"])
142                .await
143                .inspect_err(|err2| log::error!("Failed to detect wsl interop: {err}; {err2}"))?,
144        };
145        Ok(interop.contains("enabled"))
146    }
147
148    async fn windows_path_to_wsl_path(&self, source: &Path) -> Result<String> {
149        windows_path_to_wsl_path_impl(&self.connection_options, source).await
150    }
151
152    async fn run_wsl_command_with_output(&self, program: &str, args: &[&str]) -> Result<String> {
153        run_wsl_command_with_output_impl(&self.connection_options, program, args).await
154    }
155
156    async fn run_wsl_command(&self, program: &str, args: &[&str]) -> Result<()> {
157        run_wsl_command_impl(wsl_command_impl(
158            &self.connection_options,
159            program,
160            args,
161            false,
162        ))
163        .await
164        .map(|_| ())
165    }
166
167    async fn ensure_server_binary(
168        &self,
169        delegate: &Arc<dyn RemoteClientDelegate>,
170        release_channel: ReleaseChannel,
171        version: Version,
172        cx: &mut AsyncApp,
173    ) -> Result<Arc<RelPath>> {
174        let version_str = match release_channel {
175            ReleaseChannel::Dev => "build".to_string(),
176            _ => version.to_string(),
177        };
178
179        let binary_name = format!(
180            "zed-remote-server-{}-{}",
181            release_channel.dev_name(),
182            version_str
183        );
184
185        let dst_path =
186            paths::remote_server_dir_relative().join(RelPath::unix(&binary_name).unwrap());
187
188        if let Some(parent) = dst_path.parent() {
189            let parent = parent.display(PathStyle::Posix);
190            let mkdir = self.shell_kind.prepend_command_prefix("mkdir");
191            self.run_wsl_command(&mkdir, &["-p", &parent])
192                .await
193                .map_err(|e| anyhow!("Failed to create directory: {}", e))?;
194        }
195
196        let binary_exists_on_server = self
197            .run_wsl_command(&dst_path.display(PathStyle::Posix), &["version"])
198            .await
199            .is_ok();
200
201        #[cfg(any(debug_assertions, feature = "build-remote-server-binary"))]
202        if let Some(remote_server_path) = super::build_remote_server_from_source(
203            &self.platform,
204            delegate.as_ref(),
205            binary_exists_on_server,
206            cx,
207        )
208        .await?
209        {
210            let tmp_path = paths::remote_server_dir_relative().join(
211                &RelPath::unix(&format!(
212                    "download-{}-{}",
213                    std::process::id(),
214                    remote_server_path.file_name().unwrap().to_string_lossy()
215                ))
216                .unwrap(),
217            );
218            self.upload_file(&remote_server_path, &tmp_path, delegate, cx)
219                .await?;
220            self.extract_and_install(&tmp_path, &dst_path, delegate, cx)
221                .await?;
222            return Ok(dst_path);
223        }
224
225        if binary_exists_on_server {
226            return Ok(dst_path);
227        }
228
229        let wanted_version = match release_channel {
230            ReleaseChannel::Nightly | ReleaseChannel::Dev => None,
231            _ => Some(cx.update(|cx| AppVersion::global(cx))),
232        };
233
234        let src_path = delegate
235            .download_server_binary_locally(self.platform, release_channel, wanted_version, cx)
236            .await?;
237
238        let tmp_path = format!(
239            "{}.{}.gz",
240            dst_path.display(PathStyle::Posix),
241            std::process::id()
242        );
243        let tmp_path = RelPath::unix(&tmp_path).unwrap();
244
245        self.upload_file(&src_path, &tmp_path, delegate, cx).await?;
246        self.extract_and_install(&tmp_path, &dst_path, delegate, cx)
247            .await?;
248
249        Ok(dst_path)
250    }
251
252    async fn upload_file(
253        &self,
254        src_path: &Path,
255        dst_path: &RelPath,
256        delegate: &Arc<dyn RemoteClientDelegate>,
257        cx: &mut AsyncApp,
258    ) -> Result<()> {
259        delegate.set_status(Some("Uploading remote server"), cx);
260
261        if let Some(parent) = dst_path.parent() {
262            let parent = parent.display(PathStyle::Posix);
263            let mkdir = self.shell_kind.prepend_command_prefix("mkdir");
264            self.run_wsl_command(&mkdir, &["-p", &parent])
265                .await
266                .context("Failed to create directory when uploading file")?;
267        }
268
269        let t0 = Instant::now();
270        let src_stat = fs::metadata(&src_path)
271            .await
272            .with_context(|| format!("source path does not exist: {}", src_path.display()))?;
273        let size = src_stat.len();
274        log::info!(
275            "uploading remote server to WSL {:?} ({}kb)",
276            dst_path,
277            size / 1024
278        );
279
280        let src_path_in_wsl = self.windows_path_to_wsl_path(src_path).await?;
281        let cp = self.shell_kind.prepend_command_prefix("cp");
282        self.run_wsl_command(
283            &cp,
284            &["-f", &src_path_in_wsl, &dst_path.display(PathStyle::Posix)],
285        )
286        .await
287        .map_err(|e| {
288            anyhow!(
289                "Failed to copy file {}({}) to WSL {:?}: {}",
290                src_path.display(),
291                src_path_in_wsl,
292                dst_path,
293                e
294            )
295        })?;
296
297        log::info!("uploaded remote server in {:?}", t0.elapsed());
298        Ok(())
299    }
300
301    async fn extract_and_install(
302        &self,
303        tmp_path: &RelPath,
304        dst_path: &RelPath,
305        delegate: &Arc<dyn RemoteClientDelegate>,
306        cx: &mut AsyncApp,
307    ) -> Result<()> {
308        delegate.set_status(Some("Extracting remote server"), cx);
309
310        let tmp_path_str = tmp_path.display(PathStyle::Posix);
311        let dst_path_str = dst_path.display(PathStyle::Posix);
312
313        // Build extraction script with proper error handling
314        let script = if tmp_path_str.ends_with(".gz") {
315            let uncompressed = tmp_path_str.trim_end_matches(".gz");
316            format!(
317                "set -e; gunzip -f '{}' && chmod 755 '{}' && mv -f '{}' '{}'",
318                tmp_path_str, uncompressed, uncompressed, dst_path_str
319            )
320        } else {
321            format!(
322                "set -e; chmod 755 '{}' && mv -f '{}' '{}'",
323                tmp_path_str, tmp_path_str, dst_path_str
324            )
325        };
326
327        self.run_wsl_command("sh", &["-c", &script])
328            .await
329            .map_err(|e| anyhow!("Failed to extract server binary: {}", e))?;
330        Ok(())
331    }
332}
333
334#[async_trait(?Send)]
335impl RemoteConnection for WslRemoteConnection {
336    fn start_proxy(
337        &self,
338        unique_identifier: String,
339        reconnect: bool,
340        incoming_tx: UnboundedSender<Envelope>,
341        outgoing_rx: UnboundedReceiver<Envelope>,
342        connection_activity_tx: Sender<()>,
343        delegate: Arc<dyn RemoteClientDelegate>,
344        cx: &mut AsyncApp,
345    ) -> Task<Result<i32>> {
346        delegate.set_status(Some("Starting proxy"), cx);
347
348        let Some(remote_binary_path) = &self.remote_binary_path else {
349            return Task::ready(Err(anyhow!("Remote binary path not set")));
350        };
351
352        let mut proxy_args = vec![];
353        for env_var in ["RUST_LOG", "RUST_BACKTRACE", "ZED_GENERATE_MINIDUMPS"] {
354            if let Some(value) = std::env::var(env_var).ok() {
355                // We don't quote the value here as it seems excessive and may result in invalid envs for the
356                // proxy server. For example, `RUST_LOG='debug'` will result in a warning "invalid logging spec 'debug'', ignoring it"
357                // in the proxy server. Therefore, we pass the env vars as is.
358                proxy_args.push(format!("{}={}", env_var, value));
359            }
360        }
361
362        proxy_args.push(remote_binary_path.display(PathStyle::Posix).into_owned());
363        proxy_args.push("proxy".to_owned());
364        proxy_args.push("--identifier".to_owned());
365        proxy_args.push(unique_identifier);
366
367        if reconnect {
368            proxy_args.push("--reconnect".to_owned());
369        }
370
371        let proxy_process =
372            match wsl_command_impl(&self.connection_options, "env", &proxy_args, true)
373                .kill_on_drop(true)
374                .spawn()
375            {
376                Ok(process) => process,
377                Err(error) => {
378                    return Task::ready(Err(anyhow!("failed to spawn remote server: {}", error)));
379                }
380            };
381
382        super::handle_rpc_messages_over_child_process_stdio(
383            proxy_process,
384            incoming_tx,
385            outgoing_rx,
386            connection_activity_tx,
387            cx,
388        )
389    }
390
391    fn upload_directory(
392        &self,
393        src_path: PathBuf,
394        dest_path: RemotePathBuf,
395        cx: &App,
396    ) -> Task<Result<()>> {
397        cx.background_spawn({
398            let options = self.connection_options.clone();
399            async move {
400                let wsl_src = windows_path_to_wsl_path_impl(&options, &src_path).await?;
401                let command = wsl_command_impl(
402                    &options,
403                    "cp",
404                    &["-r", &wsl_src, &dest_path.to_string()],
405                    true,
406                );
407                run_wsl_command_impl(command).await.map_err(|e| {
408                    anyhow!(
409                        "failed to upload directory {} -> {}: {}",
410                        src_path.display(),
411                        dest_path,
412                        e
413                    )
414                })?;
415
416                Ok(())
417            }
418        })
419    }
420
421    async fn kill(&self) -> Result<()> {
422        Ok(())
423    }
424
425    fn has_been_killed(&self) -> bool {
426        false
427    }
428
429    fn shares_network_interface(&self) -> bool {
430        true
431    }
432
433    fn build_command(
434        &self,
435        program: Option<String>,
436        args: &[String],
437        env: &HashMap<String, String>,
438        working_dir: Option<String>,
439        port_forward: Option<(u16, String, u16)>,
440        _interactive: Interactive,
441    ) -> Result<CommandTemplate> {
442        if port_forward.is_some() {
443            bail!("WSL shares the network interface with the host system");
444        }
445
446        let shell_kind = self.shell_kind;
447        let working_dir = working_dir
448            .map(|working_dir| RemotePathBuf::new(working_dir, PathStyle::Posix).to_string())
449            .unwrap_or("~".to_string());
450
451        let mut exec = String::from("exec env ");
452
453        for (k, v) in env.iter() {
454            write!(
455                exec,
456                "{}={} ",
457                k,
458                shell_kind.try_quote(v).context("shell quoting")?
459            )?;
460        }
461
462        if let Some(program) = program {
463            write!(
464                exec,
465                "{}",
466                shell_kind
467                    .try_quote_prefix_aware(&program)
468                    .context("shell quoting")?
469            )?;
470            for arg in args {
471                let arg = shell_kind.try_quote(&arg).context("shell quoting")?;
472                write!(exec, " {}", &arg)?;
473            }
474        } else {
475            write!(&mut exec, "{} -l", self.shell)?;
476        }
477        let (command, args) =
478            ShellBuilder::new(&Shell::Program(self.shell.clone()), false).build(Some(exec), &[]);
479
480        let mut wsl_args = if let Some(user) = &self.connection_options.user {
481            vec![
482                "--distribution".to_string(),
483                self.connection_options.distro_name.clone(),
484                "--user".to_string(),
485                user.clone(),
486                "--cd".to_string(),
487                working_dir,
488                "--".to_string(),
489                command,
490            ]
491        } else {
492            vec![
493                "--distribution".to_string(),
494                self.connection_options.distro_name.clone(),
495                "--cd".to_string(),
496                working_dir,
497                "--".to_string(),
498                command,
499            ]
500        };
501        wsl_args.extend(args);
502
503        Ok(CommandTemplate {
504            program: "wsl.exe".to_string(),
505            args: wsl_args,
506            env: HashMap::default(),
507        })
508    }
509
510    fn build_forward_ports_command(
511        &self,
512        _: Vec<(u16, String, u16)>,
513    ) -> anyhow::Result<CommandTemplate> {
514        Err(anyhow!("WSL shares a network interface with the host"))
515    }
516
517    fn connection_options(&self) -> RemoteConnectionOptions {
518        RemoteConnectionOptions::Wsl(self.connection_options.clone())
519    }
520
521    fn path_style(&self) -> PathStyle {
522        PathStyle::Posix
523    }
524
525    fn shell(&self) -> String {
526        self.shell.clone()
527    }
528
529    fn default_system_shell(&self) -> String {
530        self.default_system_shell.clone()
531    }
532
533    fn has_wsl_interop(&self) -> bool {
534        self.has_wsl_interop
535    }
536}
537
538/// `wslpath` is a executable available in WSL, it's a linux binary.
539/// So it doesn't support Windows style paths.
540async fn sanitize_path(path: &Path) -> Result<String> {
541    let path = smol::fs::canonicalize(path)
542        .await
543        .with_context(|| format!("Failed to canonicalize path {}", path.display()))?;
544    let path_str = path.to_string_lossy();
545
546    let sanitized = path_str.strip_prefix(r"\\?\").unwrap_or(&path_str);
547    Ok(sanitized.replace('\\', "/"))
548}
549
550fn run_wsl_command_with_output_impl(
551    options: &WslConnectionOptions,
552    program: &str,
553    args: &[&str],
554) -> impl Future<Output = Result<String>> + use<> {
555    let exec_command = wsl_command_impl(options, program, args, true);
556    let command = wsl_command_impl(options, program, args, false);
557    async move {
558        match run_wsl_command_impl(exec_command).await {
559            Ok(res) => Ok(res),
560            Err(exec_err) => match run_wsl_command_impl(command).await {
561                Ok(res) => Ok(res),
562                Err(e) => Err(e.context(exec_err)),
563            },
564        }
565    }
566}
567
568impl WslConnectionOptions {
569    pub fn abs_windows_path_to_wsl_path(
570        &self,
571        source: &Path,
572    ) -> impl Future<Output = Result<String>> + use<> {
573        let path_str = source.to_string_lossy();
574
575        let source = path_str.strip_prefix(r"\\?\").unwrap_or(&*path_str);
576        let source = source.replace('\\', "/");
577        run_wsl_command_with_output_impl(self, "wslpath", &["-u", &source])
578    }
579}
580
581async fn windows_path_to_wsl_path_impl(
582    options: &WslConnectionOptions,
583    source: &Path,
584) -> Result<String> {
585    let source = sanitize_path(source).await?;
586    run_wsl_command_with_output_impl(options, "wslpath", &["-u", &source]).await
587}
588
589/// Converts a WSL/POSIX path to a Windows path using `wslpath -w`.
590///
591/// For example, `/home/user/project` becomes `\\wsl.localhost\Ubuntu\home\user\project`
592#[cfg(target_os = "windows")]
593pub fn wsl_path_to_windows_path(
594    options: &WslConnectionOptions,
595    wsl_path: &Path,
596) -> impl Future<Output = Result<PathBuf>> + use<> {
597    let wsl_path_str = wsl_path.to_string_lossy().to_string();
598    let command = wsl_command_impl(options, "wslpath", &["-w", &wsl_path_str], true);
599    async move {
600        let windows_path = run_wsl_command_impl(command).await?;
601        Ok(PathBuf::from(windows_path))
602    }
603}
604
605fn run_wsl_command_impl(
606    mut command: util::command::Command,
607) -> impl Future<Output = Result<String>> {
608    async move {
609        let output = command
610            .output()
611            .await
612            .with_context(|| format!("Failed to run command '{:?}'", command))?;
613
614        if !output.status.success() {
615            return Err(anyhow!(
616                "Command '{:?}' failed: {}",
617                command,
618                String::from_utf8_lossy(&output.stderr).trim()
619            ));
620        }
621
622        Ok(String::from_utf8_lossy(&output.stdout).trim().to_owned())
623    }
624}
625
626/// Creates a new `wsl.exe` command that runs the given program with the given arguments.
627///
628/// If `exec` is true, the command will be executed in the WSL environment without spawning a new shell.
629fn wsl_command_impl(
630    options: &WslConnectionOptions,
631    program: &str,
632    args: &[impl AsRef<OsStr>],
633    exec: bool,
634) -> util::command::Command {
635    let mut command = util::command::new_command("wsl.exe");
636
637    if let Some(user) = &options.user {
638        command.arg("--user").arg(user);
639    }
640
641    command
642        .stdin(Stdio::piped())
643        .stdout(Stdio::piped())
644        .stderr(Stdio::piped())
645        .arg("--distribution")
646        .arg(&options.distro_name)
647        .arg("--cd")
648        .arg("~");
649
650    if exec {
651        command.arg("--exec");
652    }
653
654    command.arg(program).args(args);
655
656    log::debug!("wsl {:?}", command);
657    command
658}