Skip to content

The agent that grows with you

fix(desktop): a renderer built under other VITE_*/NODE_ENV settings is never reused

kshitijk4poorbc0758c393c6
diff --git a/scripts/build/freshness.mjs b/scripts/build/freshness.mjs
index f013d794902..d97b6365f06 100644
--- a/scripts/build/freshness.mjs
+++ b/scripts/build/freshness.mjs
@@ -127,12 +127,23 @@ function stampClock(prepared = {}) {
return identity ? (identity.builtAt ?? null) : null
}
-export function buildInputs(source, product, prepared = {}) {
+// Vite inlines every VITE_* variable into the renderer and NODE_ENV selects its mode (and the React
+// Compiler's dev output), so a renderer is a function of these as much as of its sources: a
+// VITE_PERF_PROBE=1 build must never be reused or certified for a plain one. An unset NODE_ENV is
+// the production default vite's build() itself writes into process.env, so a build that started
+// without one still matches the receipt it records afterwards.
+function rendererEnv(env) {
+ return Object.entries({ ...env, NODE_ENV: env.NODE_ENV || 'production' })
+ .filter(([key]) => key === 'NODE_ENV' || key.startsWith('VITE_')).sort()
+}
+
+export function buildInputs(source, product, prepared = {}, env = process.env) {
return {
sourceHash: sourceHash(source, product),
prepared: Object.entries(prepared).sort().map(([name, path]) => ({
name, path: resolve(path), hash: name === 'stamp' ? stampContentHash(resolve(path)) : treeHash(resolve(path), ['.'], () => false),
})),
+ ...(product === 'desktop' ? { env: rendererEnv(env) } : {}),
}
}
@@ -184,15 +195,17 @@ export function productCurrent({ source, product, out, prepared }) {
} catch { return false }
}
-/** True when ``out`` holds a desktop product whose receipt matches ``inputs`` in everything but
- * the install stamp and whose bytes are intact. Only the main/preload bundles bake the stamp, so
- * the renderer such a product carries is still the one these inputs compile to. */
+/** True when ``out`` holds a desktop product whose receipt matches ``inputs`` (sources, renderer
+ * environment, prepared inputs) in everything but the install stamp and whose bytes are intact.
+ * Only the main/preload bundles bake the stamp, so the renderer such a product carries is still
+ * the one these inputs compile to. */
export function rendererCurrent(out, inputs) {
try {
const saved = hostReceipt(out, 'desktop')
const unstamped = list => JSON.stringify(list.filter(({ name }) => name !== 'stamp'))
return !!saved
&& saved.inputs.sourceHash === inputs.sourceHash
+ && JSON.stringify(saved.inputs.env) === JSON.stringify(inputs.env)
&& unstamped(saved.inputs.prepared) === unstamped(inputs.prepared)
&& saved.outputHash === outputHash(out)
} catch { return false }
diff --git a/tests-js/desktop-builder.test.mjs b/tests-js/desktop-builder.test.mjs
index 6c17ec5d83a..157a3fe93c4 100644
--- a/tests-js/desktop-builder.test.mjs
+++ b/tests-js/desktop-builder.test.mjs
@@ -206,6 +206,37 @@ test('a stamp-only change reuses the renderer bytes and rebakes only main/preloa
expect(files(join(input.out, 'assets')).map(([, b]) => Buffer.from(b, 'base64').toString()).join('')).toContain('changed source')
}, 60000)
+test('a renderer built under different VITE_*/NODE_ENV settings is never reused or certified', async () => {
+ const { buildDesktop } = await import('../scripts/build/desktop.mjs')
+ const { productCurrent } = await import('../scripts/build/freshness.mjs')
+ const input = fixture()
+ const restamp = commit => put(input.stamp, JSON.stringify({ ...JSON.parse(readFileSync(input.stamp, 'utf8')), commit }))
+ const saved = { probe: process.env.VITE_PERF_PROBE, node: process.env.NODE_ENV }
+ try {
+ process.env.NODE_ENV = 'production' // vitest itself runs with NODE_ENV=test
+ process.env.VITE_PERF_PROBE = '1'
+ expect((await buildDesktop(input)).reusedRenderer).toBe(false)
+ delete process.env.VITE_PERF_PROBE
+ // The probe build's receipt no longer describes a plain build: neither gate accepts it.
+ expect(productCurrent({ ...input, product: 'desktop' })).toBe(false)
+ restamp('e'.repeat(40))
+ expect((await buildDesktop(input)).reusedRenderer).toBe(false)
+ // Unset NODE_ENV means production (vite's own build default): the production receipt stays
+ // current without it, and the next stamp-only build reuses it.
+ delete process.env.NODE_ENV
+ expect(productCurrent({ ...input, product: 'desktop' })).toBe(true)
+ restamp('f'.repeat(40))
+ expect((await buildDesktop(input)).reusedRenderer).toBe(true)
+ process.env.NODE_ENV = 'development'
+ restamp('a'.repeat(40))
+ expect((await buildDesktop(input)).reusedRenderer).toBe(false)
+ } finally {
+ for (const [key, value] of [['VITE_PERF_PROBE', saved.probe], ['NODE_ENV', saved.node]]) {
+ if (value === undefined) delete process.env[key]; else process.env[key] = value
+ }
+ }
+}, 90000)
+
test('the mid-compile guard ignores the build clock but still fails on a real provenance change', async () => {
const { buildDesktop } = await import('../scripts/build/desktop.mjs')
const { buildInputs, recordProduct } = await import('../scripts/build/freshness.mjs')