Start: dev compiler loads whole dependency modules to classify top-level calls such as `z.object()` #8757
Which project does this relate to?#
Start
Describe the bug#
In dev, the Start compiler loads and Babel-parses whole dependency modules to rule out that a top-level call such as const Input = z.object({...}) is a renamed createServerOnlyFn/createClientOnlyFn. On a cold dev server every compiled file in the page waits for those loads, so each compiler transform takes tens of ms of wall time while its own work is under 1 ms. In one of our apps (Cloudflare plugin, ~90 compiled files per environment, zod and Sentry) the average was ~200 ms per transform.
The path on main: in a file with createServerFn, checkDirectCalls is on and isTopLevelDirectCallCandidate accepts any a.b() initializer. resolveCalleeKind → resolveIdentifierKind('z') → resolveImportKind resolves zod, then getModuleInfo → loadModule runs zod.js?v=…&server-fn-module-lookup through the plugin pipeline and ingestModule parses it. That happens once per environment and dependency, but every file that calls into the dependency waits on it.
Reproducer numbers (examples/react/start-basic-cloudflare with 60 server function files, 10 middleware files and 10 isomorphic function files; each server function file has import { z } from 'zod' schemas and a top-level Sentry.getClient() from @sentry/core). Cold dev server, warm node_modules/.vite, first load of /perf in headless Chromium, median of 10 runs:
| client | ssr (workerd) | |
|---|---|---|
| compiler transforms | 97 | 90 |
| average wall time per compiler transform | 50 ms | 1.9 ms |
?server-fn-module-lookup transforms | 4, 976 KiB | 5, 977 KiB |
this.resolve calls from the compiler | 363 | 249 |
| compiler hook time, files transformed one at a time | 92 ms (first file 48 ms, then 0.56 ms per file) | 159 ms (first file 124 ms, then 0.54 ms per file) |
The lookups are the pre-bundled zod.js and @sentry_core.js, the rolldown runtime chunk and the local logger.ts. In the client environment the 60 server function files make 60 transformRequest calls for @sentry_core.js?…&server-fn-module-lookup and 120 for zod.js?…&server-fn-module-lookup while the first of each is still running, and all of them wait ~55 ms for the two to be transformed and parsed. The SSR environment imports modules one at a time, so the same wait (~125 ms) lands on its first file.
Caching resolveIdCached and findExportInModule in dev (both build-only today) does not help: I tried it, and compiler resolves fell from 363 to 184 (client) and 249 to 187 (ssr), but one resolve costs 0.02–0.07 ms here and the first load did not change (1358 ms median before and after, 10 runs each). Anyone who adds a dev cache should know that invalidateModules([]) returns before it clears the resolution caches, that Vite calls hotUpdate with no modules when a file is created, and that deps re-optimization changes the ?v= ids.
Complete minimal reproducer#
https://gist.github.com/shkumbinhasani/9df6bcf65b8f5d982f601fc5472119c4 (scripts that run against examples/react/start-basic-cloudflare in this repo)
Steps to Reproduce the Bug#
- In a
TanStack/routercheckout atb1b111761:pnpm install, thenpnpm nx run-many -t build -p @tanstack/react-start @tanstack/react-router-devtools pnpm --filter tanstack-start-example-basic-cloudflare add zod@4.4.3 @sentry/core@10.32.0andpnpm exec playwright install chromiumSENTRY=1 node <gist>/setup.mjs examples/react/start-basic-cloudflare 60node <gist>/measure.mjs examples/react/start-basic-cloudflare 5for the page load,node <gist>/measure-seq.mjs examples/react/start-basic-cloudflare 5for the files one at a time.LIST_LOOKUPS=1lists the lookups andTIMELINE=1shows when they run.
Expected behavior#
Classifying z.object() should not need zod's code, so dev compile time should not grow with the size of the libraries that server function files call at the top level.
Screenshots or Videos#
No response
Platform#
- Start Version:
@tanstack/react-start1.169.3,@tanstack/start-plugin-core1.171.54 (mainatb1b111761) - OS: macOS 27.0.1, Apple M6
- Browser: Chromium (Playwright 1.63, headless)
- Bundler: Vite 8.3.4,
@cloudflare/vite-plugin1.29.1 - Node: 26.10.0
Additional context#
- For
a.b()whereais not a namespace import,resolveCalleeKindreturns the kind ofaitself. That passes the candidate filter only whenais an env-only factory and the code calls one of its properties (createServerOnlyFn.bind(...)): aRoot/Builderreceiver resolves toBuilder, and a method-chain kind failsisDirectCallCandidateForKind. So the compiler can skip these calls. With that change the reproducer no longer loads zod: 2 lookups (544 KiB) client and 3 (545 KiB) ssr, 50 → 33 ms average per client transform, compiler hook time 92 → 74 ms (client) and 159 → 96 ms (ssr), first load 1355 → 1320 ms. I'll open a PR. - That leaves
import * as z from 'zod'andimport * as Sentry:Ns.b()can reach a renamed factory (export { createServerOnlyFn as serverOnly }), so it has to be resolved. Skipping every target insidenode_moduleswould cover it, but libraries use Start factories (@clerk/tanstack-react-startcallscreateMiddleware()), so a library export can be a factory or a builder. I did not find a safe rule for that. resolveKnownImportKindalready skips roots that don't export the imported name, so its loop overknownRootImportsis not part of the cost.
Written with an AI assistant (Claude Code), which also ran the measurements.
Sheraff(GitHub profile, opens in a new tab) assignedSheraff
Sheraff(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.