From c3807043af5dd4b8358c258ae97afa73c6e33180 Mon Sep 17 00:00:00 2001 From: Francesco Bonacci Date: Sun, 24 May 2026 14:04:29 +0000 Subject: [PATCH] fix(cua-driver-rs)(install): re-register cua-driver-serve task on every install when it already exists Symptom: user upgrades cua-driver (e.g. via install-local.ps1 to pick up the hidden-console wrapper from #1654), then runs `cua-driver autostart kick`, then sees a visible console window again. Repro confirmed: the scheduled task's path stayed hard-pointed at the previous release-install dir (a binary lacking the wrapper code), so kick spawned the OLD binary. Both install.ps1 and install-local.ps1 only re-registered the task when -AutoStart was passed. Users on the upgrade path don't pass it (they're not opting INTO autostart - they already have it). Fix: both scripts now sniff for an existing `cua-driver-serve` task post-install and re-register it pointing at the freshly-installed binary, even without -AutoStart. The re-register is idempotent and covers the upgrade case explicitly. If no task is registered, nothing changes (still need -AutoStart to opt in initially). End-to-end: user runs install-local.ps1, the just-built binary's REGISTER_PS produces the wrapped task action, kick now spawns hidden. Replaced em-dashes in added comments with ASCII hyphens - the file gets rewritten as UTF-8 on edit, but PS 5.1's parser had read older em-dashes in the same file as windows-1252 successfully (mixed- encoding), and treating new bytes as UTF-8 surfaced unterminated- string errors. Stay on ASCII in newly-added blocks to avoid drift. --- libs/cua-driver/scripts/install-local.ps1 | 29 +++++++++++++++++++++++ libs/cua-driver/scripts/install.ps1 | 26 ++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/libs/cua-driver/scripts/install-local.ps1 b/libs/cua-driver/scripts/install-local.ps1 index 265eac8fe0..cc7c274a61 100644 --- a/libs/cua-driver/scripts/install-local.ps1 +++ b/libs/cua-driver/scripts/install-local.ps1 @@ -239,6 +239,35 @@ if ($AutoStart) { catch { Write-Host " Failed to register: $($_.Exception.Message)" -ForegroundColor Red } +} else { + # User didn't pass -AutoStart, but if a `cua-driver-serve` task is + # ALREADY registered (from a previous `install.ps1 -AutoStart` or + # `cua-driver autostart enable`), re-register it pointing at this + # fresh binary. Otherwise the user ends up with a task whose + # path is the OLD release-install dir, running the OLD + # binary - even though `cua-driver` on PATH now resolves to the + # fresh one. See trycua/cua#1654 (hidden-console wrapper landed + # later - old tasks that survived an upgrade still produce the + # visible console window at logon). + $prevEAP = $ErrorActionPreference + $ErrorActionPreference = 'Continue' + try { + & schtasks.exe /Query /TN "cua-driver-serve" 2>$null | Out-Null + $hasTask = ($LASTEXITCODE -eq 0) + } finally { + $ErrorActionPreference = $prevEAP + } + if ($hasTask) { + Write-Step "found existing 'cua-driver-serve' task - re-registering against fresh binary" + try { + Register-CuaDriverAutostart -InstalledBinary (Join-Path $VisibleBinDir $BinaryName) + Write-Host " Re-registered. Task action now uses this build's hidden-console wrapper." -ForegroundColor Green + } + catch { + Write-Host " Failed to re-register: $($_.Exception.Message)" -ForegroundColor Red + Write-Host " The existing task still points at the previous binary. Run 'cua-driver autostart enable' from an elevated shell to update." + } + } } # Unified post-install hints come from a single shared text file so the diff --git a/libs/cua-driver/scripts/install.ps1 b/libs/cua-driver/scripts/install.ps1 index 34c1fcf46e..0598b7c4a0 100644 --- a/libs/cua-driver/scripts/install.ps1 +++ b/libs/cua-driver/scripts/install.ps1 @@ -1173,6 +1173,32 @@ if ($AutoStart) { Write-Host " In THIS shell (if already elevated), use: $installedBinary autostart enable" Write-Host "" } +} else { + # No -AutoStart, but if a `cua-driver-serve` task is already + # registered, re-register it against the fresh binary. Otherwise + # the task still points at the previous release dir + an + # older binary that may be missing the hidden-console wrapper (#1654) + # or any later autostart-shape fix. + $prevEAP = $ErrorActionPreference + $ErrorActionPreference = 'Continue' + try { + & schtasks.exe /Query /TN "cua-driver-serve" 2>$null | Out-Null + $hasTask = ($LASTEXITCODE -eq 0) + } finally { + $ErrorActionPreference = $prevEAP + } + if ($hasTask) { + Write-Host "" + Write-Host "Existing 'cua-driver-serve' autostart task detected - re-registering against the fresh binary..." -ForegroundColor Cyan + try { + Register-CuaDriverAutostart -InstalledBinary $installedBinary + Write-Host " Re-registered. Task action now uses this build's hidden-console wrapper." -ForegroundColor Green + } + catch { + Write-Host " Failed to re-register: $($_.Exception.Message)" -ForegroundColor Red + Write-Host " The existing task still points at the previous binary. Run 'cua-driver autostart enable' from an elevated shell to update." + } + } } # Unified post-install hints come from a single shared text file so the