Skip to content

The agent that grows with you

refactor(desktop): one receipt reader, one output list, no native double copy on renderer reuse

kshitijk4poor3fc37481eb6d
diff --git a/apps/desktop/scripts/bundle-electron-main.mjs b/apps/desktop/scripts/bundle-electron-main.mjs
index 878d0ad5ecb..b6d7cd6998b 100644
--- a/apps/desktop/scripts/bundle-electron-main.mjs
+++ b/apps/desktop/scripts/bundle-electron-main.mjs
@@ -27,6 +27,11 @@ function productIdentity(source, stamp) {
}).trim()
}
+/** Every file bundleElectronMain writes: the only outputs that bake the install stamp. */
+export const electronOutputs = Object.freeze({
+ main: 'electron-main.mjs', preload: 'electron-preload.js', previewGuestPreload: 'preview-guest-preload.js',
+})
+
/** Bundle main/preload using only the prepared workspace's compiler. */
export async function bundleElectronMain({ source, out, stamp, dev = false }) {
source = resolve(source)
@@ -64,14 +69,14 @@ export async function bundleElectronMain({ source, out, stamp, dev = false }) {
// relaunches before main.ts loads; Electron reads it only from argv.
entryPoints: [join(source, 'apps/desktop/electron/entry.ts')],
format: 'esm',
- outfile: join(out, 'electron-main.mjs'),
+ outfile: join(out, electronOutputs.main),
banner: { js: "import { createRequire } from 'module'; const require = createRequire(import.meta.url);" + envBanner },
})
await build({
...common,
entryPoints: [join(source, 'apps/desktop/electron/preload.ts')],
format: 'cjs',
- outfile: join(out, 'electron-preload.js'),
+ outfile: join(out, electronOutputs.preload),
})
// Preview-pane <webview> guest preload; main.ts hands this path to the
// preview webview via will-attach-webview.
@@ -79,7 +84,7 @@ export async function bundleElectronMain({ source, out, stamp, dev = false }) {
...common,
entryPoints: [join(source, 'apps/desktop/electron/preview-guest-preload-entry.ts')],
format: 'cjs',
- outfile: join(out, 'preview-guest-preload.js'),
+ outfile: join(out, electronOutputs.previewGuestPreload),
})
return { stampClock: bakedStamp }
}
diff --git a/scripts/build/desktop.mjs b/scripts/build/desktop.mjs
index 17f544c4e85..dcd5fff0f2a 100644
--- a/scripts/build/desktop.mjs
+++ b/scripts/build/desktop.mjs
@@ -1,9 +1,9 @@
#!/usr/bin/env node
import { execFileSync } from 'node:child_process'
import { cpSync, existsSync, readdirSync } from 'node:fs'
-import { dirname, join, relative, resolve } from 'node:path'
+import { dirname, join, relative, resolve, sep } from 'node:path'
import { pathToFileURL } from 'node:url'
-import { bundleElectronMain } from '../../apps/desktop/scripts/bundle-electron-main.mjs'
+import { bundleElectronMain, electronOutputs } from '../../apps/desktop/scripts/bundle-electron-main.mjs'
import { checkDistBuilt } from '../../apps/desktop/scripts/assert-dist-built.mjs'
import { classifyNativeBinary } from '../../apps/desktop/scripts/stage-native-deps.mjs'
import { copyNativeTree } from '../../apps/desktop/scripts/prepared-native-deps.mjs'
@@ -28,8 +28,9 @@ function validateNativeTree(nativeDeps, platform) {
// Only main/preload bake the install stamp (bundleElectronMain); the renderer is a function of
// the source tree, icons and native inputs alone. A new commit with no desktop change therefore
// leaves the published renderer valid: reuse it when the receipt proves those inputs and the
-// output bytes are unchanged, and rebuild only what carries the stamp.
-const stampedOutputs = new Set(['electron-main.mjs', 'electron-preload.js', 'preview-guest-preload.js'])
+// output bytes are unchanged, and rebuild only what carries the stamp. The native trees are
+// recopied from nativeDeps below either way.
+const notReused = new Set([...Object.values(electronOutputs), 'node_modules', 'native'])
/** Pure desktop compilation. out is the dist directory, not the source app. */
export async function buildDesktop({ source, out, icons, stamp, nativeDeps, typecheck = false, platform = process.platform }) {
@@ -47,10 +48,11 @@ export async function buildDesktop({ source, out, icons, stamp, nativeDeps, type
if (!existsSync(join(publicIcons, 'apple-touch-icon.png'))) throw new Error(`Missing desktop icon: ${join(publicIcons, 'apple-touch-icon.png')}`)
validateNativeTree(resolve(nativeDeps), platform)
const inputs = buildInputs(source, 'desktop', { icons: publicIcons, stamp, nativeDeps })
- const reuse = rendererCurrent(out, inputs)
+ // A typechecked build must run the check: a reused renderer never passed through tsc here.
+ const reuse = !typecheck && rendererCurrent(out, inputs)
await withProduct(out, async (product, scratch) => {
if (reuse) {
- cpSync(out, product, { recursive: true, filter: file => !stampedOutputs.has(relative(out, file)) })
+ cpSync(out, product, { recursive: true, filter: file => !notReused.has(relative(out, file).split(sep)[0]) })
} else await compileRenderer(product, scratch)
// The receipt must carry the clock this output BAKED, not whatever the live
// stamp says by now: electron-builder copies that live file into the bundle
diff --git a/scripts/build/freshness.mjs b/scripts/build/freshness.mjs
index cc7938b0fcf..f013d794902 100644
--- a/scripts/build/freshness.mjs
+++ b/scripts/build/freshness.mjs
@@ -156,13 +156,19 @@ export function recordProduct({ source, product, out, inputs, stampClock }) {
}) + '\n')
}
+/** The receipt in ``out`` when it was written for ``product`` by this platform, arch and Node. */
+function hostReceipt(out, product) {
+ const saved = JSON.parse(readFileSync(join(out, receiptName), 'utf8'))
+ return saved.schema === 1 && saved.product === product
+ && saved.platform === process.platform && saved.arch === process.arch
+ && saved.node === process.versions.node ? saved : null
+}
+
export function productCurrent({ source, product, out, prepared }) {
try {
- const saved = JSON.parse(readFileSync(join(out, receiptName), 'utf8'))
- const paths = prepared ?? preparedPaths(saved.inputs)
- return saved.schema === 1 && saved.product === product
- && saved.platform === process.platform && saved.arch === process.arch
- && saved.node === process.versions.node
+ const saved = hostReceipt(out, product)
+ const paths = saved && (prepared ?? preparedPaths(saved.inputs))
+ return !!saved
// The pre-build gate, unlike recordProduct, must also see the clock. A restamp
// that lands after the build (a racing write-build-stamp, or a second builder
// that reached extraResources first) leaves dist baking one builtAt while the
@@ -183,10 +189,9 @@ export function productCurrent({ source, product, out, prepared }) {
* the renderer such a product carries is still the one these inputs compile to. */
export function rendererCurrent(out, inputs) {
try {
- const saved = JSON.parse(readFileSync(join(out, receiptName), 'utf8'))
+ const saved = hostReceipt(out, 'desktop')
const unstamped = list => JSON.stringify(list.filter(({ name }) => name !== 'stamp'))
- return saved.schema === 1 && saved.product === 'desktop'
- && saved.platform === process.platform && saved.arch === process.arch && saved.node === process.versions.node
+ return !!saved
&& saved.inputs.sourceHash === inputs.sourceHash
&& unstamped(saved.inputs.prepared) === unstamped(inputs.prepared)
&& saved.outputHash === outputHash(out)