Skip to content

The agent that grows with you

fix(desktop): order PE stamping before ASAR integrity

joaomarcos168e0f78adb4
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 6cf5c2b58d8..792b7afb25e 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -213,7 +213,7 @@
],
"beforeBuild": "scripts/before-build.mjs",
"beforePack": "scripts/before-pack.mjs",
- "afterPack": "scripts/after-pack.mjs",
+ "afterExtract": "scripts/after-extract.mjs",
"extraResources": [
{
"from": "build/install-stamp.json",
diff --git a/apps/desktop/scripts/after-extract.mjs b/apps/desktop/scripts/after-extract.mjs
new file mode 100644
index 00000000000..e410a8986dd
--- /dev/null
+++ b/apps/desktop/scripts/after-extract.mjs
@@ -0,0 +1,39 @@
+/**
+ * after-extract.mjs — electron-builder afterExtract hook.
+ *
+ * Stamps the Hermes icon + identity onto the unpacked Windows Electron binary
+ * before electron-builder renames it and injects the ASAR integrity resource.
+ * Keeping the rcedit mutation before electron-builder's resedit rewrite makes
+ * both resource edits compose into the final Hermes.exe.
+ *
+ * Windows-only: rcedit edits PE resources, irrelevant on macOS/Linux where the
+ * app identity comes from the bundle Info.plist / desktop entry. Best-effort:
+ * a stamp failure must never fail an otherwise-good build (worst case is the
+ * stock icon, not a broken app), so we log and resolve rather than throw.
+ *
+ * electron-builder passes a context with:
+ * - electronPlatformName: 'win32' | 'darwin' | 'linux'
+ * - appOutDir: the unpacked Electron directory
+ * - packager.config.electronBranding.projectName: source binary basename
+ */
+
+import path from 'node:path'
+
+import { stampExeIdentity } from './set-exe-identity.mjs'
+
+export default async function afterExtract(context) {
+ if (context.electronPlatformName !== 'win32') {
+ return
+ }
+
+ const projectName = context.packager?.config?.electronBranding?.projectName || 'electron'
+ const exe = path.join(context.appOutDir, `${projectName}.exe`)
+ const desktopRoot = path.resolve(import.meta.dirname, '..')
+
+ try {
+ await stampExeIdentity(exe, desktopRoot)
+ } catch (err) {
+ // Never fail the build over a cosmetic stamp.
+ console.warn(`[after-extract] exe identity stamp failed (${err.message}); ${projectName}.exe keeps the stock Electron icon`)
+ }
+}
diff --git a/apps/desktop/scripts/after-extract.test.mjs b/apps/desktop/scripts/after-extract.test.mjs
new file mode 100644
index 00000000000..cae086eb51b
--- /dev/null
+++ b/apps/desktop/scripts/after-extract.test.mjs
@@ -0,0 +1,44 @@
+import assert from 'node:assert/strict'
+import path from 'node:path'
+import { createRequire } from 'node:module'
+import { fileURLToPath } from 'node:url'
+import { test, vi } from 'vitest'
+
+const stampExeIdentity = vi.hoisted(() => vi.fn().mockResolvedValue(undefined))
+vi.mock('./set-exe-identity.mjs', () => ({ stampExeIdentity }))
+
+const { default: afterExtract } = await import('./after-extract.mjs')
+
+const require = createRequire(import.meta.url)
+const desktopRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
+const desktopPackage = require(path.join(desktopRoot, 'package.json'))
+
+test('configures executable stamping before electron-builder adds ASAR integrity', () => {
+ assert.equal(desktopPackage.build.afterExtract, 'scripts/after-extract.mjs')
+ assert.equal(desktopPackage.build.afterPack, undefined)
+})
+
+test('stamps the stock Windows executable before it is renamed and rewritten', async () => {
+ stampExeIdentity.mockClear()
+ const appOutDir = path.join('tmp', 'win-unpacked')
+
+ await afterExtract({
+ appOutDir,
+ electronPlatformName: 'win32',
+ packager: { appInfo: { productFilename: 'Hermes' } }
+ })
+
+ assert.deepEqual(stampExeIdentity.mock.calls, [[path.join(appOutDir, 'electron.exe'), desktopRoot]])
+})
+
+test('does not edit non-Windows Electron binaries', async () => {
+ stampExeIdentity.mockClear()
+
+ await afterExtract({
+ appOutDir: path.join('tmp', 'linux-unpacked'),
+ electronPlatformName: 'linux',
+ packager: { appInfo: { productFilename: 'Hermes' } }
+ })
+
+ assert.equal(stampExeIdentity.mock.calls.length, 0)
+})
diff --git a/apps/desktop/scripts/after-pack.mjs b/apps/desktop/scripts/after-pack.mjs
deleted file mode 100644
index 509cbb42481..00000000000
--- a/apps/desktop/scripts/after-pack.mjs
+++ /dev/null
@@ -1,41 +0,0 @@
-/**
- * after-pack.mjs — electron-builder afterPack hook.
- *
- * Stamps the Hermes icon + identity onto the packed Windows Hermes.exe via
- * rcedit (delegated to set-exe-identity.mjs). This runs for EVERY packed build
- * — first install, `hermes desktop`, the installer's --update rebuild, and a
- * dev's manual `npm run pack` — so the branded exe can never silently revert
- * to the stock "Electron" icon/name (the bug when the stamp lived only in
- * install.ps1, which the update path doesn't use).
- *
- * Windows-only: rcedit edits PE resources, irrelevant on macOS/Linux where the
- * app identity comes from the bundle Info.plist / desktop entry. Best-effort:
- * a stamp failure must never fail an otherwise-good build (worst case is the
- * stock icon, not a broken app), so we log and resolve rather than throw.
- *
- * electron-builder passes a context with:
- * - electronPlatformName: 'win32' | 'darwin' | 'linux'
- * - appOutDir: the unpacked app directory for this target
- * - packager.appInfo.productFilename: the exe basename (e.g. 'Hermes')
- */
-
-import path from 'node:path'
-
-import { stampExeIdentity } from './set-exe-identity.mjs'
-
-export default async function afterPack(context) {
- if (context.electronPlatformName !== 'win32') {
- return
- }
-
- const productName = context.packager?.appInfo?.productFilename || 'Hermes'
- const exe = path.join(context.appOutDir, `${productName}.exe`)
- const desktopRoot = path.resolve(import.meta.dirname, '..')
-
- try {
- await stampExeIdentity(exe, desktopRoot)
- } catch (err) {
- // Never fail the build over a cosmetic stamp.
- console.warn(`[after-pack] exe identity stamp failed (${err.message}); Hermes.exe keeps the stock Electron icon`)
- }
-}
diff --git a/apps/desktop/scripts/set-exe-identity.mjs b/apps/desktop/scripts/set-exe-identity.mjs
index 3c8d342b782..03ae85e76c2 100644
--- a/apps/desktop/scripts/set-exe-identity.mjs
+++ b/apps/desktop/scripts/set-exe-identity.mjs
@@ -20,19 +20,19 @@
//
// HOW IT RUNS
// -----------
-// Primarily as an electron-builder `afterPack` hook (scripts/after-pack.mjs),
+// Primarily as an electron-builder `afterExtract` hook (scripts/after-extract.mjs),
// so EVERY packed build — first install, `hermes desktop`, the installer's
// --update rebuild, or a dev's manual `npm run pack` — gets a branded exe from
// one place. Previously this stamp lived only in install.ps1, so the update
// path (which rebuilds via `hermes desktop --build-only`, never install.ps1)
-// shipped a stock "Electron" exe. Keeping it in afterPack closes that gap.
+// shipped a stock "Electron" exe. Keeping it in afterExtract closes that gap.
//
// Also runnable standalone for ad-hoc re-stamping:
// node scripts/set-exe-identity.mjs <path-to-Hermes.exe>
//
// Exits 0 on success, non-zero on failure when run as a CLI. As a hook,
// stampExeIdentity() resolves on success and rejects on failure; the caller
-// (after-pack.mjs) swallows the rejection so a stamp failure never fails an
+// (after-extract.mjs) swallows the rejection so a stamp failure never fails an
// otherwise-good build (worst case: stock icon, not a broken app).
import { resolve, join } from 'node:path'