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