Skip to content

About

Minimal reproduction: TanStack Start compiler miscompiles createServerFn().handler() when its argument is a createServerOnlyFn()/createIsomorphicFn() call expression

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

createServerFn().handler() miscompiles a call expression argument

The TanStack Start compiler emits a broken server function when .handler() receives a createServerOnlyFn(...) or a createIsomorphicFn()... call expression instead of an identifier bound to that same call. Both builds succeed. One app then answers every request with 500, and the other answers 200 with no data.

Observed on @tanstack/start-plugin-core 1.171.44, the version @tanstack/react-start 1.168.54 pins.

Run it

npm install
npm run repro

repro.mjs builds each app under apps/, prints the .handler(...) calls it finds in that app's dist/server, starts vite preview, and requests /.

The three apps

Each app is the @tanstack/cli create --blank scaffold with one route whose loader calls one server function. They differ only in src/server-fn.ts, and the implementation they wrap is the same const readOnServer = () => ({ ranOn: 'server' }) in all three.

app .handler() argument GET /
apps/hoisted wrapped, an identifier bound to createServerOnlyFn(readOnServer) 200, {"ranOn":"server"}
apps/inline-server-only createServerOnlyFn(readOnServer), written inline 500
apps/inline-isomorphic createIsomorphicFn().server(readOnServer).client(...), written inline 200, undefined

apps/hoisted and apps/inline-server-only are the same JavaScript. Binding the call to a name is the workaround.

What each build emitted

The server function is compiled twice: once in the split module that runs the implementation, and once in the caller, which should hold an RPC stub. The caller is where the three apps diverge.

apps/hoisted, in dist/server/assets/router-*.js:

createServerFn({ method: "GET" }).handler(createSsrRpc("da38dc3f0228fb3e…"))

apps/inline-server-only, same file:

createServerFn({ method: "GET" }).handler("da38dc3f0228fb3e…")

apps/inline-isomorphic, same file:

createServerFn({ method: "GET" }).handler(readOnServer)

The client bundles are wrong in the same place. apps/inline-server-only ships .handler(() => { throw Error("createServerOnlyFn() functions can only be called on the server!") }) and apps/inline-isomorphic ships .handler(() => ({ ranOn: "client" })), so a client-side call never reaches the server.

The 500

createServerFn().handler() assigns method onto its first argument, which is now a string, so the server function throws while Node evaluates the module that defines it:

TypeError: Cannot create property 'method' on string 'da38dc3f0228fb3ebeaf2c5eeca2817c5f085d88e7c69ee886858ce1ddb0300d'
    at fun.handler (…/dist/server/server.js:237:23)
    at …/dist/server/assets/router-D0ZEiova.js:45:50
    at ModuleJob.run (node:internal/modules/esm/module_job:569:25)
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
    at async node:internal/modules/esm/loader:650:26
    at async Promise.all (index 0)
    at async loadEntries (…/dist/server/server.js:1419:52)

loadEntries imports the route tree, so the throw precedes routing and every route in the app returns 500, not only the one that calls the server function.

vite dev fails the same way in apps/inline-server-only; the id in the message is a base64 string there rather than a hash.

Where it comes from

packages/start-plugin-core/src/start-compiler/compiler.ts collects candidates in one pass, each holding a Babel path, then runs the handlers in a fixed order:

const BuiltInKindHandlerOrder = ['ServerFn', 'Middleware', 'IsomorphicFn', 'ServerOnlyFn', 'ClientOnlyFn']

handleCreateServerFn.ts runs first and replaces .handler()'s first argument with the RPC stub:

handlerFnPath.replaceWith(rpcStub)

handleEnvOnly.ts and handleCreateIsomorphicFn.ts run after that, through paths captured before it. handleEnvOnly.ts reads the argument of the node the path now addresses:

const innerFn = path.node.arguments[0]
path.replaceWith(innerFn)

That node is createSsrRpc("<id>"), so arguments[0] is the id string and the emitted call is .handler("<id>"). handleCreateIsomorphicFn.ts keeps a node from the original tree instead:

const innerFn = envCallInfo.firstArgPath?.node
path.replaceWith(innerFn)

so it overwrites the stub with the raw implementation, and the emitted call is .handler(impl).

Hoisting avoids both because the call then sits at a variable declarator init, which neither handler replaces, and .handler() receives an identifier.

Versions

Installed exactly as pinned in each app's package.json:

package version
@tanstack/react-start 1.168.54
@tanstack/start-plugin-core 1.171.44 (transitive)
@tanstack/react-router 1.170.36
vite 8.3.0
@vitejs/plugin-react 6.1.1
react, react-dom 19.3.0
typescript 7.0.2

tsc --noEmit passes in all three apps.

Run on macOS 26.6.2 (arm64) with Node 26.7.0 and npm 11.19.0 workspaces. Requests were made with fetch from repro.mjs and with curl; no browser was involved.

About

Minimal reproduction: TanStack Start compiler miscompiles createServerFn().handler() when its argument is a createServerOnlyFn()/createIsomorphicFn() call expression

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages