Skip to content
Source
Sign inSign up
TanStack/router

Start: dev compiler loads whole dependency modules to classify top-level calls such as `z.object()` #8757

Closedshkumbinhasani(GitHub profile, opens in a new tab) opened this issue 0 comments

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:

clientssr (workerd)
compiler transforms9790
average wall time per compiler transform50 ms1.9 ms
?server-fn-module-lookup transforms4, 976 KiB5, 977 KiB
this.resolve calls from the compiler363249
compiler hook time, files transformed one at a time92 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

  1. In a TanStack/router checkout at b1b111761: pnpm install, then pnpm nx run-many -t build -p @tanstack/react-start @tanstack/react-router-devtools
  2. pnpm --filter tanstack-start-example-basic-cloudflare add zod@4.4.3 @sentry/core@10.32.0 and pnpm exec playwright install chromium
  3. SENTRY=1 node <gist>/setup.mjs examples/react/start-basic-cloudflare 60
  4. node <gist>/measure.mjs examples/react/start-basic-cloudflare 5 for the page load, node <gist>/measure-seq.mjs examples/react/start-basic-cloudflare 5 for the files one at a time. LIST_LOOKUPS=1 lists the lookups and TIMELINE=1 shows 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-start 1.169.3, @tanstack/start-plugin-core 1.171.54 (main at b1b111761)
  • OS: macOS 27.0.1, Apple M6
  • Browser: Chromium (Playwright 1.63, headless)
  • Bundler: Vite 8.3.4, @cloudflare/vite-plugin 1.29.1
  • Node: 26.10.0

Additional context

  • For a.b() where a is not a namespace import, resolveCalleeKind returns the kind of a itself. That passes the candidate filter only when a is an env-only factory and the code calls one of its properties (createServerOnlyFn.bind(...)): a Root/Builder receiver resolves to Builder, and a method-chain kind fails isDirectCallCandidateForKind. 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' and import * as Sentry: Ns.b() can reach a renamed factory (export { createServerOnlyFn as serverOnly }), so it has to be resolved. Skipping every target inside node_modules would cover it, but libraries use Start factories (@clerk/tanstack-react-start calls createMiddleware()), so a library export can be a factory or a builder. I did not find a safe rule for that.
  • resolveKnownImportKind already skips roots that don't export the imported name, so its loop over knownRootImports is not part of the cost.

Written with an AI assistant (Claude Code), which also ran the measurements.

Sign in to join this conversation.