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 (key, value) in env.iter() {
454            let assignment = format!("{key}={value}");
455            let assignment = shell_kind.try_quote(&assignment).context("shell quoting")?;
456            write!(exec, "{assignment} ")?;
457        }
458
459        if let Some(program) = program {
460            write!(
461                exec,
462                "{}",
463                shell_kind
464                    .try_quote_prefix_aware(&program)
465                    .context("shell quoting")?
466            )?;
467            for arg in args {
468                let arg = shell_kind.try_quote(&arg).context("shell quoting")?;
469                write!(exec, " {}", &arg)?;
470            }
471        } else {
472            write!(&mut exec, "{} -l", self.shell)?;
473        }
474        let (command, args) =
475            ShellBuilder::new(&Shell::Program(self.shell.clone()), false).build(Some(exec), &[]);
476
477        let mut wsl_args = if let Some(user) = &self.connection_options.user {
478            vec![
479                "--distribution".to_string(),
480                self.connection_options.distro_name.clone(),
481                "--user".to_string(),
482                user.clone(),
483                "--cd".to_string(),
484                working_dir,
485                "--".to_string(),
486                command,
487            ]
488        } else {
489            vec![
490                "--distribution".to_string(),
491                self.connection_options.distro_name.clone(),
492                "--cd".to_string(),
493                working_dir,
494                "--".to_string(),
495                command,
496            ]
497        };
498        wsl_args.extend(args);
499
500        Ok(CommandTemplate {
501            program: "wsl.exe".to_string(),
502            args: wsl_args,
503            env: HashMap::default(),
504        })
505    }
506
507    fn build_forward_ports_command(
508        &self,
509        _: Vec<(u16, String, u16)>,
510    ) -> anyhow::Result<CommandTemplate> {
511        Err(anyhow!("WSL shares a network interface with the host"))
512    }
513
514    fn connection_options(&self) -> RemoteConnectionOptions {
515        RemoteConnectionOptions::Wsl(self.connection_options.clone())
516    }
517
518    fn path_style(&self) -> PathStyle {
519        PathStyle::Posix
520    }
521
522    fn shell(&self) -> String {
523        self.shell.clone()
524    }
525
526    fn default_system_shell(&self) -> String {
527        self.default_system_shell.clone()
528    }
529
530    fn has_wsl_interop(&self) -> bool {
531        self.has_wsl_interop
532    }
533}
534
535/// `wslpath` is a executable available in WSL, it's a linux binary.
536/// So it doesn't support Windows style paths.
537async fn sanitize_path(path: &Path) -> Result<String> {
538    let path = smol::fs::canonicalize(path)
539        .await
540        .with_context(|| format!("Failed to canonicalize path {}", path.display()))?;
541    let path_str = path.to_string_lossy();
542
543    let sanitized = path_str.strip_prefix(r"\\?\").unwrap_or(&path_str);
544    Ok(sanitized.replace('\\', "/"))
545}
546
547fn run_wsl_command_with_output_impl(
548    options: &WslConnectionOptions,
549    program: &str,
550    args: &[&str],
551) -> impl Future<Output = Result<String>> + use<> {
552    let exec_command = wsl_command_impl(options, program, args, true);
553    let command = wsl_command_impl(options, program, args, false);
554    async move {
555        match run_wsl_command_impl(exec_command).await {
556            Ok(res) => Ok(res),
557            Err(exec_err) => match run_wsl_command_impl(command).await {
558                Ok(res) => Ok(res),
559                Err(e) => Err(e.context(exec_err)),
560            },
561        }
562    }
563}
564
565impl WslConnectionOptions {
566    pub fn abs_windows_path_to_wsl_path(
567        &self,
568        source: &Path,
569    ) -> impl Future<Output = Result<String>> + use<> {
570        let path_str = source.to_string_lossy();
571
572        let source = path_str.strip_prefix(r"\\?\").unwrap_or(&*path_str);
573        let source = source.replace('\\', "/");
574        run_wsl_command_with_output_impl(self, "wslpath", &["-u", &source])
575    }
576}
577
578async fn windows_path_to_wsl_path_impl(
579    options: &WslConnectionOptions,
580    source: &Path,
581) -> Result<String> {
582    let source = sanitize_path(source).await?;
583    run_wsl_command_with_output_impl(options, "wslpath", &["-u", &source]).await
584}
585
586/// Converts a WSL/POSIX path to a Windows path using `wslpath -w`.
587///
588/// For example, `/home/user/project` becomes `\\wsl.localhost\Ubuntu\home\user\project`
589#[cfg(target_os = "windows")]
590pub fn wsl_path_to_windows_path(
591    options: &WslConnectionOptions,
592    wsl_path: &Path,
593) -> impl Future<Output = Result<PathBuf>> + use<> {
594    let wsl_path_str = wsl_path.to_string_lossy().to_string();
595    let command = wsl_command_impl(options, "wslpath", &["-w", &wsl_path_str], true);
596    async move {
597        let windows_path = run_wsl_command_impl(command).await?;
598        Ok(PathBuf::from(windows_path))
599    }
600}
601
602fn run_wsl_command_impl(
603    mut command: util::command::Command,
604) -> impl Future<Output = Result<String>> {
605    async move {
606        let output = command
607            .output()
608            .await
609            .with_context(|| format!("Failed to run command '{:?}'", command))?;
610
611        if !output.status.success() {
612            return Err(anyhow!(
613                "Command '{:?}' failed: {}",
614                command,
615                String::from_utf8_lossy(&output.stderr).trim()
616            ));
617        }
618
619        Ok(String::from_utf8_lossy(&output.stdout).trim().to_owned())
620    }
621}
622
623/// Creates a new `wsl.exe` command that runs the given program with the given arguments.
624///
625/// If `exec` is true, the command will be executed in the WSL environment without spawning a new shell.
626fn wsl_command_impl(
627    options: &WslConnectionOptions,
628    program: &str,
629    args: &[impl AsRef<OsStr>],
630    exec: bool,
631) -> util::command::Command {
632    let mut command = util::command::new_command("wsl.exe");
633
634    if let Some(user) = &options.user {
635        command.arg("--user").arg(user);
636    }
637
638    command
639        .stdin(Stdio::piped())
640        .stdout(Stdio::piped())
641        .stderr(Stdio::piped())
642        .arg("--distribution")
643        .arg(&options.distro_name)
644        .arg("--cd")
645        .arg("~");
646
647    if exec {
648        command.arg("--exec");
649    }
650
651    command.arg(program).args(args);
652
653    log::debug!("wsl {:?}", command);
654    command
655}