Skip to content

Server function RPCs fail with (0 , __vite_ssr_import_N__.x) is not a function after HMR — partial-exports race with plain named imports (vite 8.1.3) #7825

Closed

Description

@alvarogomezj

Which project does this relate to?

Start

Describe the bug

After an HMR invalidation in dev, server function RPC calls (/_serverFn/…) start failing with:

TypeError: (0 , __vite_ssr_import_0__.fetchData) is not a function

where fetchData is a plain named export of an app module (a shared helper imported by several server functions). Once broken, the dev server keeps returning the error until it is restarted.

This looks like the same class of bug as #7285 / #5673, which was closed as fixed by vitejs/vite#22369 (shipped in vite@8.0.12). That fix does resolve the document SSR path — curling the page after an edit renders fine — but the server-function RPC path still races. Notably, #22369 is scoped to re-export chains ("partial-exports race on concurrent imports of in-flight invalidated re-export chains"); here the same race happens with a plain named import, which the fix does not cover. So this likely needs another upstream fix in Vite's module runner, but I'm filing it here first since Start's serverFn RPCs are how it surfaces (and #7285 lived here).

Steps to reproduce

Minimal reproduction (fresh npm install, reproduces reliably):

https://github.com/alvarogomezj/tanstack-start-serverfn-hmr-race

npm install
npm run dev          # terminal 1
npm run reproduce    # terminal 2 — puppeteer opens the page, appends a comment
                     # to a server-fn file, and reports the broken RPC payloads

Or manually: open http://localhost:3100/ (a route whose loader calls 8 server functions in parallel, all of which import a shared helper), then append a comment to src/service1.actions.ts and save. Several of the 8 RPC responses now carry the is not a function error and the page renders ERROR rows. Typically 3–5 of 8 calls fail; the count varies because it's a race.

The setup mirrors a real mid-size app (~20 routes, ~40 server-function modules sharing a fetch-wrapper helper) where this happens on every save of a route/server file. The repro's shared helper uses a 300 ms top-level await to stand in for the evaluation time that a real import subtree takes naturally.

Expected behavior

Server functions keep working after HMR, or the affected environment is fully re-evaluated before serving RPCs.

What seems to happen

Instrumenting the SSR module runner's evaluator (runInlinedModule) shows this order after the edit, with the 8 invalidated ?tss-serverfn-split modules re-evaluating concurrently (one per in-flight RPC):

EVAL-START service1.actions.ts?tss-serverfn-split
EVAL-START service2.actions.ts?tss-serverfn-split
...
EVAL-START helper.ts        <- shared dep starts evaluating (in-flight)
EVAL-START api1.ts
EVAL-END   api1.ts          <- finishes WITHOUT awaiting in-flight helper.ts
EVAL-END   service1.actions.ts?tss-serverfn-split
...
EVAL-END   helper.ts        <- shared dep finishes last

api1.ts completes while its dependency helper.ts is still evaluating, so its imported binding is undefined when the RPC handler runs.

I saw the identical failure shape in the real app for imports from @tanstack/react-start/server too ((0 , __vite_ssr_import_1__.getCookie) is not a function), consistent with the re-export-chain variant of the same race.

For triage, things I ruled out in the real app:

Workaround

A dev-only Vite plugin that restarts the dev server whenever a file in the server-function graph actually changes content (SHA-1 dedupe to ignore editor safe-write saves), then sends a full-reload to clients after warming the new server with a self-fetch (the automatic client reload otherwise races the server warm-up and renders the error into the page). Reliable, but a full restart per server-file edit is a heavy price, and pure Fast Refresh flows lose out.

Platform

  • OS: macOS (Darwin 25.5.0)
  • Node: 24.14.1
  • Browser: Chrome (also headless via puppeteer in the repro script)

Package versions

Package Version
@tanstack/react-start 1.168.27
@tanstack/react-router 1.170.17
vite 8.1.3
@vitejs/plugin-react 6.0.3
react / react-dom 19.2.4

