Skip to content

The agent that grows with you

fix(desktop): restore packaged macOS locale markers

lgy1027093db2b19024
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 4d11c69652f..46fd736c05f 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -217,6 +217,7 @@
"beforeBuild": "scripts/before-build.mjs",
"beforePack": "scripts/before-pack.mjs",
"afterExtract": "scripts/after-extract.mjs",
+ "afterPack": "scripts/after-pack.mjs",
"extraResources": [
{
"from": "build/install-stamp.json",
diff --git a/apps/desktop/scripts/after-pack.mjs b/apps/desktop/scripts/after-pack.mjs
new file mode 100644
index 00000000000..56a471c74bd
--- /dev/null
+++ b/apps/desktop/scripts/after-pack.mjs
@@ -0,0 +1,30 @@
+/** Restore the empty app-level localizations dropped during Electron extraction.
+ * Run after language filtering and before signing; derive the markers from the
+ * packaged framework, not the host's Electron (which may be another version).
+ * Windows identity stamping stays in afterExtract, before ASAR integrity.
+ */
+import { mkdir, readdir } from 'node:fs/promises'
+import path from 'node:path'
+
+export default async function afterPack({ electronPlatformName, appOutDir, packager }) {
+ if (electronPlatformName !== 'darwin') {
+ return
+ }
+
+ try {
+ const resources = packager.getResourcesDir(appOutDir)
+ const framework = packager.getMacOsElectronFrameworkResourcesDir(appOutDir)
+ const entries = await readdir(framework, { withFileTypes: true })
+ // Chromium also ships grammatical-gender packs; these are not macOS locales.
+ const locales = entries.filter(
+ entry =>
+ entry.isDirectory() && entry.name.endsWith('.lproj') && !/_(FEMININE|MASCULINE|NEUTER)\.lproj$/.test(entry.name)
+ )
+ await Promise.all(locales.map(entry => mkdir(path.join(resources, entry.name), { recursive: true })))
+ } catch (error) {
+ // Keep an otherwise usable package, but make failed locale restoration visible.
+ console.warn(
+ `[after-pack] macOS locale markers were not restored: ${error instanceof Error ? error.message : String(error)}`
+ )
+ }
+}
diff --git a/apps/desktop/scripts/after-pack.test.mjs b/apps/desktop/scripts/after-pack.test.mjs
new file mode 100644
index 00000000000..3ef0500ee77
--- /dev/null
+++ b/apps/desktop/scripts/after-pack.test.mjs
@@ -0,0 +1,66 @@
+import { mkdtemp, mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises'
+import os from 'node:os'
+import path from 'node:path'
+import { Platform } from 'app-builder-lib'
+import { PlatformPackager } from 'app-builder-lib/out/platformPackager.js'
+import { expect, it, vi } from 'vitest'
+
+import pkg from '../package.json' with { type: 'json' }
+
+async function configuredHook(context) {
+ if (pkg.build.afterPack) {
+ const hook = await import(new URL(`../${pkg.build.afterPack}`, import.meta.url).href)
+ await hook.default(context)
+ }
+}
+
+function context(appOutDir, productFilename = 'Hermes Preview') {
+ // Use electron-builder's real bundle path resolution, including branding.
+ const packager = Object.assign(Object.create(PlatformPackager.prototype), {
+ platform: Platform.MAC,
+ appInfo: { productFilename },
+ info: { framework: { distMacOsAppName: 'Electron.app' } }
+ })
+ return { appOutDir, electronPlatformName: 'darwin', packager }
+}
+
+it('restores app localizations from the filtered framework without copying locale data', async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'hermes-locale-pack-'))
+ try {
+ const ctx = context(root)
+ const framework = ctx.packager.getMacOsElectronFrameworkResourcesDir(root)
+ const resources = ctx.packager.getResourcesDir(root)
+ await mkdir(resources, { recursive: true })
+ for (const name of ['nb.lproj', 'en_GB.lproj', 'nb_FEMININE.lproj']) {
+ await mkdir(path.join(framework, name), { recursive: true })
+ await writeFile(path.join(framework, name, 'locale.pak'), 'untouched locale data')
+ }
+ await writeFile(path.join(framework, 'not-a-directory.lproj'), 'not a locale')
+ await mkdir(path.join(framework, 'other'), { recursive: true })
+ await configuredHook(ctx)
+ await configuredHook(ctx)
+ expect((await readdir(resources)).sort()).toEqual(['en_GB.lproj', 'nb.lproj'])
+ expect(await readdir(path.join(resources, 'nb.lproj'))).toEqual([])
+ expect(await readFile(path.join(framework, 'nb.lproj', 'locale.pak'), 'utf8')).toBe('untouched locale data')
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+it('leaves other platforms alone and reports a missing framework without failing packaging', async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'hermes-locale-pack-'))
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ try {
+ for (const electronPlatformName of ['linux', 'win32']) {
+ await configuredHook({ appOutDir: root, electronPlatformName })
+ }
+ expect(await readdir(root)).toEqual([])
+ expect(warn).not.toHaveBeenCalled()
+ await configuredHook(context(root))
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('macOS locale markers were not restored'))
+ expect(await readdir(root)).toEqual([])
+ } finally {
+ warn.mockRestore()
+ await rm(root, { recursive: true, force: true })
+ }
+})