AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

[Turbopack] Duplicate Class Definitions Across Route Chunks, Breaking `Instanceof`

When multiple route handlers import the same class from an external package, Turbopack inlines a separate copy of the class definition into each route's bundle. This breaks `instanceof` checks across routes because each bundle has a distinct class object, leading to incorrect runtime behavior in production builds.

highConfidence 85%Next.jsReact Server Componentsnext buildTurbopackAffected V15.5.10

Origin Analysis

Turbopack's server chunking strategy does not extract shared modules (or their transitive class definitions) into a common chunk that is imported by all route entries. Instead, each route receives its own bundled copy of the class, violating JavaScript module identity guarantees required by `instanceof`.
1. Clone https://github.com/TrevorBurnham/nextjs-instanceof-api-error 2. Run `npm install && npm run build` 3. Inspect build output: `grep -c "class e extends d.ServiceException" .next/server/app/api/*/route.js` 4. Observe that each route file contains its own class definition.

Fixing Code Block

Edge Case Audit

Externalizing packages increases server startup time because the bundler no longer optimizes or tree-shakes them; the packages must be present in the deployment environment. It may also expose underlying CJS/ESM interop issues. Rollback by removing the packages from `serverExternalPackages` and rebuilding. Official fix should address the root cause in Turbopack's chunking to avoid requiring manual per-package configuration.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state