diff --git a/packages/desktop/resources/windows/installer.nsh b/packages/desktop/resources/windows/installer.nsh index 6c994e5d5d3..1615efa328d 100644 --- a/packages/desktop/resources/windows/installer.nsh +++ b/packages/desktop/resources/windows/installer.nsh @@ -1,8 +1,56 @@ +; electron-builder names a one-click per-user install directory after the package name, which every +; channel shares, so OpenCode, OpenCode Beta and OpenCode Dev all installed into one "@opencodedesktop" +; directory and each channel's installer and uninstaller replaced or deleted the others' files. +; Each channel now installs into a directory named after its app ID, like its data directory. +; Only the installer moves, and NSIS rejects a variable that the uninstaller build never sets. +!ifndef BUILD_UNINSTALLER + Var /GLOBAL sharedInstallDir +!endif + +!macro customInit + StrCpy $sharedInstallDir "" + ${StdUtils.GetParentPath} $R0 $INSTDIR + ${If} $INSTDIR == "$R0\${APP_FILENAME}" + ${If} ${FileExists} "$INSTDIR\${APP_EXECUTABLE_FILENAME}" + StrCpy $sharedInstallDir $INSTDIR + ; The old uninstaller would delete every channel's files from the shared directory, so the + ; install skips it and removes only this channel's files afterwards. The install writes the + ; uninstall entry again for the new directory. + DeleteRegValue SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" UninstallString + !ifdef UNINSTALL_REGISTRY_KEY_2 + DeleteRegValue SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY_2}" UninstallString + !endif + ${EndIf} + StrCpy $INSTDIR "$R0\${APP_ID}" + ${EndIf} +!macroend + !macro customInstall ; Chromium's sandbox needs read/execute access to the installed runtime files. ; https://github.com/electron/electron/issues/49143#issuecomment-3618354787 nsExec::ExecToLog '"$SYSDIR\icacls.exe" "$INSTDIR" /grant "*S-1-15-2-2:(OI)(CI)(RX)"' Pop $0 + !insertmacro leaveSharedInstall +!macroend + +; Removes this channel's files from the shared directory, and the directory with the files every +; channel shared once no other channel's uninstaller is left in it. A taskbar pin still starts the +; old executable, so it is pointed at the new one. +!macro leaveSharedInstall + ${If} $sharedInstallDir != "" + Delete "$sharedInstallDir\${APP_EXECUTABLE_FILENAME}" + Delete "$sharedInstallDir\${UNINSTALL_FILENAME}" + !insertmacro removeStagedUpdates $sharedInstallDir + FindFirst $R0 $R1 "$sharedInstallDir\Uninstall *.exe" + FindClose $R0 + ${If} $R1 == "" + RMDir /r $sharedInstallDir + ${EndIf} + ${If} ${FileExists} "$QUICKLAUNCH\User Pinned\TaskBar\${SHORTCUT_NAME}.lnk" + CreateShortCut "$QUICKLAUNCH\User Pinned\TaskBar\${SHORTCUT_NAME}.lnk" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 + WinShell::SetLnkAUMI "$QUICKLAUNCH\User Pinned\TaskBar\${SHORTCUT_NAME}.lnk" "${APP_ID}" + ${EndIf} + ${EndIf} !macroend !macro customUnInstall @@ -41,9 +89,20 @@ System::Call "kernel32::QueryFullProcessImageNameW(p R4, i 0, w .R5, *i ${NSIS_MAX_STRLEN}) i .R6" System::Call "kernel32::CloseHandle(p R4)" ${If} $R6 != 0 + ; A channel leaving the shared directory stops its own app there, but not the others'. + StrCpy $2 0 + !ifndef BUILD_UNINSTALLER + ${If} $sharedInstallDir != "" + ${AndIf} $R5 == "$sharedInstallDir\${APP_EXECUTABLE_FILENAME}" + StrCpy $2 1 + ${EndIf} + !endif StrLen $R6 "$INSTDIR\" StrCpy $R5 $R5 $R6 ${If} $R5 == "$INSTDIR\" + StrCpy $2 1 + ${EndIf} + ${If} $2 == 1 IntOp $R7 $R7 + 1 ${If} $1 >= 0 System::Call "kernel32::OpenProcess(i 1, i 0, i R3) p .R4" @@ -129,6 +188,12 @@ ${AndIf} ${FileExists} "$R1\${APP_EXECUTABLE_FILENAME}" ${AndIf} $R2 > $R3 !insertmacro removeStagedUpdates $R1 + ; Stage beside the directory the install will use, which customInit moves out of the shared one. + ${StdUtils.GetParentPath} $R5 $R1 + ${If} $R1 == "$R5\${APP_FILENAME}" + StrCpy $R1 "$R5\${APP_ID}" + !insertmacro removeStagedUpdates $R1 + ${EndIf} InitPluginsDir ; Stored like the template's copy, so the installer keeps one copy of the payload. !ifdef COMPRESS diff --git a/packages/desktop/scripts/windows-installer.test.ts b/packages/desktop/scripts/windows-installer.test.ts index 137569b3167..98fbe85764e 100644 --- a/packages/desktop/scripts/windows-installer.test.ts +++ b/packages/desktop/scripts/windows-installer.test.ts @@ -8,6 +8,9 @@ const windows = test.skipIf(process.platform !== "win32") // Matches the 16 hex characters the staged directory name takes from APP_64_HASH. const payloadHash = "0123456789ABCDEF0123456789ABCDEF" +// The installer clears the uninstall entry of an install it moves; the tests point it at their own key. +const uninstallKey = "Software\\OpenCodeInstallerTest" + async function harness() { const { getMakeNsisPath, getNsisPluginsPath } = await import("app-builder-lib/out/toolsets/windows") const { nsisTemplatesDir } = await import("app-builder-lib/out/targets/nsis/nsisUtil") @@ -52,6 +55,11 @@ LangString appRunning \${LANG_ENGLISH} "running" LangString appClosing \${LANG_ENGLISH} "closing" LangString appCannotBeClosed \${LANG_ENGLISH} "cannot close" !define APP_EXECUTABLE_FILENAME "OpenCode.exe" +!define APP_ID "ai.opencode.desktop" +!define APP_FILENAME "@opencodedesktop" +!define UNINSTALL_FILENAME "Uninstall OpenCode.exe" +!define UNINSTALL_REGISTRY_KEY "${uninstallKey}" +!define SHORTCUT_NAME "OpenCode Installer Test" !define APP_64 "${path.join(dir, "app.7z")}" !define APP_64_HASH "${payloadHash}" !define APP_64_UNPACKED_SIZE 1 @@ -201,6 +209,92 @@ windows( 120_000, ) +// Every channel used to install into one directory named after the package. Each channel now installs into +// a directory named after its app ID and leaves the shared one without touching the other channels' files. +const directoryCases = [ + { + name: "a first install uses the app ID", + from: "@opencodedesktop", + shared: [], + expected: "ai.opencode.desktop", + left: undefined, + uninstallKept: true, + }, + { + name: "leaving the shared directory keeps the other channels", + from: "@opencodedesktop", + shared: [ + "OpenCode.exe", + "Uninstall OpenCode.exe", + "OpenCode Beta.exe", + "Uninstall OpenCode Beta.exe", + "resources/app.asar", + ], + expected: "ai.opencode.desktop", + left: ["OpenCode Beta.exe", "Uninstall OpenCode Beta.exe", "resources/app.asar"], + uninstallKept: false, + }, + { + name: "the last channel removes the shared directory", + from: "@opencodedesktop", + shared: ["OpenCode.exe", "Uninstall OpenCode.exe", "resources/app.asar"], + expected: "ai.opencode.desktop", + left: undefined, + uninstallKept: false, + }, + { + name: "an install elsewhere stays where it is", + from: "custom", + shared: ["OpenCode.exe", "Uninstall OpenCode.exe"], + expected: "custom", + left: ["OpenCode.exe", "Uninstall OpenCode.exe"], + uninstallKept: true, + }, +] + +windows( + "installs each channel into a directory named after its app ID", + async () => { + const installer = await harness() + const registry = (args: string[]) => installer.run(["reg.exe", ...args, "/f"]) + + try { + const exe = await installer.compile( + "directory", + ` !insertmacro customInit + FileOpen $0 "$EXEDIR\\instdir.txt" w + FileWrite $0 $INSTDIR + FileClose $0 + !insertmacro leaveSharedInstall`, + ) + + for (const item of directoryCases) { + const root = path.join(installer.dir, item.name.replaceAll(" ", "-")) + const from = path.join(root, item.from) + await mkdir(root, { recursive: true }) + await Promise.all(item.shared.map((file) => Bun.write(path.join(from, file), "old"))) + await registry(["add", `HKCU\\${uninstallKey}`, "/v", "UninstallString", "/d", "uninstall"]) + + expect((await installer.run([exe, `/D=${from}`])).code, item.name).toBe(0) + expect(await Bun.file(path.join(installer.dir, "instdir.txt")).text(), item.name).toBe( + path.join(root, item.expected), + ) + const left = (await readdir(from, { recursive: true, withFileTypes: true }).catch(() => undefined)) + ?.filter((entry) => entry.isFile()) + .map((entry) => path.relative(from, path.join(entry.parentPath, entry.name)).replaceAll("\\", "/")) + .toSorted() + expect(left, item.name).toEqual(item.left) + const query = await installer.run(["reg.exe", "query", `HKCU\\${uninstallKey}`, "/v", "UninstallString"]) + expect(query.code === 0, item.name).toBe(item.uninstallKept) + } + } finally { + await installer.run(["reg.exe", "delete", `HKCU\\${uninstallKey}`, "/f"]) + await installer.cleanup() + } + }, + 120_000, +) + // Stand-ins for the app are copies of ping.exe: it exits with 0 when it finishes and 1 when terminated. const runningCases = [ { name: "a manual install stops the app at once", args: [], pings: 30, exitCode: 1 },