Skip to content

The agent that grows with you

fix(webapp): warn when a previous chunk cannot be kept across a rebuild

unsupportedpastels148f36ea6cce
diff --git a/scripts/build/webapp.mjs b/scripts/build/webapp.mjs
index 85365fcb771..64c85cde714 100644
--- a/scripts/build/webapp.mjs
+++ b/scripts/build/webapp.mjs
@@ -10,6 +10,10 @@ import { buildInputs, outputIntact, recordProduct, sourceHash } from './freshnes
// Vite's build manifest (apps/desktop/vite.config.ts names it too): the files a generation emitted.
const manifestName = 'renderer-manifest.json'
+// The build still publishes; say why open tabs of the previous generation may miss this chunk.
+const notKept = (from, error) =>
+ console.warn(`webapp: could not keep ${from} for tabs on the previous generation (${error?.code ?? error})`)
+
/** Keep in ``next`` the lazy chunks an open tab of the previous generation still loads: only missing
* regular files from a generation intact against its receipt, never through a symlink or over a file.
* A manifest omits the chunks its generation inherited, so retention stays one generation back. */
@@ -38,17 +42,23 @@ export function keepPreviousGeneration(previous, next) {
}
try {
mkdirSync(dirname(to), { recursive: true })
- } catch {
- continue // e.g. a new file stands where the name's directory would go
+ } catch (error) {
+ // Quiet when a new file stands where the name's directory would go.
+ if (!['EEXIST', 'ENOTDIR'].includes(error?.code)) notKept(from, error)
+ continue
}
try {
copyFileSync(from, to, constants.COPYFILE_EXCL)
kept.push(name)
} catch (error) {
- // Only ever this copy's own partial file (EEXIST: one stood there after all).
+ if (error?.code === 'EEXIST') continue // a new file stood there after all
+ notKept(from, error)
+ // Only ever this copy's own partial file.
try {
- if (error?.code !== 'EEXIST') rmSync(to, { force: true })
- } catch { /* a failed cleanup must not fail the build either */ }
+ rmSync(to, { force: true })
+ } catch (cleanup) {
+ console.warn(`webapp: could not remove the partial copy ${to} (${cleanup?.code ?? cleanup})`)
+ }
}
}
return kept
diff --git a/tests-js/webapp-keep-previous.test.mjs b/tests-js/webapp-keep-previous.test.mjs
new file mode 100644
index 00000000000..bb7b1ab77f8
--- /dev/null
+++ b/tests-js/webapp-keep-previous.test.mjs
@@ -0,0 +1,86 @@
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { dirname, join } from 'node:path'
+import { afterEach, beforeEach, expect, test, vi } from 'vitest'
+
+// A rebuild copies the previous generation's chunks for tabs that still load them; a copy can
+// fail for reasons that are no fault of the entry (a full disk). Fault only the copies a test
+// names; every other copy reaches the real filesystem.
+const faults = vi.hoisted(() => [])
+
+vi.mock('node:fs', async importOriginal => {
+ const actual = await importOriginal()
+ return {
+ ...actual,
+ copyFileSync(from, to, mode) {
+ const fault = faults.find(entry => from.endsWith(entry.name))
+ if (fault) {
+ if (fault.written !== undefined) actual.writeFileSync(to, fault.written)
+ throw Object.assign(new Error(`${fault.code}: copyfile '${from}' -> '${to}'`), { code: fault.code })
+ }
+ return actual.copyFileSync(from, to, mode)
+ },
+ }
+})
+
+const { keepPreviousGeneration } = await import('../scripts/build/webapp.mjs')
+const { buildInputs, recordProduct } = await import('../scripts/build/freshness.mjs')
+
+let root
+let warn
+
+beforeEach(() => {
+ root = mkdtempSync(join(tmpdir(), 'webapp-keep-'))
+ warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+})
+
+afterEach(() => {
+ faults.length = 0
+ vi.restoreAllMocks()
+ rmSync(root, { recursive: true, force: true })
+})
+
+function put(dir, name, content) {
+ mkdirSync(dirname(join(dir, name)), { recursive: true })
+ writeFileSync(join(dir, name), content)
+}
+
+function generations(previousFiles, nextFiles, manifest) {
+ const [previous, next] = [join(root, 'previous'), join(root, 'next')]
+ for (const [name, content] of Object.entries(previousFiles)) put(previous, name, content)
+ for (const [name, content] of Object.entries(nextFiles)) put(next, name, content)
+ put(previous, 'renderer-manifest.json', JSON.stringify(manifest))
+ recordProduct({ source: root, product: 'webapp', out: previous, inputs: buildInputs(root, 'webapp') })
+ return { previous, next }
+}
+
+test('a chunk that cannot be copied is reported with its path, and the rest are still kept', () => {
+ const { previous, next } = generations(
+ { 'index.html': 'old', 'assets/full.js': 'old full', 'assets/ok.js': 'old ok' },
+ { 'index.html': 'new' },
+ { a: { file: 'assets/full.js', assets: ['assets/ok.js'] } },
+ )
+ faults.push({ name: 'assets/full.js', code: 'ENOSPC', written: 'partial' })
+ expect(keepPreviousGeneration(previous, next)).toEqual(['assets/ok.js'])
+ expect(warn).toHaveBeenCalledTimes(1)
+ expect(warn.mock.calls[0][0]).toContain(join(previous, 'assets/full.js'))
+ expect(warn.mock.calls[0][0]).toContain('ENOSPC')
+ // The failed copy's partial file is not published as the chunk.
+ expect(existsSync(join(next, 'assets/full.js'))).toBe(false)
+ expect(readFileSync(join(next, 'assets/ok.js'), 'utf8')).toBe('old ok')
+})
+
+test('a missing previous build, rejected entries and a new file standing in the way stay quiet', () => {
+ expect(keepPreviousGeneration(join(root, 'never-built'), join(root, 'next'))).toEqual([])
+ const { previous, next } = generations(
+ { 'index.html': 'old', 'assets/raced.js': 'old raced', 'x/kept.js': 'old' },
+ { 'index.html': 'new', 'x': 'new file' },
+ { a: { file: 'assets/raced.js', css: ['../escape.css', '/abs.css'], assets: ['assets/gone.js', 'bad\0name.js', 'x/kept.js', 'index.html'] } },
+ )
+ // The new build wrote this name between the existence check and the copy.
+ faults.push({ name: 'assets/raced.js', code: 'EEXIST', written: 'new raced' })
+ expect(keepPreviousGeneration(previous, next)).toEqual([])
+ expect(warn).not.toHaveBeenCalled()
+ expect(readFileSync(join(next, 'assets/raced.js'), 'utf8')).toBe('new raced')
+ expect(readFileSync(join(next, 'x'), 'utf8')).toBe('new file')
+})