vitejs/vite · critical · Error

Internal @vitejs/plugin-legacy error: discovered polyfills f

Error message

Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist

What it means

An internal assertion in plugin-legacy's renderChunk hook. After the plugin initializes a Map of chunk file names to polyfill sets (on the first renderChunk call per output), it looks up each chunk by fileName. If a chunk's fileName is not found in the map, it means the chunk appeared in a renderChunk call but was not present in the `chunks` snapshot taken during initialization. This typically indicates non-deterministic chunk ordering or a plugin that emits additional chunks mid-render.

Source

Thrown at packages/plugin-legacy/src/index.ts:586

      if (config.build.ssr) {
        return null
      }

      // On first run, initialize the map with sorted chunk file names
      let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts)
      if (chunkFileNameToPolyfills == null) {
        chunkFileNameToPolyfills = new Map()
        for (const fileName in chunks) {
          chunkFileNameToPolyfills.set(fileName, {
            modern: new Set(),
            legacy: new Set(),
          })
        }
        outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills)
      }
      const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)
      if (polyfillsDiscovered == null) {
        throw new Error(
          `Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`,
        )
      }

      if (!isLegacyChunk(chunk)) {
        if (
          options.modernPolyfills &&
          !Array.isArray(options.modernPolyfills) &&
          genModern
        ) {
          // analyze and record modern polyfills
          await detectPolyfills(
            raw,
            modernTargets,
            assumptions,
            polyfillsDiscovered.modern,
          )
        }

View on GitHub (pinned to 89620f09af)

Solutions

  1. Update @vitejs/plugin-legacy and Vite to the latest compatible versions — this may be a fixed bug.
  2. Identify custom plugins or manualChunks configs that alter chunk file names or emit chunks during renderChunk and adjust them.
  3. Simplify manualChunks configuration to test whether the issue is chunk-splitting related.
  4. File a bug report with reproduction if it occurs with a clean config.
Defensive patterns

Strategy: try-catch

Try / catch

// Internal error — wrap build and report
try {
  await build({ ... })
} catch (e) {
  if (e.message.includes('discovered polyfills for') && e.message.includes('should exist')) {
    console.error('plugin-legacy chunk mismatch. Check custom chunk-splitting plugins.')
    process.exit(1)
  }
  throw e
}

Prevention

When it happens

Trigger: A Rollup plugin dynamically emits or renames chunks between the first renderChunk call and subsequent ones, so the chunk.fileName doesn't match any key in the initial chunks map. Also possible if chunk file names include non-deterministic content (e.g., variable hashes computed by a custom plugin).

Common situations: Using plugins like rollup-plugin-visualizer, manualChunks configurations that split chunks dynamically, or custom plugins that modify chunk.fileName in renderChunk. Encountered with code-splitting heavy apps combined with plugin-legacy.

Related errors


AI-assisted analysis of vitejs/vite@89620f09af (2026-08-03). Data as JSON: /data/errors/4819d90964871af4.json. Report an issue: GitHub.