{"id":"4819d90964871af4","repo":"vitejs/vite","slug":"internal-vitejs-plugin-legacy-error-discovered-p-4819d9","errorCode":null,"errorMessage":"Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist","messagePattern":"Internal @vitejs/plugin-legacy error: discovered polyfills for (.+?) should exist","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/plugin-legacy/src/index.ts","lineNumber":586,"sourceCode":"      if (config.build.ssr) {\n        return null\n      }\n\n      // On first run, initialize the map with sorted chunk file names\n      let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts)\n      if (chunkFileNameToPolyfills == null) {\n        chunkFileNameToPolyfills = new Map()\n        for (const fileName in chunks) {\n          chunkFileNameToPolyfills.set(fileName, {\n            modern: new Set(),\n            legacy: new Set(),\n          })\n        }\n        outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills)\n      }\n      const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)\n      if (polyfillsDiscovered == null) {\n        throw new Error(\n          `Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`,\n        )\n      }\n\n      if (!isLegacyChunk(chunk)) {\n        if (\n          options.modernPolyfills &&\n          !Array.isArray(options.modernPolyfills) &&\n          genModern\n        ) {\n          // analyze and record modern polyfills\n          await detectPolyfills(\n            raw,\n            modernTargets,\n            assumptions,\n            polyfillsDiscovered.modern,\n          )\n        }","sourceCodeStart":568,"sourceCodeEnd":604,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/plugin-legacy/src/index.ts#L568-L604","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Update @vitejs/plugin-legacy and Vite to the latest compatible versions — this may be a fixed bug.","Identify custom plugins or manualChunks configs that alter chunk file names or emit chunks during renderChunk and adjust them.","Simplify manualChunks configuration to test whether the issue is chunk-splitting related.","File a bug report with reproduction if it occurs with a clean config."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Internal error — wrap build and report\ntry {\n  await build({ ... })\n} catch (e) {\n  if (e.message.includes('discovered polyfills for') && e.message.includes('should exist')) {\n    console.error('plugin-legacy chunk mismatch. Check custom chunk-splitting plugins.')\n    process.exit(1)\n  }\n  throw e\n}","preventionTips":["Avoid custom Rollup plugins that emit or rename chunks during renderChunk when using plugin-legacy.","Keep manualChunks configuration simple and deterministic.","Test plugin-legacy builds after upgrading any chunk-splitting-related dependencies."],"tags":["plugin-legacy","internal-error","polyfills","code-splitting"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}