From 58c801b2cbfa353f4ca41608e7b14067b700530b Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Thu, 2 Jul 2026 12:25:42 -0700 Subject: [PATCH] Fix electron dev launch and add test --- apps/desktop/scripts/electron-launcher.mjs | 19 +++++++++++----- .../scripts/electron-launcher.test.mjs | 22 ++++++++++++++++++- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/apps/desktop/scripts/electron-launcher.mjs b/apps/desktop/scripts/electron-launcher.mjs index cfe110f0e32..081ef903a06 100644 --- a/apps/desktop/scripts/electron-launcher.mjs +++ b/apps/desktop/scripts/electron-launcher.mjs @@ -358,10 +358,7 @@ function resolveLinuxSandboxArgs(electronBinaryPath) { } export function resolveElectronPath() { - ensureElectronRuntime(); - - const require = NodeModule.createRequire(import.meta.url); - const electronBinaryPath = require("electron"); + const electronBinaryPath = resolveElectronBinaryPath(); if (hostPlatform !== "darwin") { return electronBinaryPath; @@ -378,13 +375,23 @@ export function resolveElectronLaunchCommand(args = []) { }; } +export function resolveElectronBinaryPath({ + ensureRuntime = ensureElectronRuntime, + createRequire = NodeModule.createRequire, + moduleUrl = import.meta.url, +} = {}) { + ensureRuntime(); + + const require = createRequire(moduleUrl); + return require("electron"); +} + export function resolveDevProtocolClient() { if (hostPlatform !== "darwin" || !isDevelopment) { return null; } - const require = NodeModule.createRequire(import.meta.url); - const electronBinaryPath = require("electron"); + const electronBinaryPath = resolveElectronBinaryPath(); const launcherBinaryPath = buildMacLauncher(electronBinaryPath); return { appBundlePath: NodePath.resolve(launcherBinaryPath, "..", "..", ".."), diff --git a/apps/desktop/scripts/electron-launcher.test.mjs b/apps/desktop/scripts/electron-launcher.test.mjs index 5a1df0ec667..36e9429e444 100644 --- a/apps/desktop/scripts/electron-launcher.test.mjs +++ b/apps/desktop/scripts/electron-launcher.test.mjs @@ -1,6 +1,6 @@ import { assert, describe, it } from "vite-plus/test"; -import { makeDevelopmentLauncherScript } from "./electron-launcher.mjs"; +import { makeDevelopmentLauncherScript, resolveElectronBinaryPath } from "./electron-launcher.mjs"; describe("electron development launcher", () => { it("uses captured values only as fallbacks for a live runner environment", () => { @@ -25,4 +25,24 @@ describe("electron development launcher", () => { "exec '/repo/node_modules/electron/Electron' --t3code-dev-root='/repo/apps/desktop' '/repo/apps/desktop/dist-electron/main.cjs' \"$@\"", ); }); + + it("repairs Electron before loading the package entrypoint", () => { + const calls = []; + const electronPath = resolveElectronBinaryPath({ + ensureRuntime: () => { + calls.push("ensure"); + }, + createRequire: () => (specifier) => { + calls.push(`require:${specifier}`); + return "/repo/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron"; + }, + moduleUrl: import.meta.url, + }); + + assert.equal( + electronPath, + "/repo/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron", + ); + assert.deepEqual(calls, ["ensure", "require:electron"]); + }); });