diff --git a/apps/launcher/src/main/services/app-update/app-update.ts b/apps/launcher/src/main/services/app-update/app-update.ts index 3fe966b..62bc40d 100644 --- a/apps/launcher/src/main/services/app-update/app-update.ts +++ b/apps/launcher/src/main/services/app-update/app-update.ts @@ -22,7 +22,9 @@ import { FeedUnreachableError, readReleaseAsset, releaseAssetChunks, RELEASE_MANIFEST_MAX_BYTES, RELEASE_SIGNATURE_MAX_BYTES, requestReleaseAsset, } from '../patch-pack/release-assets.ts'; import { installerDigest, launcherSignaturePayload, portableSignaturePayload } from './installer-integrity.ts'; -import { isPortableLauncher, launchPortableUpdater, preparePortableInstall } from './portable-update.ts'; +import { + cleanupPreparedPortableInstall, isPortableLauncher, launchPortableUpdater, preparePortableInstall, +} from './portable-update.ts'; type UpdateMode = 'installer' | 'portable'; @@ -215,7 +217,12 @@ export function installAppUpdate(version: string, launcherBusy: () => boolean): try { if (offer.mode === 'portable') { const prepared = await preparePortableInstall(path, offer.manifest.version, updatesRoot()); - launchPortableUpdater(prepared); + try { + await launchPortableUpdater(prepared); + } catch (launchError) { + await cleanupPreparedPortableInstall(prepared); + throw launchError; + } app.quit(); return; } diff --git a/apps/launcher/src/main/services/app-update/portable-update.ts b/apps/launcher/src/main/services/app-update/portable-update.ts index d25f7e4..25089bd 100644 --- a/apps/launcher/src/main/services/app-update/portable-update.ts +++ b/apps/launcher/src/main/services/app-update/portable-update.ts @@ -7,7 +7,7 @@ * only obsolete files listed in the previous portable inventory (never touching unrelated user * files in the same folder), and restarts the updated executable. */ -import { execFile, spawn } from 'node:child_process'; +import { execFile } from 'node:child_process'; import { randomUUID } from 'node:crypto'; import * as nodeFs from 'node:fs'; import { existsSync } from 'node:fs'; @@ -283,16 +283,41 @@ export async function preparePortableInstall( } } -export function launchPortableUpdater(prepared: PreparedPortableUpdate): void { - const child = spawn('powershell.exe', [ - '-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-ExecutionPolicy', 'Bypass', - '-File', prepared.scriptPath, prepared.planPath, - ], { - cwd: dirname(prepared.scriptPath), - detached: true, - stdio: 'ignore', - windowsHide: true, - shell: false, - }); - child.unref(); +export async function cleanupPreparedPortableInstall(prepared: PreparedPortableUpdate): Promise { + const rawFs = rawFsPromises(); + await rawFs.rm(prepared.stagingDir, { recursive: true, force: true }).catch(() => undefined); + await rm(prepared.planPath, { force: true }).catch(() => undefined); +} + +/** + * Launch the portable apply script in a hidden PowerShell process that survives Electron exit. + * + * On Windows, spawning powershell.exe directly with Node's `detached: true` sets DETACHED_PROCESS + * (no console), causing Windows PowerShell 5.1 ConsoleHost to exit silently with code 0 before + * running `-File`. Spawning without `detached` lets PowerShell run, but Electron tears down + * attached children on `app.quit()`. Using a short hidden PowerShell helper with `Start-Process` + * gives the updater its own hidden console and cleanly detaches it before the launcher quits. + */ +export async function launchPortableUpdater(prepared: PreparedPortableUpdate): Promise { + const workDir = dirname(prepared.scriptPath); + const launcherPath = join(workDir, `launch-portable-${randomUUID()}.ps1`); + const script = [ + 'param([string]$ScriptPath, [string]$PlanPath, [string]$WorkDir)', + "$ErrorActionPreference = 'Stop'", + '$argsList = @(', + " '-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-ExecutionPolicy', 'Bypass',", + ' \'-File\', "`"$ScriptPath`"", "`"$PlanPath`""', + ')', + "Start-Process -FilePath 'powershell.exe' -WorkingDirectory $WorkDir -WindowStyle Hidden -ArgumentList $argsList", + ].join('\r\n'); + // UTF-8 BOM ensures Windows PowerShell 5.1 decodes non-ASCII paths properly. + await writeFile(launcherPath, `\uFEFF${script}\r\n`, 'utf8'); + try { + await execute('powershell.exe', [ + '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', + '-File', launcherPath, prepared.scriptPath, prepared.planPath, workDir, + ], { cwd: workDir, windowsHide: true, timeout: 30_000 }); + } finally { + await rm(launcherPath, { force: true }).catch(() => undefined); + } }