Activity

  1. changed the title [-]Server function RPCs fail with `(0 , __vite_ssr_import_N__.x) is not a function` after HMR — partial-exports race with plain named imports (vite 8.1.3, follow-up to #7285)[/-] [+]Server function RPCs fail with `(0 , __vite_ssr_import_N__.x) is not a function` after HMR — partial-exports race with plain named imports (vite 8.1.3)[/+] on Jul 15, 2026
  2. gonzoblasco commented on Jul 20, 2026

    @gonzoblasco

    I took a look at the Vite fix that resolved #7285 (vitejs/vite#22369) to understand why it doesn't cover this case.

    The Vite fix addresses re-export chains specifically. The root cause was that mod.importers (a monotonic set never cleared by HMR) caused the module runner to mis-classify concurrent callers as "in a cycle" and return a partial, empty mod.exports before export * had populated it. The fix replaced the importer-based heuristics with a callstack-driven forward-graph check (isCircularRequest) that only breaks for real cycles.

    Why plain named imports still race. The isCircularRequest fix works for the export * case because it checks whether anything mod is waiting on appears in the current callstack. But for plain named imports, the race is different: the module runner evaluates the imported module and its dependencies concurrently, and a module can finish evaluation (and its mod.promise resolve) before a shared dependency that it statically imports has finished evaluating. The module runner sees the dependent module as "done" and returns it, but its imported bindings from the shared dependency are still undefined because that shared module's top-level execution hasn't completed yet.

    In the repro's trace:

    EVAL-START helper.ts        <- shared dep starts evaluating (in-flight)
    EVAL-START api1.ts
    EVAL-END   api1.ts          <- finishes WITHOUT awaiting in-flight helper.ts
    EVAL-END   helper.ts        <- shared dep finishes last
    

    api1.ts's import { fetchData } from "./helper" gets a binding that is undefined because helper.ts hasn't finished its top-level await yet. The module runner considers api1.ts "evaluated" and resolves its mod.promise, but the binding from the in-flight dependency is not yet populated.

    This is a different mechanism from the re-export race — it's about the module runner not tracking that a module's "completeness" depends on its statically imported dependencies being fully evaluated, not just having their mod.promise resolved. The fix would likely need to be in Vite's module runner (cachedRequest or the evaluation pipeline in esm-compiled.js) to ensure that a module's mod.promise doesn't resolve until all its static import bindings are fully populated — i.e., until all dependencies in its import graph have finished evaluation, not just the direct dependency's mod.promise.

    Workaround that works today (from the issue): dynamically import the shared helper inside the server function body instead of at the top level. This avoids the race because dynamic imports are awaited per-call rather than evaluated eagerly at module load time.

  3. alvarogomezj commented on Jul 21, 2026

    @alvarogomezj
    Author

    Filed this upstream as vitejs/vite#22999.

    Rationale: instrumenting the module runner's evaluator (runInlinedModule) places the race inside Vite's module runner — a module finishes evaluation (its mod.promise resolves) without awaiting an in-flight statically-imported shared dependency, exactly as @gonzoblasco described above. The #22369 fix only covers the re-export-chain variant, so the plain named-import case needs its own upstream fix.

    One data point I added there for triage: I could not reproduce this with a plain createServerModuleRunner script (concurrent runner.import() calls sharing a top-level-await dependency; tried cold concurrent imports, invalidateAll + evaluatedModules.clear() mid-flight, and real watcher-driven HMR over 20 edit rounds). The reproduction seems to need the module-id pattern Start creates (the same source file imported both directly and under ?tss-serverfn-split, i.e. two module nodes per file), which may be relevant to how Start's plugin interacts with the runner — so keeping this issue open in parallel seems right.

  4. schiller-manuel commented on Aug 20, 2026

    @schiller-manuel
    Collaborator
  5. mlegls commented on Oct 8, 2026

    @mlegls

    Related observation from a macOS TanStack Start development application on 2026-10-07: two reloads after HMR displayed (0 , __vite_ssr_import_2__.getCookie) is not a function at the root loader. Each next cache-bypassing reload reached the signed-in route without restarting services or changing data. The second occurrence followed codegen and viewport emulation. This is the same error shape involving @tanstack/react-start/server, but we have not isolated the partial-exports race or reproduced it in this issue's minimal repository, and this does not prove that cache bypass was necessary. The packaged Node surface is the verification workaround; no production occurrence was observed. Exact package versions at the two encountered revisions were not retained here. Recording this as a related observation, not proof that the closed fix is ineffective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions