From a1f9e16ccb17da644dfabc1834cdb6f51c182e8d Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Wed, 20 May 2026 20:16:56 -0700 Subject: [PATCH] Rebuild stale macOS dev Electron app copies (#2479) - Detect missing Chromium framework resources before reusing the copied app - Prevent interrupted copies from leaving `pn dev` with a blank Electron window --- config/scripts/run-electron-vite-dev.mjs | 38 +++++++-- .../startup/run-electron-vite-dev.test.ts | 85 ++++++++++++++++++- 2 files changed, 115 insertions(+), 8 deletions(-) diff --git a/config/scripts/run-electron-vite-dev.mjs b/config/scripts/run-electron-vite-dev.mjs index 109b55da0..d539b6f0d 100644 --- a/config/scripts/run-electron-vite-dev.mjs +++ b/config/scripts/run-electron-vite-dev.mjs @@ -155,14 +155,40 @@ function prepareMacDevElectronApp() { null, 2 ) + const executablePath = path.join(appPath, 'Contents', 'MacOS', 'Electron') + const requiredResourcePaths = [ + path.join( + appPath, + 'Contents', + 'Frameworks', + 'Electron Framework.framework', + 'Resources', + 'icudtl.dat' + ) + ] - if (existsSync(markerPath) && existsSync(appPath)) { + function copiedAppIsUsable() { + if (!existsSync(markerPath) || !existsSync(appPath)) { + return false + } try { - if (readFileSync(markerPath, 'utf8') === expectedMarker) { - process.env.ELECTRON_EXEC_PATH = path.join(appPath, 'Contents', 'MacOS', 'Electron') - return + if (readFileSync(markerPath, 'utf8') !== expectedMarker) { + return false } - } catch {} + } catch { + return false + } + // Why: a previous interrupted copy can leave the marker and executable + // present but miss Chromium framework resources, causing a blank crash. + return ( + existsSync(executablePath) && + requiredResourcePaths.every((resourcePath) => existsSync(resourcePath)) + ) + } + + if (copiedAppIsUsable()) { + process.env.ELECTRON_EXEC_PATH = executablePath + return } rmSync(distDir, { recursive: true, force: true }) @@ -178,7 +204,7 @@ function prepareMacDevElectronApp() { // and Electron's framework bundle is ambiguous to codesign when deep-signing // an already-built distribution. Avoid blocking `pn dev` on local signing. writeFileSync(markerPath, expectedMarker, 'utf8') - process.env.ELECTRON_EXEC_PATH = path.join(appPath, 'Contents', 'MacOS', 'Electron') + process.env.ELECTRON_EXEC_PATH = executablePath } function getDevUserDataPath() { diff --git a/src/main/startup/run-electron-vite-dev.test.ts b/src/main/startup/run-electron-vite-dev.test.ts index 14b2fc2b4..2bb9f9a43 100644 --- a/src/main/startup/run-electron-vite-dev.test.ts +++ b/src/main/startup/run-electron-vite-dev.test.ts @@ -1,6 +1,6 @@ -import { mkdtempSync, readFileSync } from 'node:fs' +import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' -import { join, resolve } from 'node:path' +import { dirname, join, resolve } from 'node:path' import { spawn } from 'node:child_process' import { afterEach, describe, expect, it } from 'vitest' @@ -228,4 +228,85 @@ describe('run-electron-vite-dev', () => { wrapper.kill('SIGINT') }) + + it.skipIf(process.platform !== 'darwin')( + 'rebuilds the copied Electron app when Chromium resources are missing', + async () => { + const tempDir = mkdtempSync(join(tmpdir(), 'orca-dev-wrapper-')) + const wrapperPath = resolve('config/scripts/run-electron-vite-dev.mjs') + const fakeCliPath = resolve('src/main/startup/__fixtures__/fake-electron-vite-dev-cli.mjs') + const baseEnv = devWrapperTestEnv({ + ORCA_ELECTRON_VITE_CLI: fakeCliPath, + ORCA_SKIP_DEV_CLI_PREPARE: '1', + ORCA_SKIP_DEV_WEB_PREPARE: '1', + ORCA_DEV_BRANCH: 'feature/rebuild-electron-app', + ORCA_DEV_WORKTREE_NAME: 'electron-app-rebuild' + }) + + async function runWrapper(runId: string): Promise<{ electronExecPath: string }> { + const pidFile = join(tempDir, `${runId}.pid`) + const envFile = join(tempDir, `${runId}.json`) + const wrapper = spawn(process.execPath, [wrapperPath, '--remote-debugging-port=9448'], { + cwd: resolve('.'), + env: { + ...baseEnv, + ORCA_DEV_WRAPPER_TEST_PID_FILE: pidFile, + ORCA_DEV_WRAPPER_TEST_ENV_FILE: envFile + }, + stdio: 'ignore' + }) + + expect(wrapper.pid).toBeTypeOf('number') + processesToCleanUp.add(wrapper.pid!) + + await waitFor(() => { + try { + return readFileSync(envFile, 'utf8').trim().length > 0 + } catch { + return false + } + }, 20000) + + const grandchildPid = Number.parseInt(readFileSync(pidFile, 'utf8').trim(), 10) + if (Number.isFinite(grandchildPid)) { + processesToCleanUp.add(grandchildPid) + } + + const envSnapshot = JSON.parse(readFileSync(envFile, 'utf8')) as { + electronExecPath: string | null + } + expect(envSnapshot.electronExecPath).toBeTypeOf('string') + wrapper.kill('SIGINT') + return { electronExecPath: envSnapshot.electronExecPath! } + } + + let distDir: string | null = null + try { + const firstRun = await runWrapper('first') + const appPath = dirname(dirname(dirname(firstRun.electronExecPath))) + distDir = dirname(appPath) + const icuDataPath = join( + appPath, + 'Contents', + 'Frameworks', + 'Electron Framework.framework', + 'Resources', + 'icudtl.dat' + ) + expect(existsSync(icuDataPath)).toBe(true) + + rmSync(icuDataPath, { force: true }) + expect(existsSync(icuDataPath)).toBe(false) + + const secondRun = await runWrapper('second') + expect(secondRun.electronExecPath).toBe(firstRun.electronExecPath) + expect(existsSync(icuDataPath)).toBe(true) + } finally { + if (distDir) { + rmSync(distDir, { recursive: true, force: true }) + } + } + }, + 30000 + ) })