{"id":"5138068311c729b0","repo":"vitejs/vite","slug":"internal-vitejs-plugin-legacy-error-discovered-p","errorCode":null,"errorMessage":"Internal @vitejs/plugin-legacy error: discovered polyfills should exist","messagePattern":"Internal @vitejs/plugin-legacy error: discovered polyfills should exist","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/plugin-legacy/src/index.ts","lineNumber":365,"sourceCode":"        )\n      }\n    },\n  }\n\n  let supportsLegacyOxcMinification: boolean | undefined\n  const legacyGenerateBundlePlugin: Plugin = {\n    name: 'vite:legacy-generate-polyfill-chunk',\n    apply: 'build',\n\n    async generateBundle(opts, bundle) {\n      if (config.build.ssr) {\n        return\n      }\n\n      const chunkFileNameToPolyfills =\n        outputToChunkFileNameToPolyfills.get(opts)\n      if (chunkFileNameToPolyfills == null) {\n        throw new Error(\n          'Internal @vitejs/plugin-legacy error: discovered polyfills should exist',\n        )\n      }\n\n      if (!isLegacyBundle(bundle)) {\n        // Merge discovered modern polyfills to `modernPolyfills`\n        for (const { modern } of chunkFileNameToPolyfills.values()) {\n          modern.forEach((p) => modernPolyfills.add(p))\n        }\n        if (!modernPolyfills.size) {\n          return\n        }\n        if (isDebug) {\n          console.log(\n            `[@vitejs/plugin-legacy] modern polyfills:`,\n            modernPolyfills,\n          )\n        }","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/plugin-legacy/src/index.ts#L347-L383","documentation":"An internal assertion in plugin-legacy's generateBundle hook. The plugin maintains a WeakMap (outputToChunkFileNameToPolyfills) keyed by output options, initialized to null in renderStart (line 457) and populated during renderChunk. If generateBundle fires and the WeakMap entry for the current output options is null or missing, it means the plugin's hooks ran out of order or renderStart was never called for this output. This indicates a bug in plugin-legacy or a plugin ordering conflict with another plugin that manipulates outputs.","triggerScenarios":"Another Vite/Rollup plugin reorders or clones output options objects after renderStart but before generateBundle, causing the WeakMap reference to no longer match. Can also occur with custom multi-output configurations where the output options object identity changes between hooks.","commonSituations":"Using a third-party plugin that modifies rollupOptions.output in its own configResolved or renderStart, or using plugin-legacy with experimental/unstable Vite features that alter the output pipeline. Encountered during version upgrades where plugin-legacy's internal hook ordering assumptions break.","solutions":["Check for other plugins in your config that modify rollupOptions.output and ensure they run after plugin-legacy or don't alter output object identity.","Update @vitejs/plugin-legacy and vite to matching compatible versions.","If reproducible, file a bug report with the minimal config that triggers it — this is an internal invariant violation.","Temporarily simplify your output configuration to a single output object to see if the issue is multi-output related."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// This is an internal error — wrap the build to fail gracefully and collect diagnostics\ntry {\n  await build({ plugins: [legacy()], ...config })\n} catch (e) {\n  if (e.message.includes('Internal @vitejs/plugin-legacy error')) {\n    console.error('plugin-legacy internal error. This is likely a bug.')\n    console.error('Please report at https://github.com/vitejs/vite/issues with your config.')\n    process.exit(1)\n  }\n  throw e\n}","preventionTips":["Keep plugin-legacy and Vite at matching version ranges to avoid internal API mismatches.","Audit custom plugins for any that modify rollupOptions.output after plugin-legacy's configResolved.","If you see this consistently, bisect your plugin list to find which one disrupts hook ordering."],"tags":["plugin-legacy","internal-error","polyfills","plugin-ordering"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}