diff --git a/.changepacks/changepack_log_prewarm_warn_unreachable.json b/.changepacks/changepack_log_prewarm_warn_unreachable.json new file mode 100644 index 000000000..c23ff737d --- /dev/null +++ b/.changepacks/changepack_log_prewarm_warn_unreachable.json @@ -0,0 +1,8 @@ +{ + "changes": { + "packages/next-plugin/package.json": "Patch", + "packages/webpack-plugin/package.json": "Patch" + }, + "note": "A production prewarm no longer fails the build on a file it cannot extract. The Next.js prewarm still extracts every source file, but a file that does not parse (for example dead source no route imports) is now skipped with a warning naming the file and the error. If Next.js or webpack compiles that file, its loader still fails the build with the same error. Output for projects without such files is unchanged.", + "date": "2026-11-19T00:00:00.000Z" +} diff --git a/apps/landing/src/app/(detail)/docs/build-errors/page.mdx b/apps/landing/src/app/(detail)/docs/build-errors/page.mdx index 26e7c895f..b597ec384 100644 --- a/apps/landing/src/app/(detail)/docs/build-errors/page.mdx +++ b/apps/landing/src/app/(detail)/docs/build-errors/page.mdx @@ -1965,6 +1965,8 @@ E.__emotion_user(); const broken; **Fix:** Correct the syntax at the reported location, then fix any style diagnostic the build reports next. +A production `next build` first extracts every source file of the project, including files no route imports. A file it cannot extract there does not fail the build: the plugin prints a warning naming the file and the error, `[devup-ui] Turbopack prewarm of failed: . ...` (or `[devup-ui] webpack stylesheet warm-up of failed: . ...` with webpack), and skips it. If Next.js compiles that file, the build still fails with the error above. + {/* probe: parse-error after */} ```tsx diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index b70090bbd..a6fc7c79e 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -1833,6 +1833,69 @@ export const box = style({ color: 'red' })` } }) + it('skips an unreachable file it cannot extract with a warning, and goes on', () => { + process.env.TURBOPACK = '1' + // Given a dead file no route imports, which does not parse. + const [page, dead, late] = [ + 'src/app/page.tsx', + 'src/dead.tsx', + 'src/late.tsx', + ].map((file) => resolve(file)) + const files = [page, dead, late] + const graphSpy = spyOn( + importGraphModule, + 'buildStaticImportGraph', + ).mockReturnValue({ + files, + fileSet: new Set(files), + staticImports: new Map(files.map((file) => [file, new Set()])), + staticImporters: new Map( + files.map((file) => [file, new Set()]), + ), + dynamicTargets: new Set(), + dynamicImports: new Map(files.map((file) => [file, new Set()])), + externalImports: new Map( + files.map((file) => [file, new Set()]), + ), + }) + const compiledSpy = spyOn( + importGraphModule, + 'computeCompiledFiles', + ).mockReturnValue(['src/app/page.tsx']) + codeExtractWithoutSourceMapSpy.mockImplementation( + (filename: string, contents: string) => { + if (filename === 'src/dead.tsx') + throw new Error('Cannot parse source: Unexpected token') + return createCodeExtractResult(contents) + }, + ) + const warn = spyOn(console, 'warn').mockImplementation(() => {}) + try { + // When the production prewarm runs, the build is not failed. + expect(() => DevupUI({})).not.toThrow() + + // Then the dead file is named, left out, and the files after it prewarm. + expect(warn).toHaveBeenCalledWith( + '[devup-ui] Turbopack prewarm of src/dead.tsx failed: Cannot parse source: Unexpected token. The file is skipped; if Next.js compiles it, the build fails with this error.', + ) + expect(codeExtractWithoutSourceMapSpy).toHaveBeenCalledTimes(3) + expect(startCoordinatorSpy).toHaveBeenCalledWith( + expect.objectContaining({ + expectedBaseFiles: ['src/app/page.tsx'], + prewarmedFiles: ['src/app/page.tsx', 'src/late.tsx'], + }), + ) + const coordinatorOptions = startCoordinatorSpy.mock.calls[0]?.[0] + expect([ + ...(coordinatorOptions?.prewarmedOutputs?.keys() ?? []), + ]).toEqual(['src/app/page.tsx', 'src/late.tsx']) + } finally { + warn.mockRestore() + graphSpy.mockRestore() + compiledSpy.mockRestore() + } + }) + it('prewarms the same complete file set in singleCss mode', () => { process.env.TURBOPACK = '1' const compiledSpy = spyOn( diff --git a/packages/next-plugin/src/__tests__/prewarm-error.wasm.test.ts b/packages/next-plugin/src/__tests__/prewarm-error.wasm.test.ts new file mode 100644 index 000000000..defeb0e43 --- /dev/null +++ b/packages/next-plugin/src/__tests__/prewarm-error.wasm.test.ts @@ -0,0 +1,129 @@ +import { existsSync, mkdtempSync, rmSync, watch } from 'node:fs' +import { tmpdir } from 'node:os' +import { join, relative } from 'node:path' + +import * as wasm from '@devup-ui/wasm' +import { describe, expect, it } from 'bun:test' + +import { + flushCoordinatorWrites, + resetCoordinator, + startCoordinator, +} from '../coordinator' +import loader, { resetInit } from '../loader' +import { withModuleResolver } from '../wasm' + +const page = `import { Box } from '@devup-ui/react'; +export const box = ;` +const dead = `import { Box } from '@devup-ui/react'; +export const box = ; const broken;` + +function captureState() { + const sheet = wasm.exportSheet() + const classes = wasm.exportClassMap() + const files = wasm.exportFileMap() + const canonical = wasm.exportCanonicalMap() + return () => { + wasm.importSheet(JSON.parse(sheet)) + wasm.importClassMap(JSON.parse(classes)) + wasm.importFileMap(JSON.parse(files)) + wasm.importCanonicalMap(JSON.parse(canonical)) + } +} + +describe('real WASM prewarm of a file that does not parse', () => { + it('leaves the sheet as it was, and fails the loader that compiles the file', async () => { + const restore = captureState() + const root = mkdtempSync(join(tmpdir(), 'devup-ui-prewarm-error-')) + const coordinatorPortFile = join(root, 'port') + const watcher = watch(root) + let readinessTimeout: ReturnType | undefined + let coordinator: ReturnType | undefined + try { + wasm.importCanonicalMap({}) + wasm.setModuleResolver(undefined) + const key = (file: string) => + relative(process.cwd(), join(root, file)).replaceAll('\\', '/') + const args = ['@devup-ui/react', './', true, false, true, {}] as const + // Given the prewarm extracts a valid page, then a dead file. + wasm.codeExtractWithoutSourceMap(key('page.tsx'), page, ...args).free() + const css = wasm.getCss(null, false) + // When the dead file does not parse, the prewarm sees the real error. + expect(() => + wasm.codeExtractWithoutSourceMap(key('dead.tsx'), dead, ...args), + ).toThrow('Cannot parse source') + // Then skipping it leaves the sheet byte-identical. + expect(wasm.getCss(null, false)).toBe(css) + + const ready = new Promise((resolve, reject) => { + watcher.on('error', reject) + readinessTimeout = setTimeout( + () => reject(new Error('Coordinator port file was not ready')), + 3000, + ) + watcher.on('change', () => { + if (existsSync(coordinatorPortFile)) resolve() + }) + }) + const options = { + package: '@devup-ui/react', + cssDir: root, + sheetFile: join(root, 'sheet.json'), + classMapFile: join(root, 'classes.json'), + fileMapFile: join(root, 'files.json'), + singleCss: true, + coordinatorPortFile, + } + coordinator = startCoordinator({ + ...options, + distDir: root, + wasm, + importAliases: {}, + canonicalMap: {}, + prewarmedFiles: [key('page.tsx')], + expectedBaseFiles: [key('page.tsx')], + }) + await ready + // When a route imports the dead file and Next.js compiles it, its + // loader fails the build with the same error. + const error = await new Promise((resolve) => { + const context = { + resourcePath: join(root, 'dead.tsx'), + getOptions: () => ({ + ...options, + themeFile: join(root, 'theme.json'), + watch: false, + defaultSheet: {}, + defaultClassMap: {}, + defaultFileMap: {}, + }), + addDependency: (_path: string) => {}, + async: () => { + const callback: ReturnType< + ThisParameterType['async'] + > = (error) => resolve(error) + return callback + }, + } satisfies Pick< + ThisParameterType, + 'resourcePath' | 'getOptions' | 'addDependency' | 'async' + > + Reflect.apply(loader, context, [Buffer.from(dead)]) + }) + expect(error?.message).toContain('Cannot parse source') + } finally { + clearTimeout(readinessTimeout) + watcher.close() + coordinator?.close() + try { + await flushCoordinatorWrites() + } finally { + resetCoordinator() + resetInit() + restore() + withModuleResolver(wasm) + rmSync(root, { recursive: true, force: true }) + } + } + }) +}) diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index a9dc21d09..d593fe057 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -527,7 +527,8 @@ export function DevupUI( // entries synchronously first. The wider set covers template imports, MDX // dependencies and package-level globalCss (notably reset-css) that the // route graph cannot represent. Loader-time extraction uses the same - // keys/options and is idempotent. + // keys/options and is idempotent. A file that fails to extract is skipped + // with a warning; the loader reports its error if Next.js compiles it. const prewarmedFiles: string[] = [] const prewarmedOutputs = new Map() if (!watch && staticGraph) { @@ -561,19 +562,35 @@ export function DevupUI( } const extractStartedAt = prewarmStartedAt === undefined ? undefined : performance.now() - const output = takeExtractOutput( - extract( - filename, - source, - libPackage, - relCssDir, - singleCss, + let output: ReturnType + try { + output = takeExtractOutput( + extract( + filename, + source, + libPackage, + relCssDir, + singleCss, + false, + true, + importAliases as unknown as Record, + ), false, - true, - importAliases as unknown as Record, - ), - false, - ) + ) + } catch (error) { + // The prewarm covers every source file, including ones no route + // imports, so a file it cannot extract must not fail the build. It + // stays out of the prewarm: when Next.js compiles it, its loader + // extracts it again and fails the build with the same error. + console.warn( + setupWarning( + `prewarm of ${filename}`, + error, + 'The file is skipped; if Next.js compiles it, the build fails with this error.', + ), + ) + continue + } if (extractStartedAt !== undefined) { prewarmExtractMs += performance.now() - extractStartedAt } diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index ca2499b9d..aaab9d8d8 100644 --- a/packages/webpack-plugin/src/__tests__/plugin.test.ts +++ b/packages/webpack-plugin/src/__tests__/plugin.test.ts @@ -1533,7 +1533,7 @@ describe('devupUIWebpackPlugin', () => { }, ) - it.each(['canonical', 'numbering', 'read', 'extract'])( + it.each(['canonical', 'numbering', 'read'])( 'propagates genuine %s errors for an explicit complete set', (stage) => { // Given a failing correctness-prewarm dependency. @@ -1550,7 +1550,6 @@ describe('devupUIWebpackPlugin', () => { if (stage === 'canonical') buildCanonicalMapSpy.mockImplementation(fail) if (stage === 'numbering') numbering.mockImplementation(fail) if (stage === 'read') readFileSyncSpy.mockImplementation(fail) - if (stage === 'extract') codeExtractSpy.mockImplementation(fail) const plugin = new DevupUIWebpackPlugin({ engine: 'wasm', debug: stage === 'numbering', @@ -1577,6 +1576,38 @@ describe('devupUIWebpackPlugin', () => { }, ) + it('skips a file of an explicit complete set it cannot extract, with a warning', () => { + // Given a complete set holding a dead file that does not parse. + readFileSyncSpy.mockImplementation((path: string) => String(path)) + codeExtractSpy.mockImplementation( + (filename: string, contents: string) => { + if (filename === 'src/dead.tsx') + throw new Error('Cannot parse source: Unexpected token') + return createCodeExtractResult(contents) + }, + ) + const plugin = new DevupUIWebpackPlugin({ + engine: 'wasm', + productionPrewarmFiles: ['src/a.tsx', 'src/dead.tsx', 'src/z.tsx'], + }) + const compiler = createCompiler() + const warn = spyOn(console, 'warn').mockImplementation(() => {}) + try { + plugin.apply(asCompiler(compiler)) + // When the warm-up runs, the build goes on past the dead file. + expect(() => compiler.hooks.run.tap.mock.calls[0][1]()).not.toThrow() + // Then every file is still tried, and the dead one is named. + expect(codeExtractSpy.mock.calls.map(([filename]) => filename)).toEqual( + ['src/a.tsx', 'src/dead.tsx', 'src/z.tsx'], + ) + expect(warn).toHaveBeenCalledWith( + '[devup-ui] webpack stylesheet warm-up of src/dead.tsx failed: Cannot parse source: Unexpected token. The file is skipped; if webpack compiles it, the build fails with this error.', + ) + } finally { + warn.mockRestore() + } + }) + it('pre-warms without collapse, from no entries when they are a function', () => { buildCanonicalMapSpy.mockReturnValue({}) const compiler = createCompiler() @@ -1613,11 +1644,41 @@ describe('devupUIWebpackPlugin', () => { throw new Error('extract boom') }) const plugin = new DevupUIWebpackPlugin({ engine: 'wasm' }) - // apply must still complete without throwing - const compiler = createCompiler() - plugin.apply(asCompiler(compiler)) - compiler.hooks.run.tap.mock.calls[0][1]() - expect(codeExtractSpy).toHaveBeenCalled() + const warn = spyOn(console, 'warn').mockImplementation(() => {}) + try { + // apply must still complete without throwing + const compiler = createCompiler() + plugin.apply(asCompiler(compiler)) + compiler.hooks.run.tap.mock.calls[0][1]() + expect(codeExtractSpy).toHaveBeenCalled() + expect(warn).toHaveBeenCalledWith( + '[devup-ui] webpack stylesheet warm-up of src/parent.tsx failed: extract boom. The file is skipped; if webpack compiles it, the build fails with this error.', + ) + } finally { + warn.mockRestore() + } + }) + + it('stops a generic pre-warm it cannot read, with a warning', () => { + const parent = resolve(process.cwd(), 'src', 'parent.tsx') + computeReachableFilesSpy.mockReturnValue([parent]) + readFileSyncSpy.mockImplementation((path: string) => { + if (path === parent) throw new Error('read boom') + return '{}' + }) + const plugin = new DevupUIWebpackPlugin({ engine: 'wasm' }) + const warn = spyOn(console, 'warn').mockImplementation(() => {}) + try { + const compiler = createCompiler() + plugin.apply(asCompiler(compiler)) + compiler.hooks.run.tap.mock.calls[0][1]() + expect(codeExtractSpy).not.toHaveBeenCalled() + expect(warn).toHaveBeenCalledWith( + '[devup-ui] webpack stylesheet warm-up failed: read boom. A stylesheet webpack builds before every file it holds is extracted can miss their styles.', + ) + } finally { + warn.mockRestore() + } }) it('composes collapse + hoist and folds reach onto the canonical bucket', () => { diff --git a/packages/webpack-plugin/src/__tests__/production-prewarm.mjs b/packages/webpack-plugin/src/__tests__/production-prewarm.mjs index 11a745c14..36e970c13 100644 --- a/packages/webpack-plugin/src/__tests__/production-prewarm.mjs +++ b/packages/webpack-plugin/src/__tests__/production-prewarm.mjs @@ -193,8 +193,29 @@ try { 'bad.tsx', 'import { Box } from "@devup-ui/react"; export const c = ', ) - compiler = apply({ productionPrewarmFiles: ['bad.tsx'] }) - await assert.rejects(prepare(compiler)) + write( + 'z-valid.tsx', + 'import { Box } from "@devup-ui/react"; export const c = ', + ) + // A file the complete set holds but cannot extract (dead source) is skipped + // with a warning naming it, and the files after it are still extracted. + const warnings = [] + const warn = console.warn + console.warn = (message) => warnings.push(message) + try { + compiler = apply({ productionPrewarmFiles: ['bad.tsx', 'z-valid.tsx'] }) + await prepare(compiler) + } finally { + console.warn = warn + } + assert.equal(warnings.length, 1) + assert.match( + warnings[0], + /^\[devup-ui\] webpack stylesheet warm-up of bad\.tsx failed: .*Cannot parse source.*The file is skipped; if webpack compiles it, the build fails with this error\.$/, + ) + assert.match(sheet(), /color:orchid/) + // The loader's extraction of the same file still fails with the real error. + assert.throws(() => extract('bad.tsx', false), /Cannot parse source/) await close(compiler) for (const invalid of ['missing.tsx', 'bad.tsx']) { // Given a valid file sorted before the failing file, initialization partially extracts. @@ -202,14 +223,23 @@ try { 'a-valid.tsx', 'import { Box } from "@devup-ui/react"; export const c = ', ) - // When run preparation rejects, the returned compiler still needs awaited shutdown. + // When run preparation rejects (a file it cannot read) or warns (one it + // cannot extract), the returned compiler still needs awaited shutdown. compiler = apply({ prefix: 'failed', shorthands: { insetX: ['left', 'right'] }, atomHoist: 2, productionPrewarmFiles: ['a-valid.tsx', invalid], }) - await assert.rejects(prepare(compiler)) + if (invalid === 'missing.tsx') await assert.rejects(prepare(compiler)) + else { + console.warn = () => {} + try { + await prepare(compiler) + } finally { + console.warn = warn + } + } assert.match(sheet(), /chartreuse/) assert.match(sheet(), /left:19px/) await close(compiler) diff --git a/packages/webpack-plugin/src/plugin.ts b/packages/webpack-plugin/src/plugin.ts index 7874c3b83..147f8d0f2 100644 --- a/packages/webpack-plugin/src/plugin.ts +++ b/packages/webpack-plugin/src/plugin.ts @@ -276,8 +276,9 @@ export class DevupUIWebpackPlugin { * happened to build first. Mirrors the loader's `codeExtract` call (same * filename keying + options) so re-extraction during compilation is * idempotent. Generic entry discovery remains best-effort; an adapter's - * complete set is authoritative and its read/extraction failures must fail - * the build. + * complete set is authoritative and its read failures must fail the build. + * A file that fails to extract is skipped with a warning either way: its + * loader reports the error if webpack compiles it. */ private prewarmExtractor( entries: string[], @@ -312,17 +313,33 @@ export class DevupUIWebpackPlugin { '/', ) if (!relCssDir.startsWith('./')) relCssDir = `./${relCssDir}` - const output = this.compiler.codeExtract( - relativePath, - readFileSync(file, 'utf-8'), - this.options.package, - relCssDir, - this.options.singleCss, - false, - true, - this.importAliases, - ) - output.free() + const source = readFileSync(file, 'utf-8') + try { + this.compiler + .codeExtract( + relativePath, + source, + this.options.package, + relCssDir, + this.options.singleCss, + false, + true, + this.importAliases, + ) + .free() + } catch (error) { + // Next's set covers every source file, including ones no route + // imports, so a file the warm-up cannot extract must not fail the + // build. It stays out of the warm-up: when webpack compiles it, its + // loader extracts it again and fails the build with the same error. + console.warn( + setupWarning( + `webpack stylesheet warm-up of ${relativePath}`, + error, + 'The file is skipped; if webpack compiles it, the build fails with this error.', + ), + ) + } } } catch (error) { if (this.options.productionPrewarmFiles !== undefined) throw error