diff --git a/apps/desktop/src/electron/ElectronUpdater.launchPaths.test.ts b/apps/desktop/src/electron/ElectronUpdater.launchPaths.test.ts new file mode 100644 index 000000000000..2f411c9b3fdb --- /dev/null +++ b/apps/desktop/src/electron/ElectronUpdater.launchPaths.test.ts @@ -0,0 +1,120 @@ +// @effect-diagnostics nodeBuiltinImport:off - Native synchronous fixtures exercise updater event and relaunch ordering. +import * as NodeChildProcess from "node:child_process"; +import * as NodeFS from "node:fs"; +import * as NodeModule from "node:module"; +import * as NodeOS from "node:os"; +import * as NodePath from "node:path"; + +import { assert, describe, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import { vi } from "vite-plus/test"; +import type { AppImageUpdater as AppImageUpdaterType } from "electron-updater"; + +const { getUpdater } = vi.hoisted(() => ({ getUpdater: vi.fn<() => AppImageUpdaterType>() })); +vi.mock("electron-updater", async (importOriginal) => { + const { AppImageUpdater } = await importOriginal(); + return { + AppImageUpdater, + get autoUpdater() { + return getUpdater(); + }, + }; +}); + +import * as ElectronUpdater from "./ElectronUpdater.ts"; + +const require = NodeModule.createRequire(import.meta.url); +const { AppImageUpdater } = require("electron-updater") as typeof import("electron-updater"); + +// Exercise the real synchronous installer without Electron's deferred app.quit lifecycle. +class FixtureUpdater extends AppImageUpdater { + readonly download: string; + + constructor(download: string) { + super(null, { version: "0.1.7" }); + this.download = download; + this.logger = null; + } + + protected override get installerPath() { + return this.download; + } + + override quitAndInstall(isSilent = false, isForceRunAfter = false) { + this.doInstall({ isSilent, isForceRunAfter, isAdminRightsRequired: false }); + } +} + +function fixture(oldName: string, newName: string) { + const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-appimage-launcher-")); + const oldPath = NodePath.join(directory, oldName); + const newPath = NodePath.join(directory, newName); + const cache = NodePath.join(directory, "cache"); + NodeFS.mkdirSync(cache); + const download = NodePath.join(cache, newName); + NodeFS.writeFileSync(oldPath, "#!/bin/sh\necho old\n", { mode: 0o755 }); + NodeFS.writeFileSync(download, "#!/bin/sh\necho updated\n", { mode: 0o755 }); + const launcher = NodePath.join(directory, "launcher.AppImage"); + NodeFS.symlinkSync(oldPath, launcher); + const updater = new FixtureUpdater(download); + getUpdater.mockReturnValue(updater); + const prototype = AppImageUpdater.prototype as unknown as { + spawnLog: (destination: string, args?: Array) => Promise; + }; + const spawnLog = vi.spyOn(prototype, "spawnLog").mockImplementation((destination, args = []) => { + assert.equal(NodeChildProcess.execFileSync(oldPath, { encoding: "utf8" }), "updated\n"); + assert.equal( + NodeChildProcess.execFileSync(destination, args, { encoding: "utf8" }), + "updated\n", + ); + return Promise.resolve(true); + }); + const originalAppImage = process.env.APPIMAGE; + process.env.APPIMAGE = oldPath; + return { + oldPath, + newPath, + launcher, + updater, + [Symbol.dispose]() { + spawnLog.mockRestore(); + if (originalAppImage === undefined) delete process.env.APPIMAGE; + else process.env.APPIMAGE = originalAppImage; + NodeFS.rmSync(directory, { recursive: true, force: true }); + }, + }; +} + +// oxlint-disable-next-line t3code/no-global-process-runtime -- Native shell fixtures require a Linux host. +describe.skipIf(process.platform !== "linux")("AppImage launch paths", () => { + it.live("keeps a launcher through the versioned-to-stable transition", () => + Effect.gen(function* () { + using f = fixture("T3-Code-0.1.7-arm64.AppImage", "T3-Code-arm64.AppImage"); + yield* ElectronUpdater.make.quitAndInstall({ isSilent: true, isForceRunAfter: true }); + assert.equal(NodeChildProcess.execFileSync(f.launcher, { encoding: "utf8" }), "updated\n"); + assert.equal(NodeFS.readlinkSync(f.oldPath), NodePath.basename(f.newPath)); + assert.equal(f.updater.listenerCount("appimage-filename-updated"), 0); + }), + ); + + it.live("does not build alias chains between versioned releases", () => + Effect.gen(function* () { + using f = fixture("T3-Code-0.1.7.AppImage", "T3-Code-0.1.8.AppImage"); + yield* ElectronUpdater.make.quitAndInstall({ isSilent: true, isForceRunAfter: false }); + assert.throws(() => NodeFS.lstatSync(f.oldPath)); + assert.equal(NodeChildProcess.execFileSync(f.newPath, { encoding: "utf8" }), "updated\n"); + }), + ); + + it.live("does not overwrite a path recreated during installation or abort relaunch", () => + Effect.gen(function* () { + using f = fixture("T3-Code-0.1.7.AppImage", "T3-Code-x86_64.AppImage"); + f.updater.on("appimage-filename-updated", () => + NodeFS.writeFileSync(f.oldPath, "other file"), + ); + yield* ElectronUpdater.make.quitAndInstall({ isSilent: true, isForceRunAfter: false }); + assert.equal(NodeFS.readFileSync(f.oldPath, "utf8"), "other file"); + assert.equal(NodeChildProcess.execFileSync(f.newPath, { encoding: "utf8" }), "updated\n"); + }), + ); +}); diff --git a/apps/desktop/src/electron/ElectronUpdater.ts b/apps/desktop/src/electron/ElectronUpdater.ts index 0cc22bb4b163..73e876026ca8 100644 --- a/apps/desktop/src/electron/ElectronUpdater.ts +++ b/apps/desktop/src/electron/ElectronUpdater.ts @@ -1,3 +1,8 @@ +// @effect-diagnostics nodeBuiltinImport:off - The filename event must finish synchronously before updater relaunch. +import * as NodeFS from "node:fs"; +import * as NodePath from "node:path"; + +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -167,23 +172,42 @@ export const make = ElectronUpdater.of({ catch: (cause) => new ElectronUpdaterDownloadUpdateError({ channel, cause }), }).pipe(Effect.asVoid); }), - quitAndInstall: Effect.fn("ElectronUpdater.quitAndInstall")(function ({ + quitAndInstall: Effect.fn("ElectronUpdater.quitAndInstall")(function* ({ isSilent, isForceRunAfter, }) { - return Effect.suspend(() => { - const channel = autoUpdater.channel; - if (autoUpdater instanceof AppImageUpdater) relaunchWithoutInheritedDescriptors(autoUpdater); - return Effect.try({ - try: () => autoUpdater.quitAndInstall(isSilent, isForceRunAfter), - catch: (cause) => - new ElectronUpdaterQuitAndInstallError({ - channel, - isSilent, - isForceRunAfter, - cause, - }), - }); + const appImagePath = (yield* HostProcess.Environment).APPIMAGE; + const channel = autoUpdater.channel; + if (autoUpdater instanceof AppImageUpdater) relaunchWithoutInheritedDescriptors(autoUpdater); + return yield* Effect.try({ + try: () => { + if (appImagePath === undefined) + return autoUpdater.quitAndInstall(isSilent, isForceRunAfter); + // electron-updater 6.8.9 emits this synchronously after moving the file, before relaunch. + const preserveAppImagePath = (destination: string) => { + const filename = NodePath.basename(destination); + // A versioned name is replaced by the next update, so aliasing it would build chains. + if (/\d+\.\d+\.\d+/.test(filename)) return; + try { + NodeFS.symlinkSync(filename, appImagePath); + } catch { + autoUpdater.logger?.warn("Could not preserve the previous AppImage launch path."); + } + }; + autoUpdater.on("appimage-filename-updated", preserveAppImagePath); + try { + autoUpdater.quitAndInstall(isSilent, isForceRunAfter); + } finally { + autoUpdater.removeListener("appimage-filename-updated", preserveAppImagePath); + } + }, + catch: (cause) => + new ElectronUpdaterQuitAndInstallError({ + channel, + isSilent, + isForceRunAfter, + cause, + }), }); }), on: Effect.fn("ElectronUpdater.on")(function (eventName, listener) {