remote_server: Cleanup old server binaries on wsl (#47839)

Lukas Wirth created

Closes https://github.com/zed-industries/zed/issues/44736

Release Notes:

- Fixed remote server binaries accumulating on wsl over time

Change summary

crates/paths/src/paths.rs          |  1 +
crates/remote/src/transport/wsl.rs |  4 ++--
crates/remote_server/src/server.rs | 16 ++++++++++++++--
3 files changed, 17 insertions(+), 4 deletions(-)

Detailed changes

crates/paths/src/paths.rs 🔗

@@ -36,6 +36,7 @@ pub fn remote_server_dir_relative() -> &'static RelPath {
     *CACHED
 }
 
+// Remove this once 223 goes stable
 /// Returns the relative path to the zed_wsl_server directory on the wsl host.
 pub fn remote_wsl_server_dir_relative() -> &'static RelPath {
     static CACHED: LazyLock<&'static RelPath> =

crates/remote/src/transport/wsl.rs 🔗

@@ -176,7 +176,7 @@ impl WslRemoteConnection {
         );
 
         let dst_path =
-            paths::remote_wsl_server_dir_relative().join(RelPath::unix(&binary_name).unwrap());
+            paths::remote_server_dir_relative().join(RelPath::unix(&binary_name).unwrap());
 
         if let Some(parent) = dst_path.parent() {
             let parent = parent.display(PathStyle::Posix);
@@ -200,7 +200,7 @@ impl WslRemoteConnection {
         )
         .await?
         {
-            let tmp_path = paths::remote_wsl_server_dir_relative().join(
+            let tmp_path = paths::remote_server_dir_relative().join(
                 &RelPath::unix(&format!(
                     "download-{}-{}",
                     std::process::id(),

crates/remote_server/src/server.rs 🔗

@@ -575,8 +575,11 @@ pub fn execute_run(
 
         handle_crash_files_requests(&project, &session);
 
-        cx.background_spawn(async move { cleanup_old_binaries() })
-            .detach();
+        cx.background_spawn(async move {
+            cleanup_old_binaries_wsl();
+            cleanup_old_binaries()
+        })
+        .detach();
 
         mem::forget(project);
     };
@@ -1174,6 +1177,15 @@ fn cleanup_old_binaries() -> Result<()> {
     Ok(())
 }
 
+// Remove this once 223 goes stable, we only have this to clean up old binaries on WSL
+// we no longer download them into this folder, we use the same folder as other remote servers
+fn cleanup_old_binaries_wsl() {
+    let server_dir = paths::remote_wsl_server_dir_relative();
+    if let Ok(()) = std::fs::remove_dir_all(server_dir.as_std_path()) {
+        log::info!("removing old wsl remote server folder: {:?}", server_dir);
+    }
+}
+
 fn is_new_version(version: &str) -> bool {
     semver::Version::from_str(version)
         .ok()