TypeScript
fix(desktop): new worktrees install Electron from the local download cache (#22712)
Theo Brownea5d66e22396f
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex d29d970c75f..d2c569b8217 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -83,6 +83,14 @@ jobs:- name: Setup Vite+uses: ./.github/actions/setup-ci-deps+ # electron's install.js (run by ensure:electron) reads this cache before it+ # downloads from GitHub. The key changes only with the Electron version.+ - name: Cache Electron download+ uses: actions/cache@v6+ with:+ path: ~/.cache/electron+ key: electron-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('node_modules/.pnpm/electron@*/node_modules/electron/package.json') }}+- name: Ensure Electron runtime is installedrun: vp run --filter @t3tools/desktop ensure:electron@@ -132,6 +140,14 @@ jobs:- name: Setup Vite+uses: ./.github/actions/setup-ci-deps+ # electron's install.js (run by ensure:electron) reads this cache before it+ # downloads from GitHub. The key changes only with the Electron version.+ - name: Cache Electron download+ uses: actions/cache@v6+ with:+ path: ~/.cache/electron+ key: electron-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('node_modules/.pnpm/electron@*/node_modules/electron/package.json') }}+- name: Ensure Electron runtime is installedid: electronrun: vp run --filter @t3tools/desktop ensure:electron@@ -222,6 +238,14 @@ jobs:- name: Setup Vite+uses: ./.github/actions/setup-ci-deps+ # electron's install.js (run by ensure:electron) reads this cache before it+ # downloads from GitHub. The key changes only with the Electron version.+ - name: Cache Electron download+ uses: actions/cache@v6+ with:+ path: ~/.cache/electron+ key: electron-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('node_modules/.pnpm/electron@*/node_modules/electron/package.json') }}+- name: Ensure Electron runtime is installedrun: vp run --filter @t3tools/desktop ensure:electrondiff --git a/apps/desktop/scripts/ensure-electron-runtime.mjs b/apps/desktop/scripts/ensure-electron-runtime.mjsindex ae6cd05961b..933a763dfa3 100644--- a/apps/desktop/scripts/ensure-electron-runtime.mjs+++ b/apps/desktop/scripts/ensure-electron-runtime.mjs@@ -116,7 +116,26 @@ function runChecked(command, args) {);}-function installElectronRuntime(electronDir, version) {+// electron's own installer goes through @electron/get, which reuses its+// download cache (~/.cache/electron, ~/Library/Caches/electron) and verifies+// the bundled checksums. pnpm 11 never runs it because electron has no+// install script, so a new worktree would otherwise download 130 MB again.+function installFromElectronCache(electronDir) {+ const result = NodeChildProcess.spawnSync(+ process.execPath,+ [NodePath.join(electronDir, "install.js")],+ { stdio: ["ignore", "inherit", "inherit"] },+ );+ if (result.status === 0) {+ return true;+ }+ process.stderr.write(+ `electron install.js failed with exit code ${result.status ?? "unknown"}, downloading with curl instead\n`,+ );+ return false;+}++function downloadElectronRuntime(electronDir, version) {const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-electron-"));const zipPath = NodePath.join(tempDir, `electron-v${version}-${hostPlatform}-${hostArch}.zip`);@@ -149,21 +168,27 @@ function installElectronRuntime(electronDir, version) {}}+function resetRuntime(electronDir) {+ NodeFS.rmSync(NodePath.join(electronDir, "dist"), { recursive: true, force: true });+ NodeFS.rmSync(NodePath.join(electronDir, "path.txt"), { force: true });+}+export function ensureElectronRuntime() {const electronPackageJsonPath = require.resolve("electron/package.json");const electronPackageJson = JSON.parse(NodeFS.readFileSync(electronPackageJsonPath, "utf8"));const electronDir = NodePath.dirname(electronPackageJsonPath);const platformPath = getPlatformPath();const electronPath = NodePath.join(electronDir, "dist", platformPath);- const missingBeforeInstall = missingRuntimePaths(electronDir, platformPath);- const invalidBeforeInstall = invalidRuntimePaths(electronDir, platformPath);-- if (missingBeforeInstall.length > 0 || invalidBeforeInstall.length > 0) {- if (NodeFS.existsSync(NodePath.join(electronDir, "dist"))) {- NodeFS.rmSync(NodePath.join(electronDir, "dist"), { recursive: true, force: true });+ const needsInstall = () =>+ missingRuntimePaths(electronDir, platformPath).length > 0 ||+ invalidRuntimePaths(electronDir, platformPath).length > 0;++ if (needsInstall()) {+ resetRuntime(electronDir);+ if (!installFromElectronCache(electronDir) || needsInstall()) {+ resetRuntime(electronDir);+ downloadElectronRuntime(electronDir, electronPackageJson.version);}- NodeFS.rmSync(NodePath.join(electronDir, "path.txt"), { force: true });- installElectronRuntime(electronDir, electronPackageJson.version);}const missingAfterInstall = missingRuntimePaths(electronDir, platformPath);diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex 2e6aef538fd..e1d927e6f21 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -12,7 +12,9 @@ allowBuilds:browser-tabs-lock: falsebufferutil: falsecore-js: false- electron: true+ # No install script since Electron 44; ensure:electron runs install.js.+ # Keep the key: pnpm fails installs that still record a removed `true`.+ electron: falseelectron-winstaller: falseesbuild: truemsgpackr-extract: true