From 4f95186b537aaeda56d79083c1453af9f09a27b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B0=8F=E7=99=BD?= <364772080@qq.com> Date: Tue, 30 Sep 2025 21:02:46 +0800 Subject: [PATCH] windows: Fix auto-update for `conpty.dll` (#39178) This PR is a follow-up to #39090 and addresses two issues: * Moves `conpty.dll` and `OpenConsole.exe` out of the `bin` folder to prevent other programs from using them. * Updates these files only after Zed exits, avoiding update failures due to file locks. Release Notes: - N/A --------- Co-authored-by: Jakub Konka --- Cargo.lock | 1 - Cargo.toml | 1 - crates/auto_update_helper/src/updater.rs | 46 ++++++++++++++++++++++++ crates/zed/Cargo.toml | 1 - crates/zed/resources/windows/zed.iss | 2 ++ crates/zed/src/main.rs | 17 ++++++--- script/bundle-windows.ps1 | 5 ++- 7 files changed, 63 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 489560796f8b70d1289dc92773653b533d7b12fb..64aab0137a3be79ab3f21c70d0fd0853accdafce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20348,7 +20348,6 @@ dependencies = [ "web_search", "web_search_providers", "windows 0.61.1", - "windows-sys 0.61.0", "winresource", "workspace", "workspace-hack", diff --git a/Cargo.toml b/Cargo.toml index ccba5f615ddd6c8e0f241d41e4d3b862a1ebc2de..ad69e72102db918fdff3c1832d852b6d146ff2f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -714,7 +714,6 @@ wasmtime = { version = "29", default-features = false, features = [ wasmtime-wasi = "29" which = "6.0.0" windows-core = "0.61" -windows-sys = "0.61" wit-component = "0.221" workspace-hack = "0.1.0" yawc = "0.2.5" diff --git a/crates/auto_update_helper/src/updater.rs b/crates/auto_update_helper/src/updater.rs index 2f0f71efc1d68d6afd3cc18d09bc2cbfc9352570..d3d0313593329dcf993d60811f9f8e28b798b396 100644 --- a/crates/auto_update_helper/src/updater.rs +++ b/crates/auto_update_helper/src/updater.rs @@ -38,6 +38,20 @@ pub(crate) const JOBS: &[Job] = &[ std::fs::remove_file(&zed_wsl) .context(format!("Failed to remove old file {}", zed_wsl.display())) }, + |app_dir| { + let open_console = app_dir.join("OpenConsole.exe"); + log::info!("Removing old file: {}", open_console.display()); + std::fs::remove_file(&open_console).context(format!( + "Failed to remove old file {}", + open_console.display() + )) + }, + |app_dir| { + let conpty = app_dir.join("conpty.dll"); + log::info!("Removing old file: {}", conpty.display()); + std::fs::remove_file(&conpty) + .context(format!("Failed to remove old file {}", conpty.display())) + }, // Copy new files |app_dir| { let zed_executable_source = app_dir.join("install\\Zed.exe"); @@ -87,6 +101,38 @@ pub(crate) const JOBS: &[Job] = &[ zed_wsl_dest.display() )) }, + |app_dir| { + let open_console_source = app_dir.join("install\\OpenConsole.exe"); + let open_console_dest = app_dir.join("OpenConsole.exe"); + log::info!( + "Copying new file {} to {}", + open_console_source.display(), + open_console_dest.display() + ); + std::fs::copy(&open_console_source, &open_console_dest) + .map(|_| ()) + .context(format!( + "Failed to copy new file {} to {}", + open_console_source.display(), + open_console_dest.display() + )) + }, + |app_dir| { + let conpty_source = app_dir.join("install\\conpty.dll"); + let conpty_dest = app_dir.join("conpty.dll"); + log::info!( + "Copying new file {} to {}", + conpty_source.display(), + conpty_dest.display() + ); + std::fs::copy(&conpty_source, &conpty_dest) + .map(|_| ()) + .context(format!( + "Failed to copy new file {} to {}", + conpty_source.display(), + conpty_dest.display() + )) + }, // Clean up installer folder and updates folder |app_dir| { let updates_folder = app_dir.join("updates"); diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index e961f12083bee95c388d754af5d24a8aa1b46eae..9664c147f19f0fe3790ec30f283f2bb9a01f523b 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -170,7 +170,6 @@ zlog_settings.workspace = true [target.'cfg(target_os = "windows")'.dependencies] windows.workspace = true -windows-sys.workspace = true [target.'cfg(target_os = "windows")'.build-dependencies] winresource = "0.1" diff --git a/crates/zed/resources/windows/zed.iss b/crates/zed/resources/windows/zed.iss index 53ca8ab01fc53e44220d143f4c7b7a0192900667..b726bb1c2117b1d53f560aaff83acb370c2f2cd4 100644 --- a/crates/zed/resources/windows/zed.iss +++ b/crates/zed/resources/windows/zed.iss @@ -63,6 +63,8 @@ Source: "{#ResourcesDir}\bin\*"; DestDir: "{code:GetInstallDir}\bin"; Flags: ign Source: "{#ResourcesDir}\tools\*"; DestDir: "{app}\tools"; Flags: ignoreversion Source: "{#ResourcesDir}\appx\*"; DestDir: "{app}\appx"; BeforeInstall: RemoveAppxPackage; AfterInstall: AddAppxPackage; Flags: ignoreversion; Check: IsWindows11OrLater Source: "{#ResourcesDir}\amd_ags_x64.dll"; DestDir: "{app}"; Flags: ignoreversion +Source: "{#ResourcesDir}\OpenConsole.exe"; DestDir: "{code:GetInstallDir}"; Flags: ignoreversion +Source: "{#ResourcesDir}\conpty.dll"; DestDir: "{code:GetInstallDir}"; Flags: ignoreversion [Icons] Name: "{group}\{#AppName}"; Filename: "{app}\{#AppExeName}.exe"; AppUserModelID: "{#AppUserId}" diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 8dd5d8b1d612a105b3917d7170ff3904ea05f447..dfa1d7ac4b207d66da34fb87c07bb3a32ae122c9 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -1518,11 +1518,20 @@ fn dump_all_gpui_actions() { .unwrap(); } -#[cfg(windows)] +#[cfg(target_os = "windows")] fn check_for_conpty_dll() { - use windows_sys::{Win32::System::LibraryLoader::LoadLibraryW, w}; - let hmodule = unsafe { LoadLibraryW(w!("conpty.dll")) }; - if hmodule.is_null() { + use windows::{ + Win32::{Foundation::FreeLibrary, System::LibraryLoader::LoadLibraryW}, + core::w, + }; + + if let Ok(hmodule) = unsafe { LoadLibraryW(w!("conpty.dll")) } { + unsafe { + FreeLibrary(hmodule) + .context("Failed to free conpty.dll") + .log_err(); + } + } else { log::warn!("Failed to load conpty.dll. Terminal will work with reduced functionality."); } } diff --git a/script/bundle-windows.ps1 b/script/bundle-windows.ps1 index 7fa7771e720ee2f0a16ca5a9f1dec2d422fb8e55..43023a9e141b77d7770b7ddae3e459cf7159c78e 100644 --- a/script/bundle-windows.ps1 +++ b/script/bundle-windows.ps1 @@ -147,7 +147,6 @@ function DownloadAMDGpuServices { } function DownloadConpty { - # If you update the Conpty version, please also update the version in `crates/zed/build.rs`. $url = "https://www.nuget.org/api/v2/package/CI.Microsoft.Windows.Console.ConPTY/1.22.250314001" $zipPath = ".\conpty.zip" Invoke-WebRequest -Uri $url -OutFile $zipPath @@ -161,8 +160,8 @@ function CollectFiles { Move-Item -Path "$innoDir\zed.sh" -Destination "$innoDir\bin\zed" -Force Move-Item -Path "$innoDir\auto_update_helper.exe" -Destination "$innoDir\tools\auto_update_helper.exe" -Force Move-Item -Path ".\AGS_SDK-6.3.0\ags_lib\lib\amd_ags_x64.dll" -Destination "$innoDir\amd_ags_x64.dll" -Force - Move-Item -Path ".\conpty\build\native\runtimes\*" -Destination "$innoDir\bin" -Force - Move-Item -Path ".\conpty\runtimes\win10-x64\native\conpty.dll" -Destination "$innoDir\bin\conpty.dll" -Force + Move-Item -Path ".\conpty\build\native\runtimes\x64\OpenConsole.exe" -Destination "$innoDir\OpenConsole.exe" -Force + Move-Item -Path ".\conpty\runtimes\win10-x64\native\conpty.dll" -Destination "$innoDir\conpty.dll" -Force } function BuildInstaller {