{"id":"375ae798f17ccd3f","repo":"vitejs/vite","slug":"could-not-find-original-file-for-prefix-index","errorCode":null,"errorMessage":"Could not find original file for ${prefix}${index} in ${chunk.fileName}","messagePattern":"Could not find original file for (.+?)(.+?) in (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/vite/rolldown.config.ts","lineNumber":360,"sourceCode":"\n            // import.meta.url\n            s.overwrite(ss, se + 4, `${prefix}${index}`)\n          }\n        }\n        return s.hasChanged() ? s.toString() : undefined\n      },\n    },\n    renderChunk(code, chunk, outputOptions) {\n      if (!code.includes(prefix)) return\n\n      return code.replace(\n        /__vite_buildTimeImportMetaUrl_(\\d+)/g,\n        (_, index) => {\n          const originalFile = Object.keys(idMap).find(\n            (key) => idMap[key] === +index,\n          )\n          if (!originalFile) {\n            throw new Error(\n              `Could not find original file for ${prefix}${index} in ${chunk.fileName}`,\n            )\n          }\n          const outputFile = path.resolve(outputOptions.dir!, chunk.fileName)\n          const relativePath = path\n            .relative(path.dirname(outputFile), originalFile)\n            .replaceAll('\\\\', '/')\n\n          if (outputOptions.format === 'es') {\n            return `new URL(${JSON.stringify(relativePath)}, import.meta.url)`\n          } else if (outputOptions.format === 'cjs') {\n            return `new URL(${JSON.stringify(\n              relativePath,\n            )}, require('node:url').pathToFileURL(__filename))`\n          } else {\n            throw new Error(`Unsupported output format ${outputOptions.format}`)\n          }\n        },","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/rolldown.config.ts#L342-L378","documentation":"Internal assertion in buildTimeImportMetaUrlPlugin's renderChunk hook. During the transform phase, the plugin replaces each import.meta.url usage with a placeholder token __vite_buildTimeImportMetaUrl_N and records the source file path in an idMap keyed by index. In renderChunk, it looks up the original file by index. If the index is not found in idMap, the placeholder token exists in the output but no corresponding source file was recorded — indicating state was lost or the token was introduced by something other than this plugin's transform.","triggerScenarios":"A plugin running after buildTimeImportMetaUrlPlugin's transform but before its renderChunk introduces or duplicates the __vite_buildTimeImportMetaUrl_ prefix string. Or the module evaluator runs in a context where the transform hook's closure (idMap) is reset between transform and renderChunk (e.g., separate plugin instances).","commonSituations":"Contributing to Vite's codebase and modifying the build pipeline. Encountered when a new plugin or build step introduces string content matching the internal prefix pattern. Not encountered by end users of Vite.","solutions":["Ensure no other plugin emits strings containing the prefix __vite_buildTimeImportMetaUrl_.","Verify that the same plugin instance handles both transform and renderChunk for a given build (not recreated between phases).","If modifying Vite's build config, check that the plugin's closure state persists across the build lifecycle."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Internal build error — wrap and report\ntry {\n  await execBuild()\n} catch (e) {\n  if (e.message.includes('Could not find original file for __vite_buildTimeImportMetaUrl')) {\n    console.error('Build pipeline state error in import.meta.url replacement.')\n    console.error('Check for plugins that duplicate or interfere with the prefix token.')\n  }\n  throw e\n}","preventionTips":["Ensure no plugin emits strings containing the __vite_buildTimeImportMetaUrl_ prefix.","Verify that plugin instances persist across transform and renderChunk phases.","Test import.meta.url-dependent builds after modifying the build plugin chain."],"tags":["rolldown-config","internal-error","import-meta-url","build-pipeline"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}