{"record":{"id":"66d76a7252107d3c","repo":"remotion-dev/remotion","slug":"could-not-locate-the-import-prefix-to-update","errorCode":null,"errorMessage":"Could not locate the import prefix to update","messagePattern":"Could not locate the import prefix to update","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/studio-codemods/src/source-edits.ts","lineNumber":357,"sourceCode":"\t\t}\n\n\t\tconst addedSpecifiers = (statement.specifiers ?? []).filter(\n\t\t\t(specifier) => !snapshot.specifiers.includes(specifier),\n\t\t);\n\t\tif (addedSpecifiers.length === 0) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (\n\t\t\taddedSpecifiers.every(\n\t\t\t\t(specifier) => specifier.type === 'ImportDefaultSpecifier',\n\t\t\t) &&\n\t\t\tstatement.loc\n\t\t) {\n\t\t\tconst importStart = recastLocToOffset(input, statement.loc.start);\n\t\t\tconst importPrefix = input.slice(importStart).match(/^import\\s+/)?.[0];\n\t\t\tif (!importPrefix) {\n\t\t\t\tthrow new Error('Could not locate the import prefix to update');\n\t\t\t}\n\n\t\t\tconst offset = importStart + importPrefix.length;\n\t\t\tedits.push({\n\t\t\t\tend: offset,\n\t\t\t\treplacement: `${addedSpecifiers.map(renderImportSpecifier).join(', ')}, `,\n\t\t\t\tstart: offset,\n\t\t\t});\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (\n\t\t\taddedSpecifiers.some((specifier) => specifier.type !== 'ImportSpecifier')\n\t\t) {\n\t\t\tif (!statement.loc) {\n\t\t\t\tthrow new Error('Could not locate the import to update');\n\t\t\t}\n","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/remotion-dev/remotion/blob/46a3a6bf13ca367376ff18bbb06dae2e87e0d068/packages/studio-codemods/src/source-edits.ts#L339-L375","documentation":"getInsertImportSourceEdits adds specifiers to an existing import statement. When the statement is detected as a candidate for prefix insertion (side-effect-free import whose keyword span should be extended), the code slices the source from the statement start and matches /^import\\s+/. If that regex fails to match, the assumed import prefix cannot be located and the library throws rather than corrupting the import.","triggerScenarios":"An import statement whose source text does not literally start with 'import ' at the computed offset — e.g. unusual whitespace/newline right after 'import' that the \\s+ should catch but a preceding BOM, comment, or offset misalignment prevents; or statement.loc pointing at the wrong offset after prior edits.","commonSituations":"Applying edits to already-mutated source where offsets shifted; import statements preceded by comments or odd formatting; files with BOM characters; non-standard code style that confuses the prefix regex.","solutions":["Run this codemod on the freshly parsed, unedited source so statement.loc offsets are accurate.","Normalize the file: remove BOM, ensure the import statement literally begins with 'import'.","Apply all source edits through applySourceEdits in one batch instead of editing the string between computations.","Wrap in try/catch and fall back to inserting a brand-new import statement instead of extending the existing one."],"exampleFix":"// before\n// BOM+comment confuses match\n﻿import {Video} from 'remotion';\n// after: plain statement at line start\nimport {Video} from 'remotion';","handlingStrategy":"try-catch","validationCode":"const prefix = input.slice(recastLocToOffset(input, statement.loc.start)).match(/^import\\s+/);\nif (!prefix) { throw new Error('Import statement not at expected offset'); }","typeGuard":"const isPlainImportAtOffset = (input: string, offset: number) => /^import\\s+/.test(input.slice(offset));","tryCatchPattern":"try {\n  edits.push(...getInsertImportSourceEdits({statement, input, addedSpecifiers}));\n} catch (err) {\n  // append a fresh import statement instead of extending the existing one\n}","preventionTips":["Run codemods on unmodified source so offsets align with statement.loc","Strip BOM characters before parsing","Avoid manual string edits between computing and applying source edits","Ensure the import statement literally starts with the keyword 'import'"],"tags":["codemod","import","source-edit"],"backgroundTag":"resource-not-found","analyzedSha":"46a3a6bf13ca367376ff18bbb06dae2e87e0d068","analyzedAt":"2026-09-18T00:12:20.256Z","contentChangedAt":"2026-09-18T00:12:20.256Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}