diff --git a/packages/compiler-test-utils/oracle/README.md b/packages/compiler-test-utils/oracle/README.md index 1e56f03c51..49f13688a2 100644 --- a/packages/compiler-test-utils/oracle/README.md +++ b/packages/compiler-test-utils/oracle/README.md @@ -167,7 +167,10 @@ chunk layout, printed code or AST shapes. - **Laziness and provenance.** Which modules a compiler emitted is told by provenance (the plugin's own `load` and `transform` hooks observed in that build), never by id, path or the helper that builds a lazy value. - **Dead code (G8)** is decided on a real production build: a dev module that reads a name it never declares fails - only if the production build of the same app still reads it. + if the production build of the same app still reads it, or if that build fails the build harness's own + dangling-name check (it may spell the name otherwise). Routes compiled with route HMR, and two direct + module-alone checks (router-plugin `integration/pages/code-splitter-imports-exports.test.ts` and + `router-plugin-context.test.ts`), still judge the dev module alone, which is stricter than G8. ## Diagnostics diff --git a/packages/router-plugin/tests/code-splitter-scope.test.ts b/packages/router-plugin/tests/code-splitter-scope.test.ts index f244808ed5..49ddae71aa 100644 --- a/packages/router-plugin/tests/code-splitter-scope.test.ts +++ b/packages/router-plugin/tests/code-splitter-scope.test.ts @@ -8,7 +8,6 @@ */ import { parseSync, transformWithOxc } from 'vite' import { describe, expect, it } from 'vitest' -import { getModuleErrors } from '@tanstack/compiler-test-utils/validate-module' import { expectValidModules, head, loadRoute } from './route-app' import type { Plugin } from 'vite' @@ -623,16 +622,7 @@ export const Route = createFileRoute('/')({ ) // Every module as the browser receives it is valid (read as // TypeScript, as the source's angle brackets are not JSX) - const { modules } = await route.modules() - for (const [name, code] of Object.entries(modules)) { - expect( - await getModuleErrors(code, 'module.ts', { - env: 'client', - globals: ['$RefreshReg$', '$RefreshSig$'], - }), - name, - ).toEqual([]) - } + await expectValidModules(route) expect( await route.run(`return [options.loader(), await render('component')]`), ).toEqual([1, 'page']) diff --git a/packages/router-plugin/tests/route-app.ts b/packages/router-plugin/tests/route-app.ts index fc7903059c..acad9eedf4 100644 --- a/packages/router-plugin/tests/route-app.ts +++ b/packages/router-plugin/tests/route-app.ts @@ -52,7 +52,11 @@ import { parseSync } from 'vite' import { expect } from 'vitest' import { createPluginHost } from '@tanstack/compiler-test-utils/plugin-host' import { currentTest } from '@tanstack/compiler-test-utils/test-scope' -import { getModuleErrors } from '@tanstack/compiler-test-utils/validate-module' +import { DanglingNameError } from '@tanstack/compiler-test-utils/build-harness' +import { + freeIdentifiers, + getModuleErrors, +} from '@tanstack/compiler-test-utils/validate-module' import { tanstackRouter } from '../src/vite' import type { ShippedModule } from '@tanstack/compiler-test-utils/validate-module' import type { ChannelDiagnostic } from '@tanstack/compiler-test-utils/diagnostic-channel' @@ -1100,9 +1104,13 @@ return (await compiled()).filter((request) => !before.has(request))`) * input is the generated route tree, with its export kept, as an app's * router reads it: each route's `Route` and what its options load, not * the route file's other exports. Its output goes under `node_modules/`, - * which the dev server's watcher ignores. + * which the dev server's watcher ignores. `dangling`: the names the build's + * own dangling-name check (the production client check of every build the + * harness describes) finds in the modules the compilers produced: the + * build may read a dangling name the dev server's modules spell + * otherwise. */ - const shipped = async (): Promise => { + const shipped = async (): Promise => { await writeRoute(state, owner, files) const outDir = path.join(state.host.root, 'node_modules/.route-app-build') const built = await state.host.build({ @@ -1122,9 +1130,18 @@ return (await compiled()).filter((request) => !before.has(request))`) if (!client) { throw new Error('the production build has no client environment') } + let dangling: Array = [] + try { + await built.describe('client') + } catch (error) { + if (!(error instanceof DanglingNameError)) { + throw error + } + dangling = [...new Set(Object.values(error.names).flat())] + } // A free name counts wherever the build still reads it: code a // compiler generates without mappings (ยง5.14) maps to no file - return { chunks: client.chunks, outDir: client.outDir } + return { build: { chunks: client.chunks, outDir: client.outDir }, dangling } } const route: RouteApp = { @@ -1250,10 +1267,17 @@ const routeSources = new WeakMap< { code: string filename: string - shipped: (() => Promise) | undefined + shipped: (() => Promise) | undefined } >() +/** The production build of a route app (`shipped` of `routeSources`). */ +interface ShippedBuild { + build: ShippedModule + /** The free names its own dangling-name check found. */ + dangling: Array +} + /** * Asserts that every module the compilers emitted for the route is a valid * client module as the browser receives it (SPEC G1, G8 at module level): @@ -1267,7 +1291,10 @@ const routeSources = new WeakMap< * with one is checked again against a `vite build` of the same app, and the * name counts only if the build's client chunks still read it free * (`build` of `getModuleErrors`, without a module: anywhere in the chunks, - * since generated code may map to no file). The check of the module alone runs + * since generated code may map to no file), and the build itself must pass + * the build harness's dangling-name check, except for names the route file + * itself reads free (a production module may spell a dangling name + * otherwise than the dev server's). The check of the module alone runs * first, and the build only when it finds a free name, so a correct * compiler costs no build. Parse and binding errors need no build. A route * compiled with route HMR (`hmr: true`) keeps the check of the module @@ -1281,10 +1308,13 @@ export async function expectValidModules( : route const source = routeSources.get(modules) const errors: Record> = {} + // Served code is JavaScript; read with the route file's language, as + // the `source` comparison needs (in a `.ts` file, `value` is a type + // assertion, not JSX) + const filename = + source && /\.[cm]?ts$/.test(source.filename) ? 'module.ts' : 'module.tsx' const check = (code: string, build?: ShippedModule) => - // Served code is JavaScript; read with the route file's language, as - // the `source` comparison needs - getModuleErrors(code, 'module.tsx', { + getModuleErrors(code, filename, { env: 'client', source: source?.code, globals: refreshGlobals, @@ -1297,12 +1327,21 @@ export async function expectValidModules( source?.shipped && Object.values(errors).some((found) => found.length > 0) ) { - const build = await source.shipped() + const { build, dangling } = await source.shipped() for (const [name, code] of Object.entries(modules)) { if (errors[name]!.length > 0) { errors[name] = await check(code, build) } } + const sourceReads = await freeIdentifiers(source.code, filename, { + env: 'client', + }) + const shippedErrors = dangling + .filter((name) => !sourceReads.has(name)) + .map((name) => `\`${name}\` is read free by the production build`) + if (shippedErrors.length > 0) { + errors['production build'] = shippedErrors + } } expect(errors).toEqual( Object.fromEntries(Object.keys(modules).map((name) => [name, []])),