diff --git a/packages/compiler-test-utils/oracle/KNOWN-BUGS.md b/packages/compiler-test-utils/oracle/KNOWN-BUGS.md
index d86b4cf0d0..3e681171e8 100644
--- a/packages/compiler-test-utils/oracle/KNOWN-BUGS.md
+++ b/packages/compiler-test-utils/oracle/KNOWN-BUGS.md
@@ -8,7 +8,7 @@ One entry per root cause: the bug, the SPEC clause, the pins (`file` and test na
details. "Cause" files name the implementation the pin was written against (the Babel-based compilers); a rewrite may
have no such file.
-Totals: 380 pin sites in 105 test files (a table test counts once; Vitest counts each row).
+Totals: 381 pin sites in 106 test files (a table test counts once; Vitest counts each row).
Pins whose input picks a name today's compiler generates (S6) flip with a compiler that generates other names. The
adaptive pins next to them, which read the names a compiler actually generates, are the real check.
@@ -385,9 +385,9 @@ Paths under `start-plugin-core/tests/integration/`.
`start-compose-project-path.test.ts`: "the client route tree leaves out the server-only route"; "the client gets the
file, not the client route tree".
-## J3 Route HMR (28 pins: 22 below, 2 in S6, 4 in J2)
+## J3 Route HMR (29 pins: 23 below, 2 in S6, 4 in J2)
-Paths under `router-plugin/tests/`.
+Paths under `router-plugin/tests/` unless given.
- **Inline components that are not plain functions** (`memo`, `forwardRef`, method shorthand, `as`/`satisfies`), and
components inside a route helper function, **are not registered for React Refresh**, so edits do not show. SPEC J3.2,
@@ -431,6 +431,11 @@ Paths under `router-plugin/tests/`.
data in place".
- **With options in a variable, the HMR transform declares the component binding after the object that reads it.**
SPEC G1. `known-bugs-route-hmr.test.ts`: "route options in a variable ($name) evaluate to a route with a component".
+- **Rsbuild: a route module's first server fn breaks the hot update of its loaded component chunk** when Rspack's
+ `splitChunks` puts the modules the chunk gained into a new async chunk: the re-executed module requires them before
+ anything loads that chunk. Upstream, Rspack's HMR runtime (see "Upstream issues"); a plain Rsbuild app fails the
+ same way. SPEC J3.3, G10. `start-plugin-core/tests/integration/rsbuild/first-server-fn-import.test.ts`: "an edit
+ that adds a server fn to the component shows its result on the open page".
## J4 Start environment separation (86 pins: 66 below, 20 in S1–S7)
@@ -753,6 +758,7 @@ None is filed upstream yet. "Remove when" says when a harness change can go.
| Tool | Symptom | What the suite does |
| --------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Rspack 2.2 (vendored watchpack 2.5.2) | a file removal that lands while a compile runs is lost (fixed in watchpack 2.6.0, webpack/watchpack#345) | Rsbuild tests delete or recreate one file per edit and settle before the next write (`start-plugin-core/tests/integration/importProtection/v2-edits*.test.ts`, `v2-hosts.ts`, `integration/rsbuild/dev-server.test.ts`). Remove when Rspack bundles watchpack ≥ 2.6. |
+| Rspack 2.2.7 HMR runtime (webpack 5.110 has the same gap) | a hot update whose module gained imports that `splitChunks` moved to a new async chunk re-executes it before that chunk loads: `Cannot read properties of undefined (reading 'call')` | pinned (J3): `start-plugin-core/tests/integration/rsbuild/first-server-fn-import.test.ts`; rspack#11324/#11922 call it expected (a React Refresh reload 2.2.7's bare `TypeError` never triggers). Remove when Rspack loads an updated chunk's new siblings first. |
| Rspack 2.2 (vendored watchpack 2.5.2) | a file deleted during the watcher's first scan is never reported | the Rsbuild dev harness resolves once every compiler's watcher has scanned the app, and writes the initial files with past modification times (`compiler-test-utils/src/rsbuild-dev-server.ts`). This is what "the server is up" means; it may stay. |
| Vite 8 `experimental.bundledDev` | closing the server during its initial build leaves a rejection unhandled ("Failed to get bundle state"); no public API to await it (vitejs/vite#22991) | before `close()` the harness waits for the initial build, reading one Vite internal (`compiler-test-utils/src/dev-server.ts`). Remove when Vite handles the rejection. |
| Vite 8.3.4 dependency optimizer | keeps writing `node_modules/.vite/deps_temp_*` after `close()` resolved: removing the app fails with `ENOTEMPTY`, or a write into the removed app is an unhandled rejection | before `close()` the Vite dev harnesses wait until each environment's optimizer has scanned and no `deps*_temp_*` directory is left (`closeViteServer` in `compiler-test-utils/src/dev-server.ts`). Remove when Vite awaits its optimizer on close. |
diff --git a/packages/router-plugin/tests/integration/pages/known-bugs-code-splitter.test.ts b/packages/router-plugin/tests/integration/pages/known-bugs-code-splitter.test.ts
index 17826d2964..25d5349727 100644
--- a/packages/router-plugin/tests/integration/pages/known-bugs-code-splitter.test.ts
+++ b/packages/router-plugin/tests/integration/pages/known-bugs-code-splitter.test.ts
@@ -380,31 +380,6 @@ export default store`,
render: '',
html: '
box
',
},
- ])(
- 'module state that the reference module keeps for $name is initialized once',
- async ({ declaration, options = '', render, html }) => {
- const route = await loadRoute(
- `${head}import { init, register } from './state'
-${declaration}
-export const Route = createFileRoute('/')({
-${options} component: () => ${render}
,
-})`,
- { files: { 'state.ts': stateFile } },
- )
- expect(
- await route.run(
- `return [await render('component'), (await app('state.ts')).calls]`,
- ),
- ).toEqual([html, 1])
- },
- )
-
- // The row below compiles like the rows above (the initializer moves to the
- // chunk, SPEC G3) and passes alone and under COMPILER_TESTS_PINS=run, but
- // in this file's order the page's `app('state.ts')` and the chunk get two
- // instances of `state.ts` (a timestamped URL on one side): a harness
- // sequencing issue to resolve, kept pinned until then.
- test.fails.each([
{
name: 'a type alias that reads it',
// Source: real apps: route files that derive a type with
diff --git a/packages/router-plugin/tests/route-app.ts b/packages/router-plugin/tests/route-app.ts
index 2f9070cb1f..0470b8d479 100644
--- a/packages/router-plugin/tests/route-app.ts
+++ b/packages/router-plugin/tests/route-app.ts
@@ -60,7 +60,7 @@ import type { ChannelDiagnostic } from '@tanstack/compiler-test-utils/diagnostic
import type { PageRun } from '@tanstack/compiler-test-utils/browser'
import type { BuildResult } from '@tanstack/compiler-test-utils/build-harness'
import type { PluginHost } from '@tanstack/compiler-test-utils/plugin-host'
-import type { ESTree, Plugin, PluginOption } from 'vite'
+import type { ESTree, Plugin, PluginOption, ViteDevServer } from 'vite'
import type { Config, DeletableNodes } from '../src'
/** A framework the router plugin targets. */
@@ -203,6 +203,12 @@ const entrySource = (filename: string) =>
*/
const emittedEndpoint = '/__route-app__/emitted'
+/**
+ * The dev server's answer to the page: the URL the app's own modules import
+ * each request by now (see `emittedEndpointPlugin`).
+ */
+const stampedEndpoint = '/__route-app__/stamped'
+
/**
* The modules the compilers emitted code for, by environment and module id:
* for each, the hooks that gave it its code (see `observeEmitted`).
@@ -333,53 +339,76 @@ function servedId(url: string) {
}
/**
- * Answers the page's question (`emittedEndpoint`: a JSON list of requests,
- * path and query) from what `observeEmitted` saw: for each, whether the
- * module the client environment serves for it got its code from the
- * compilers.
+ * Answers the page's questions, each a JSON list of requests (path and
+ * query) posted to an endpoint:
+ * - `emittedEndpoint`, from what `observeEmitted` saw: for each, whether the
+ * module the client environment serves for it got its code from the
+ * compilers;
+ * - `stampedEndpoint`: for each, the URL the app's own modules import it by
+ * now. Vite's import analysis adds the time of a module's last hot update
+ * or prune (`t=`) to the imports of it, so a page importing the plain URL
+ * would get a second instance. Asked at import time, never ahead of the
+ * run: a module the previous route imported and the new one does not is
+ * pruned when the page loads the module that dropped the import (the
+ * route module, a chunk), which gives it a new time once the run started.
*/
function emittedEndpointPlugin(emitted: EmittedModules): Plugin {
return {
name: 'route-app:emitted-modules',
configureServer(server) {
- server.middlewares.use(emittedEndpoint, (request, response) => {
- let body = ''
- request.on('data', (chunk) => {
- body += chunk
- })
- request.on('end', () => {
- const graph = server.environments.client.moduleGraph
- // (answered whatever happens: a request left open would hold one
- // of the page's connections to the dev server)
- Promise.resolve()
- .then(() =>
- Promise.all(
- (JSON.parse(body) as Array).map(async (url) => {
- const module = await graph.getModuleByUrl(servedId(url))
- return (
- module?.id != null &&
- (emitted.get(emittedKey('client', module.id))?.size ?? 0) >
- 0
- )
- }),
- ),
- )
- .then(
- (answers) => {
- response.setHeader('content-type', 'application/json')
- response.end(JSON.stringify(answers))
- },
- (error: unknown) => {
- response.statusCode = 500
- response.end(String(error))
- },
- )
- })
+ const moduleOf = (url: string) =>
+ server.environments.client.moduleGraph.getModuleByUrl(servedId(url))
+ answerPosts(server, emittedEndpoint, async (url) => {
+ const module = await moduleOf(url)
+ return (
+ module?.id != null &&
+ (emitted.get(emittedKey('client', module.id))?.size ?? 0) > 0
+ )
+ })
+ answerPosts(server, stampedEndpoint, async (url) => {
+ const module = await moduleOf(url)
+ return module && module.lastHMRTimestamp > 0
+ ? `${url}${url.includes('?') ? '&' : '?'}t=${module.lastHMRTimestamp}`
+ : url
})
},
}
}
+/**
+ * Serves `endpoint`: a JSON list posted to it is answered with `answer` of
+ * each item, as a JSON list (answered whatever happens: a request left open
+ * would hold one of the page's connections to the dev server).
+ */
+function answerPosts(
+ server: ViteDevServer,
+ endpoint: string,
+ answer: (item: string) => Promise,
+) {
+ server.middlewares.use(endpoint, (request, response) => {
+ let body = ''
+ request.on('data', (chunk) => {
+ body += chunk
+ })
+ request.on('end', () => {
+ Promise.resolve()
+ .then(() =>
+ Promise.all((JSON.parse(body) as Array).map(answer)),
+ )
+ .then(
+ (answers) => {
+ response.setHeader('content-type', 'application/json')
+ response.end(JSON.stringify(answers))
+ },
+ (error: unknown) => {
+ response.statusCode = 500
+ response.end(String(error))
+ },
+ )
+ })
+ })
+}
+
/**
* The framework part of the page helper: `mount(target, el)` renders a
* component or the router into `el` with the framework's client renderer
@@ -502,7 +531,7 @@ function requestUrls(): Map {
const actual = url.pathname + url.search
url.searchParams.delete('t')
const request = url.pathname + url.search
- if (url.pathname === '/${probeFile}' || url.pathname === '/${entryFile}' || url.pathname === '${emittedEndpoint}') continue
+ if (url.pathname === '/${probeFile}' || url.pathname === '/${entryFile}' || url.pathname === '${emittedEndpoint}' || url.pathname === '${stampedEndpoint}') continue
if (!seen.has(request)) seen.set(request, actual)
}
return seen
@@ -510,12 +539,22 @@ function requestUrls(): Map {
/**
* The URL to import an app module by to get the instance the page's other
- * modules share: the URL they requested, or the plain one. (The import
- * itself happens in the page's script: Vite rewrites the dynamic imports
- * of the modules it serves.)
+ * modules share: the URL they requested it by, or else the URL they would
+ * import it by now, asked of the dev server at import time
+ * (\`${stampedEndpoint}\`). (The import itself happens in the page's
+ * script: Vite rewrites the dynamic imports of the modules it serves.)
*/
-export function requestUrl(request: string) {
- return requestUrls().get(request) ?? request
+export async function importUrl(request: string): Promise {
+ const requested = requestUrls().get(request)
+ if (requested !== undefined) {
+ return requested
+ }
+ const response = await fetch('${stampedEndpoint}', { method: 'POST', body: JSON.stringify([request]) })
+ if (!response.ok) {
+ throw new Error('the dev server could not tell the URL to import ' + request + ' by: ' + (await response.text()))
+ }
+ const [url]: Array = await response.json()
+ return url
}
/** Starts loading a route option as the router does (errors ignored). */
@@ -885,39 +924,16 @@ async function writeRoute(
state.used = true
}
-/**
- * The URL each request is imported by from the app's modules: Vite's import
- * analysis adds the time of a file's last hot update (`t=`) to the imports
- * of that file, so that a page importing the plain URL would get a second
- * instance. The page's own script imports by these URLs.
- */
-async function stampedUrls(host: PluginHost, requests: Array) {
- const graph = host.dev.vite.environments.client.moduleGraph
- const stamped: Record = {}
- for (const request of requests) {
- const module = await graph.getModuleByUrl(request)
- if (module && module.lastHMRTimestamp > 0) {
- stamped[request] =
- `${request}${request.includes('?') ? '&' : '?'}t=${module.lastHMRTimestamp}`
- }
- }
- return stamped
-}
-
/** The page-side names a script runs with (see `RouteApp.run`). */
function prelude(
routeUrl: string,
own: Array,
at: string,
before: string,
- stamped: Record,
) {
return `const probe = module
const own = new Set(${JSON.stringify(own)})
const routeUrl = ${JSON.stringify(routeUrl)}
-/** The URLs the app's own modules import these by (see \`stampedUrls\`). */
-const stamped = ${JSON.stringify(stamped)}
-const importUrl = (request) => stamped[request] ?? probe.requestUrl(request)
/** Modules the compilers emitted that the page requested so far (path and query; async). */
const compiled = () => probe.compiledRequests(routeUrl, [...own])
/**
@@ -926,8 +942,8 @@ const compiled = () => probe.compiledRequests(routeUrl, [...own])
* a compiler requests it with; async).
*/
const requested = async () => [...new Set((await probe.appRequests(routeUrl, [...own])).map((request) => request.split('?')[0]))]
-/** An app file the test wrote, as the page imports it (the instance the route gets). */
-const app = (file) => import(importUrl('/' + file.replace(/^\\.?\\//, '')))
+/** An app file the test wrote, as the page imports it (the instance the route gets: \`probe.importUrl\`). */
+const app = async (file) => import(await probe.importUrl('/' + file.replace(/^\\.?\\//, '')))
${before}
const exports = (await import(${JSON.stringify(`/${entryFile}`)})).route
const Route = exports.Route
@@ -1075,7 +1091,7 @@ export async function loadRoute(
try {
return await state.host.runClient(
appUrl(probeFile),
- `${prelude(url, [...own], at, `${options.pageScript ?? ''}\n${runOptions.before ?? ''}`, await stampedUrls(state.host, [...own]))}
+ `${prelude(url, [...own], at, `${options.pageScript ?? ''}\n${runOptions.before ?? ''}`)}
// A run that never settles (e.g. a page left suspended while a navigation
// is pending) fails at a bound, instead of holding its page
return await Promise.race([
diff --git a/packages/router-utils/src/compiler/analysis.ts b/packages/router-utils/src/compiler/analysis.ts
index ea787a6ebe..39346ffcad 100644
--- a/packages/router-utils/src/compiler/analysis.ts
+++ b/packages/router-utils/src/compiler/analysis.ts
@@ -304,6 +304,23 @@ export class ModuleAnalysis {
let marker: Marker = null
let ownEffects = false
let codeEffects = false
+ // the bindings import specifiers declare and exports name, from Yuku's
+ // module records by node position: a node lookup (`bindingOf`) would
+ // build the file's node index, which a Tier-0-only module never needs
+ const importLocals = new Map()
+ for (const record of file.imports) {
+ const local = record.local
+ if (local) {
+ importLocals.set(record.node.start, local.id as SymbolId)
+ }
+ }
+ const exportLocals = new Map()
+ for (const record of file.exports) {
+ const local = record.local
+ if (local) {
+ exportLocals.set(record.node.start, local.id as SymbolId)
+ }
+ }
const body = file.program.body
for (let i = this.header.directiveCount; i < body.length; i++) {
const stmt = body[i]!
@@ -325,10 +342,9 @@ export class ModuleAnalysis {
typeOnly: stmt.importKind === 'type',
phase: stmt.phase,
specifiers: stmt.specifiers.map((spec) => {
- const b = file.bindingOf(spec.local)
return {
range: { start: spec.start, end: spec.end },
- local: (b ? b.id : -1) as SymbolId,
+ local: importLocals.get(spec.start) ?? (-1 as SymbolId),
imported:
spec.type === 'ImportDefaultSpecifier'
? 'default'
@@ -431,11 +447,10 @@ export class ModuleAnalysis {
})
} else if (stmt.declaration !== null) {
for (const id of declaredNames(stmt.declaration)) {
- const b = file.bindingOf(id)
exportEntries.push({
kind: 'local',
name: id.name,
- local: b ? (b.id as SymbolId) : null,
+ local: exportLocals.get(id.start) ?? null,
typeOnly: typeOnly || isTypeDeclaration(stmt.declaration),
range: { start: id.start, end: id.end },
})
@@ -443,14 +458,10 @@ export class ModuleAnalysis {
} else {
for (const spec of stmt.specifiers) {
const exported = moduleExportNameOf(spec.exported)
- const b =
- spec.local.type === 'Identifier'
- ? (file.referenceOf(spec.local)?.binding ?? null)
- : null
exportEntries.push({
kind: 'local',
name: exported,
- local: b ? (b.id as SymbolId) : null,
+ local: exportLocals.get(spec.start) ?? null,
typeOnly: typeOnly || spec.exportKind === 'type',
range: { start: spec.start, end: spec.end },
})
@@ -465,20 +476,18 @@ export class ModuleAnalysis {
d.type === 'ClassDeclaration') &&
d.id !== null
) {
- const b = file.bindingOf(d.id)
exportEntries.push({
kind: 'local',
name: 'default',
- local: b ? (b.id as SymbolId) : null,
+ local: exportLocals.get(stmt.start) ?? null,
typeOnly: false,
range: { start: stmt.start, end: stmt.end },
})
} else if (d.type === 'Identifier') {
- const b = file.referenceOf(d)?.binding ?? null
exportEntries.push({
kind: 'local',
name: 'default',
- local: b ? (b.id as SymbolId) : null,
+ local: exportLocals.get(stmt.start) ?? null,
typeOnly: false,
range: { start: stmt.start, end: stmt.end },
})
diff --git a/packages/router-utils/src/compiler/cache.ts b/packages/router-utils/src/compiler/cache.ts
index 0c0f5fed75..6bec7f39df 100644
--- a/packages/router-utils/src/compiler/cache.ts
+++ b/packages/router-utils/src/compiler/cache.ts
@@ -285,15 +285,14 @@ export class DependencyLedger {
src: FingerprintSource,
fileSetCurrent = false,
): Promise {
+ const checks: Array> = []
for (const fp of this.entries) {
if (fileSetCurrent && fp.kind === 'resolution') {
continue
}
- if (!(await fingerprintCurrent(fp, src))) {
- return false
- }
+ checks.push(fingerprintCurrent(fp, src))
}
- return true
+ return (await Promise.all(checks)).every((ok) => ok)
}
/** The plan is published: its rows replace the key's previous plan's. */
diff --git a/packages/router-utils/src/compiler/identity.ts b/packages/router-utils/src/compiler/identity.ts
index 2cdf1cd1fc..b73e55cc5a 100644
--- a/packages/router-utils/src/compiler/identity.ts
+++ b/packages/router-utils/src/compiler/identity.ts
@@ -214,16 +214,54 @@ export function canonicalJson(value: unknown): string {
if (value === null || typeof value !== 'object') {
return JSON.stringify(value)
}
+ let out: string
if (Array.isArray(value)) {
- return `[${value.map(canonicalJson).join(',')}]`
+ out = '['
+ for (let i = 0; i < value.length; i++) {
+ if (i > 0) {
+ out += ','
+ }
+ out += canonicalJson(value[i])
+ }
+ return out + ']'
}
- const keys = Object.keys(value as Record).sort()
- return `{${keys
- .map(
- (k) =>
- `${JSON.stringify(k)}:${canonicalJson((value as Record)[k])}`,
- )
- .join(',')}}`
+ const record = value as Record
+ const keys = Object.keys(record)
+ if (keys.length > 1) {
+ keys.sort()
+ }
+ out = '{'
+ for (let i = 0; i < keys.length; i++) {
+ if (i > 0) {
+ out += ','
+ }
+ out += JSON.stringify(keys[i]) + ':' + canonicalJson(record[keys[i]!])
+ }
+ return out + '}'
+}
+
+/**
+ * The key of an import edge: unique per (environment, importer, specifier,
+ * form, origin) within a request. The digest of the record
+ * `{ env, layer, importer, rawSpecifier, form, origin? }` (`digestOf`, and
+ * `origin` only when it is not `'source'`), spelled out: one key per edge
+ * per output on every compile, so the canonical JSON is written directly
+ * instead of through `canonicalJson` (same bytes, same digest).
+ */
+export function edgeKeyOf(
+ env: string,
+ layer: string | null | undefined,
+ importer: string,
+ rawSpecifier: string,
+ form: string,
+ origin = 'source',
+): string {
+ // keys in sorted order: env, form, importer, layer, origin, rawSpecifier
+ const json =
+ `{"env":${JSON.stringify(env)},"form":${JSON.stringify(form)},"importer":${JSON.stringify(importer)},"layer":${layer == null ? 'null' : JSON.stringify(layer)},` +
+ (origin === 'source' ? '' : `"origin":${JSON.stringify(origin)},`) +
+ `"rawSpecifier":${JSON.stringify(rawSpecifier)}}`
+ return hash('sha1', json, 'base64url').slice(0, 22)
}
export function planKeyString(key: PlanKey): string {
diff --git a/packages/router-utils/src/compiler/output-builder.ts b/packages/router-utils/src/compiler/output-builder.ts
index e8b571236a..1d13f0458c 100644
--- a/packages/router-utils/src/compiler/output-builder.ts
+++ b/packages/router-utils/src/compiler/output-builder.ts
@@ -12,6 +12,9 @@
* character; whitespace never starts one. The scan is syntax-agnostic.
*/
+/** Runs in the bundler's process only: the environment is read once, not per copy. */
+const DEV = process.env.NODE_ENV !== 'production'
+
const SPACE = 0
const WORD = 1
const PUNCT = 2
@@ -204,7 +207,7 @@ export class OutputBuilder {
copy(start: number, end: number, edits?: ReadonlyArray): void {
let pos = start
if (edits !== undefined) {
- if (process.env.NODE_ENV !== 'production') {
+ if (DEV) {
assertSortedEdits(edits, start, end)
}
for (const ed of edits) {
diff --git a/packages/router-utils/src/compiler/pipeline.ts b/packages/router-utils/src/compiler/pipeline.ts
index 5b3cb7a6d4..13414e5161 100644
--- a/packages/router-utils/src/compiler/pipeline.ts
+++ b/packages/router-utils/src/compiler/pipeline.ts
@@ -7,6 +7,7 @@
import {
deriveId,
digestOf,
+ edgeKeyOf,
identify,
isCommonJsPath,
revision,
@@ -21,7 +22,13 @@ import { ExportResolver } from './resolve'
import { fingerprintSourceFor, resolveForAnalysis } from './host'
import { registerCompilerStats } from './stats'
import type { CacheEntry } from './cache'
-import type { EmitPlan, EnvInfo, FallbackRecord, PlanContext } from './plan'
+import type {
+ EmitPlan,
+ EnvInfo,
+ FallbackRecord,
+ OutputRecipe,
+ PlanContext,
+} from './plan'
import type { PlanKey, SourceIdentity } from './identity'
import type { OutputId } from './rewrite'
import type { Diagnostic } from './diagnostics'
@@ -116,16 +123,15 @@ export async function compileModule(
// resolutions validated at the current file-set version stand (no resolution cache, no re-asking either)
const fileSetVersion = caches.fileSetVersion
const resolutionsCurrent = hit.resolvedAt === fileSetVersion
- let valid = true
+ // every fingerprint at once: the checks are independent reads
+ const checks: Array> = []
for (const fp of hit.plan.dependencies) {
if (resolutionsCurrent && fp.kind === 'resolution') {
continue
}
- if (!(await fingerprintCurrent(fp, src))) {
- valid = false
- break
- }
+ checks.push(fingerprintCurrent(fp, src))
}
+ const valid = (await Promise.all(checks)).every((ok) => ok)
if (valid && cache.lookup(key, rev) === hit) {
hit.resolvedAt = fileSetVersion
cache.counters.planHits++
@@ -164,6 +170,7 @@ export async function compileModule(
cache,
ledger,
verdictMemo,
+ rev,
)
const record = { key, revision: rev, generation, promise, ledger }
cache.inFlight.set(keyString, record)
@@ -199,11 +206,6 @@ export async function compileModule(
}
const plan = result.plan
plan.dependencies = [...ledger.entries]
- plan.fingerprint = digestOf({
- rev,
- configKey: input.config.configKey,
- deps: plan.dependencies,
- })
const src = fingerprintSourceFor(caches, host, input.config.configKey)
const snapshotOk = await ledger.validateSnapshot(
src,
@@ -280,7 +282,11 @@ function emitFrom(
const plan = entry.plan
const recipe = plan.outputs[requested]
const emitted = recipe
- ? emitRecipe(recipe, new SourceText(input.code), input.wantMap)
+ ? requested === 'env' && isIdentityRecipe(recipe, input.code)
+ ? // every host passes an unchanged env module through (no map read):
+ // no builder run and no word-level map over the whole text
+ { code: input.code, map: null }
+ : emitRecipe(recipe, new SourceText(input.code), input.wantMap)
: { code: 'export {}\n', map: null }
return {
code: emitted.code,
@@ -290,11 +296,39 @@ function emitFrom(
facts: plan.facts,
diagnostics: plan.diagnostics,
dependencies: dependencyPaths(plan, caches),
- fingerprint: plan.fingerprint,
+ // only the Rsbuild session reads it, once per compile: computed on demand
+ get fingerprint() {
+ return planFingerprint(entry)
+ },
output: requested,
}
}
+/** Whether emitting `recipe` over `code` yields `code` itself (slices covering the whole text, ending in a line feed). */
+function isIdentityRecipe(recipe: OutputRecipe, code: string): boolean {
+ let at = 0
+ for (const seg of recipe.segments) {
+ if (seg.k !== 'slice' || seg.start !== at) {
+ return false
+ }
+ at = seg.end
+ }
+ return at === code.length && code.charCodeAt(code.length - 1) === 10
+}
+
+/** The plan's fingerprint (its text, configuration and dependencies), computed on first use. */
+function planFingerprint(entry: CacheEntry): string {
+ const plan = entry.plan
+ if (plan.fingerprint === '') {
+ plan.fingerprint = digestOf({
+ rev: entry.revision,
+ configKey: entry.key.configKey,
+ deps: plan.dependencies,
+ })
+ }
+ return plan.fingerprint
+}
+
/** The files a plan read (a known package is recognised by its specifier, never read). */
function dependencyPaths(
plan: EmitPlan,
@@ -331,6 +365,7 @@ async function runAttempt(
cache: PlanCache,
ledger: DependencyLedger,
verdictMemo: Map>,
+ rev: string,
): Promise {
const env = input.env
const key = ledger.key
@@ -443,7 +478,6 @@ async function runAttempt(
// ---- B: planning (sync) ------------------------------------------------------
const tier1Needed = planners.some((p) => p.tier === 1)
let emitPlan: EmitPlan
- const rev = revision(input.code)
if (!tier1Needed) {
emitPlan = tier0Plan(t0, identity, env, key, rev)
} else {
@@ -548,13 +582,7 @@ function tier0Plan(
{ k: 'slice', start: 0, end: code.length },
]
const edgeKey = (raw: string, form: string) =>
- digestOf({
- env: env.name,
- layer: env.layer ?? null,
- importer: identity.canonicalId,
- rawSpecifier: raw,
- form,
- })
+ edgeKeyOf(env.name, env.layer, identity.canonicalId, raw, form)
const mark = (range: { start: number; end: number }, k: string) => {
for (let i = 0; i < segments.length; i++) {
const s = segments[i]!
diff --git a/packages/router-utils/src/compiler/plan.ts b/packages/router-utils/src/compiler/plan.ts
index ce49ab3dce..d41061005d 100644
--- a/packages/router-utils/src/compiler/plan.ts
+++ b/packages/router-utils/src/compiler/plan.ts
@@ -6,7 +6,7 @@
* edits, per-output edges) and `freeze()` renders everything into a plain
* `EmitPlan` from which no Yuku object is reachable.
*/
-import { deriveId, digestOf, siblingSpecifier } from './identity'
+import { deriveId, edgeKeyOf, siblingSpecifier } from './identity'
import { NameAllocator } from './names'
import { computePlacement } from './reach'
import { RewriteSet } from './rewrite'
@@ -324,14 +324,14 @@ export class AnalysisPlan implements PlanContext {
form: EdgeForm,
origin: EdgeProvenance['origin'] = 'source',
): string {
- return digestOf({
- env: this.env.name,
- layer: this.env.layer ?? null,
- importer: this.identity.canonicalId,
+ return edgeKeyOf(
+ this.env.name,
+ this.env.layer,
+ this.identity.canonicalId,
rawSpecifier,
form,
- ...(origin === 'source' ? {} : { origin }),
- })
+ origin,
+ )
}
/** Renames every reference and declaration of `sym` (context-aware; exports keep their public names). */
diff --git a/packages/router-utils/src/compiler/purity.ts b/packages/router-utils/src/compiler/purity.ts
index a2ee22fb56..fef4022f60 100644
--- a/packages/router-utils/src/compiler/purity.ts
+++ b/packages/router-utils/src/compiler/purity.ts
@@ -170,7 +170,7 @@ export function isPureExpression(
}
if (node.type === 'CallExpression') {
const callee = unwrapExpression(node.callee)
- if (callee.type === 'Identifier') {
+ if (callee.type === 'Identifier' && ctx.noSideEffectCallees.size > 0) {
const b = ctx.file.referenceOf(callee)?.binding
if (b && ctx.noSideEffectCallees.has(b.id)) {
return node.arguments.every((a) =>
diff --git a/packages/router-utils/src/compiler/reach.ts b/packages/router-utils/src/compiler/reach.ts
index 957aa972eb..1066033e6b 100644
--- a/packages/router-utils/src/compiler/reach.ts
+++ b/packages/router-utils/src/compiler/reach.ts
@@ -645,21 +645,29 @@ export function computePlacement(input: PlacementInput): Placement {
}
// ---- reads per (cluster, output) -----------------------------------------
- const clusterReadsCache = new Map>()
- const clusterChildren = new Map>()
+ interface ClusterReads {
+ syms: Set
+ children: Set
+ }
+ // per output (or `*` for the dead walk), an array indexed by cluster id
+ const clusterReadsCache = new Map>()
function clusterReads(
c: ClusterId,
o: OutputId,
live: boolean,
- ): { syms: Set; children: Set } {
- const key = `${c}|${live ? o : '*'}`
- let syms = clusterReadsCache.get(key)
- let children = clusterChildren.get(key)
- if (syms && children) {
- return { syms, children }
+ ): ClusterReads {
+ const outputKey = live ? o : '*'
+ let byCluster = clusterReadsCache.get(outputKey)
+ if (!byCluster) {
+ byCluster = []
+ clusterReadsCache.set(outputKey, byCluster)
}
- syms = new Set()
- children = new Set()
+ const hit = byCluster[c]
+ if (hit) {
+ return hit
+ }
+ const syms = new Set()
+ const children = new Set()
const cl = clusters[c]!
for (const r of cl.regions) {
const reads = memberReads(r, o, live)
@@ -679,9 +687,9 @@ export function computePlacement(input: PlacementInput): Placement {
children.add(clusterOfRegion.get(ch)!)
}
}
- clusterReadsCache.set(key, syms)
- clusterChildren.set(key, children)
- return { syms, children }
+ const reads: ClusterReads = { syms, children }
+ byCluster[c] = reads
+ return reads
}
/**
* The output whose tree decides the live reads of cluster `c` reached from
diff --git a/packages/router-utils/src/compiler/strings.ts b/packages/router-utils/src/compiler/strings.ts
index 29fe14bd67..b978fdaf01 100644
--- a/packages/router-utils/src/compiler/strings.ts
+++ b/packages/router-utils/src/compiler/strings.ts
@@ -4,8 +4,14 @@
* only LF in glue, and the escaper keeps every other line terminator out.
*/
+/** A character `stringLiteral` escapes (the common specifier or name has none). */
+const NEEDS_ESCAPE = /[\\\n\r\u2028\u2029'"]/
+
/** Prints a JavaScript string literal with the given quote. */
export function stringLiteral(value: string, quote: "'" | '"' = "'"): string {
+ if (!NEEDS_ESCAPE.test(value)) {
+ return quote + value + quote
+ }
let out = quote
for (let i = 0; i < value.length; i++) {
const c = value.charCodeAt(i)
diff --git a/packages/router-utils/src/compiler/symbols.ts b/packages/router-utils/src/compiler/symbols.ts
index b53c72885d..cc75ae1011 100644
--- a/packages/router-utils/src/compiler/symbols.ts
+++ b/packages/router-utils/src/compiler/symbols.ts
@@ -7,6 +7,9 @@
import { BindingFlags } from 'yuku'
import type { Binding, SourceFile } from 'yuku'
+/** Runs in the bundler's process only: the environment is read once, not per binding. */
+const DEV = process.env.NODE_ENV !== 'production'
+
export type SymbolId = number & { __sym: true }
export type SymbolKind = 'source' | 'generated' | 'ambient' | 'synthetic'
@@ -50,11 +53,7 @@ export class SymbolTable {
}
ofBinding(b: Binding): SymbolId {
- if (
- process.env.NODE_ENV !== 'production' &&
- this.file &&
- b.file !== this.file
- ) {
+ if (DEV && this.file && b.file !== this.file) {
throw new Error('SymbolTable.ofBinding: binding of another file')
}
return b.id as SymbolId
diff --git a/packages/router-utils/tests/identity.test.ts b/packages/router-utils/tests/identity.test.ts
index f2f12c99ba..8dec00b7b8 100644
--- a/packages/router-utils/tests/identity.test.ts
+++ b/packages/router-utils/tests/identity.test.ts
@@ -1,6 +1,8 @@
import { describe, expect, it } from 'vitest'
import {
deriveId,
+ digestOf,
+ edgeKeyOf,
identify,
langFromId,
revision,
@@ -126,4 +128,29 @@ describe('identity', () => {
expect(revision('a')).not.toBe(revision('b'))
expect(revision('a')).toHaveLength(22)
})
+
+ it('spells an edge key as the digest of its record', () => {
+ const cases: Array<
+ [string, string | null | undefined, string, string, string, string?]
+ > = [
+ ['client', null, '/app/a.tsx', './b', 'static'],
+ ['client', undefined, '/app/a.tsx', './b', 'static'],
+ ['ssr', 'rsc', '/app/a.tsx?flavour=b', '@scope/pkg', 'dynamic'],
+ ['client', null, '/app/a.tsx', './it\'s "quoted" \u2028', 'bare'],
+ ['client', null, '/app/a.tsx', './b', 'reexport', 'runtime'],
+ ['client', 'x', '/app/a.tsx', './b', 'static', 'forwarded'],
+ ]
+ for (const [env, layer, importer, rawSpecifier, form, origin] of cases) {
+ expect(edgeKeyOf(env, layer, importer, rawSpecifier, form, origin)).toBe(
+ digestOf({
+ env,
+ layer: layer ?? null,
+ importer,
+ rawSpecifier,
+ form,
+ ...(origin === undefined || origin === 'source' ? {} : { origin }),
+ }),
+ )
+ }
+ })
})
diff --git a/packages/start-plugin-core/src/import-protection/utils.ts b/packages/start-plugin-core/src/import-protection/utils.ts
index 6987ad6aa5..d796a2d6f0 100644
--- a/packages/start-plugin-core/src/import-protection/utils.ts
+++ b/packages/start-plugin-core/src/import-protection/utils.ts
@@ -31,13 +31,25 @@ function stripQueryAndHash(id: string): string {
* Strip Vite query/hash parameters and normalize the path in one step.
*
* Results are memoized because the same module IDs are processed many
- * times across resolveId, transform, and trace-building hooks.
+ * times across resolveId, transform, and trace-building hooks (a miss costs
+ * ~0.3 µs, a hit ~0.03 µs). Two generations bound the memo: when the current
+ * one is full, it becomes the previous one and a fresh one starts, so at
+ * most `2 × NORMALIZE_MEMO_LIMIT` ids are held and the ids in use stay hot;
+ * ids of deleted files age out.
*/
-const normalizeFilePathCache = new Map()
+const NORMALIZE_MEMO_LIMIT = 4096
+let normalizeFilePathCache = new Map()
+let previousNormalizeFilePathCache = new Map()
export function normalizeFilePath(id: string): string {
let result = normalizeFilePathCache.get(id)
if (result === undefined) {
- result = normalizePath(stripQueryAndHash(id))
+ result =
+ previousNormalizeFilePathCache.get(id) ??
+ normalizePath(stripQueryAndHash(id))
+ if (normalizeFilePathCache.size >= NORMALIZE_MEMO_LIMIT) {
+ previousNormalizeFilePathCache = normalizeFilePathCache
+ normalizeFilePathCache = new Map()
+ }
normalizeFilePathCache.set(id, result)
}
return result
diff --git a/packages/start-plugin-core/tests/import-protection-core/path-normalization.test.ts b/packages/start-plugin-core/tests/import-protection-core/path-normalization.test.ts
new file mode 100644
index 0000000000..a88e2d6d53
--- /dev/null
+++ b/packages/start-plugin-core/tests/import-protection-core/path-normalization.test.ts
@@ -0,0 +1,42 @@
+/**
+ * `normalizeFilePath` of import protection: the query is stripped and the
+ * path normalised, and the memo stays correct past its bound (it holds two
+ * generations of ids; a session that sees many distinct ids keeps getting
+ * the right answer, for an id seen long ago as much as for a new one).
+ */
+import { describe, expect, test } from 'vitest'
+import {
+ normalizeFilePath,
+ stripQueryAndHash,
+} from '../../src/import-protection/utils'
+import { normalizePath } from '../../src/utils'
+
+describe('normalizeFilePath', () => {
+ test('strips the query and normalises the path', () => {
+ expect(
+ normalizeFilePath(
+ '/app/src/./routes/../routes/a.tsx?tsr-split=component',
+ ),
+ ).toBe('/app/src/routes/a.tsx')
+ expect(normalizeFilePath('/app/src/project #2/a.ts')).toBe(
+ '/app/src/project #2/a.ts',
+ )
+ })
+
+ test('answers correctly for many more distinct ids than the memo holds', () => {
+ const ids: Array = []
+ for (let i = 0; i < 20_000; i++) {
+ ids.push(
+ `/app/src/section${i % 31}/../section${i % 31}/item-${i}.tsx?tsr-split=${i % 3}`,
+ )
+ }
+ const expected = ids.map((id) => normalizePath(stripQueryAndHash(id)))
+ // first sight, then again after every other id was seen (the memo rolled over several times)
+ for (let i = 0; i < ids.length; i++) {
+ expect(normalizeFilePath(ids[i]!)).toBe(expected[i])
+ }
+ for (let i = 0; i < ids.length; i++) {
+ expect(normalizeFilePath(ids[i]!)).toBe(expected[i])
+ }
+ })
+})
diff --git a/packages/start-plugin-core/tests/integration/rsbuild/first-server-fn-import.test.ts b/packages/start-plugin-core/tests/integration/rsbuild/first-server-fn-import.test.ts
new file mode 100644
index 0000000000..5fa90590dd
--- /dev/null
+++ b/packages/start-plugin-core/tests/integration/rsbuild/first-server-fn-import.test.ts
@@ -0,0 +1,158 @@
+/**
+ * A route module's first server fn on Start's Rsbuild dev server: an edit
+ * that adds `createServerFn` to a code-split route whose page is open shows
+ * on the page as a hot update (SPEC J3.1, J3.3, G10). Found by Lane F's
+ * soak replay. Known bug, upstream (Rspack's HMR runtime, KNOWN-BUGS
+ * "Upstream issues"): when the modules the route's component chunk gained
+ * land in a new async chunk (Rspack's default `defaultVendors` cache group
+ * on Start's `chunks: 'async'` client), the hot update of the already
+ * loaded component chunk re-executes the route's component module, which
+ * requires them before anything loads the new chunk (`Cannot read
+ * properties of undefined (reading 'call')`; web-infra-dev/rspack#11324,
+ * #11922). A plain Rsbuild app fails the same way. A loader-only edit works
+ * (the loader chunk loads only when the loader runs), and so does the same
+ * component edit when the client entry already imports a server fn (the
+ * runtime is in the entry chunk: no new chunk).
+ */
+import { describe, expect, onTestFinished, test } from 'vitest'
+import { pluginReact } from '@rsbuild/plugin-react'
+import { openPage } from '@tanstack/compiler-test-utils/browser'
+import { waitForOutcome } from '@tanstack/compiler-test-utils/outcome'
+import {
+ reportedErrors,
+ startRsbuildStartDevServer,
+} from '../../rsbuild-dev-server'
+import { reactApp } from '../hydrate-dev-edits'
+import { loaderRoute } from '../route-edits-after-tree-rebuild'
+
+const timeout = 90_000
+
+/** The loader route with a server fn the loader calls; its component shows the result. */
+const serverFnInLoader = (
+ token: string,
+) => `import { createFileRoute } from '@tanstack/react-router'
+import { createServerFn } from '@tanstack/react-start'
+const getToken = createServerFn().handler(() => '${token}')
+export const Route = createFileRoute('/with-loader')({
+ loader: async () => ({ probe: await getToken() }),
+ component: Page,
+})
+function Page() {
+ const data = Route.useLoaderData()
+ return {data.probe}
+}
+`
+
+/** The loader route with a server fn its component calls once mounted, showing the result. */
+const serverFnInComponent = (
+ token: string,
+) => `import { createFileRoute } from '@tanstack/react-router'
+import { createServerFn } from '@tanstack/react-start'
+import { useEffect, useState } from 'react'
+const getToken = createServerFn().handler(() => '${token}')
+export const Route = createFileRoute('/with-loader')({
+ loader: async () => ({ probe: '${token}' }),
+ component: Page,
+})
+function Page() {
+ const data = Route.useLoaderData()
+ const [token, setToken] = useState('')
+ useEffect(() => {
+ getToken().then(setToken)
+ }, [])
+ return (
+
+
{data.probe}
+
{token}
+
+ )
+}
+`
+
+/** A module of the client entry that already uses the server fn runtime. */
+const warmup = `import { createServerFn } from '@tanstack/react-start'
+export const ping = createServerFn().handler(() => 'pong')
+`
+
+/**
+ * Opens the loader route's page, edits the route so that its `target`
+ * calls a server fn, and checks the page shows the server fn's result,
+ * without a reload or an error. `warm`: the client entry already imports
+ * a server fn (the runtime is in the client bundle before the edit).
+ */
+async function editInFirstServerFn(options: {
+ target: 'loader' | 'component'
+ warm?: boolean
+}) {
+ const server = await startRsbuildStartDevServer({
+ hmr: true,
+ browser: true,
+ // Start serves the pages, not an HTML template.
+ config: { tools: { htmlPlugin: false }, plugins: [pluginReact()] },
+ files: {
+ ...reactApp,
+ 'src/routes/index.tsx': `import { createFileRoute } from '@tanstack/react-router'
+export const Route = createFileRoute('/')({ component: () => index
})
+`,
+ 'src/routes/with-loader.tsx': loaderRoute('probe-v0'),
+ ...(options.warm && {
+ 'src/warmup.ts': warmup,
+ 'harness/client-entry.ts': `import '../src/warmup'\n${reactApp['harness/client-entry.ts']}`,
+ }),
+ },
+ })
+ expect(reportedErrors(server)).toEqual([])
+ const page = await openPage(`${server.url}/with-loader`, {
+ registerCleanup: onTestFinished,
+ })
+ await page.waitForText('hydrated', { selector: '[data-testid=hydrated]' })
+ await page.waitForText('probe-v0', { selector: '[data-probe=loader]' })
+
+ const route =
+ options.target === 'loader' ? serverFnInLoader : serverFnInComponent
+ await server.edit({ 'src/routes/with-loader.tsx': route('probe-v1') })
+ expect(reportedErrors(server)).toEqual([])
+ const shows =
+ options.target === 'loader'
+ ? '[data-probe=loader]'
+ : '[data-probe=server-fn]'
+ // The outcome: the page shows the result; an error of the page ends the
+ // wait at once (SPEC G10).
+ const shown = () =>
+ page.page.evaluate(
+ (selector: string) => document.querySelector(selector)?.textContent ?? '',
+ shows,
+ )
+ await waitForOutcome({
+ applied: async () => (await shown()).includes('probe-v1'),
+ problems: () => page.errors,
+ timeout: 20_000,
+ describe: async () => `the page shows ${JSON.stringify(await page.text())}`,
+ })
+ expect(page.errors).toEqual([])
+ expect(page.loads()).toBe(1)
+}
+
+describe('Rsbuild dev: a route module gains its first server fn', () => {
+ test(
+ 'an edit that adds a server fn to the loader shows its result on the open page',
+ () => editInFirstServerFn({ target: 'loader' }),
+ timeout,
+ )
+
+ // Known bug (upstream, see the module comment): the component chunk's
+ // hot update throws, the route renders nothing.
+ test.fails(
+ 'an edit that adds a server fn to the component shows its result on the open page',
+ () => editInFirstServerFn({ target: 'component' }),
+ timeout,
+ )
+
+ // The control: with the server fn runtime in the entry chunk, the same
+ // component edit adds no chunk and shows.
+ test(
+ 'the component edit shows when the client entry already imports a server fn',
+ () => editInFirstServerFn({ target: 'component', warm: true }),
+ timeout,
+ )
+})