{"record":{"id":"4d916729401d5433","repo":"remotion-dev/remotion","slug":"could-not-locate-the-import-to-update","errorCode":null,"errorMessage":"Could not locate the import to update","messagePattern":"Could not locate the import to update","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/studio-codemods/src/source-edits.ts","lineNumber":373,"sourceCode":"\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\n\t\t\tconst fullImportStart = recastLocToOffset(input, statement.loc.start);\n\t\t\tconst fullImportEnd = recastLocToOffset(input, statement.loc.end);\n\t\t\tconst fullImport = input.slice(fullImportStart, fullImportEnd);\n\t\t\tedits.push({\n\t\t\t\tend: fullImportEnd,\n\t\t\t\treplacement: renderImportDeclaration({\n\t\t\t\t\tbracketSpacing: getImportBracketSpacing({\n\t\t\t\t\t\tdeclaration: statement,\n\t\t\t\t\t\tinput,\n\t\t\t\t\t\tprettierConfigOverride,\n\t\t\t\t\t}),\n\t\t\t\t\tdeclaration: statement,\n\t\t\t\t\tquote: fullImport.includes('\"') ? '\"' : \"'\",\n\t\t\t\t\tsemicolon: fullImport.trimEnd().endsWith(';') ? ';' : '',\n\t\t\t\t}),\n\t\t\t\tstart: fullImportStart,","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/remotion-dev/remotion/blob/46a3a6bf13ca367376ff18bbb06dae2e87e0d068/packages/studio-codemods/src/source-edits.ts#L355-L391","documentation":"In getInsertImportSourceEdits, when some added specifiers are not ImportSpecifiers (e.g. ImportDefaultSpecifier or ImportNamespaceSpecifier), the whole import statement must be rewritten, which requires the statement's source location. It throws when statement.loc is missing, typically because the AST node was synthesized or loc data was not preserved.","triggerScenarios":"Adding a default import (e.g. `import React from 'react'` style additions) to an import statement node that has no loc — such as one produced by previous in-memory transforms, a cloned node, or a parse that discarded locations.","commonSituations":"Multi-step codemods that rewrite imports then add default specifiers; third-party transforms stripping loc; running the codemod on programmatically generated code.","solutions":["Parse the source with recast (which records loc) and take the ImportDeclaration directly from that tree.","If the import was created by your own code, serialize it with recast.print and re-parse before further edits.","Avoid cloning import statements; keep original node references to preserve loc.","Catch and fall back to appending a completely new import statement instead of rewriting the existing one."],"exampleFix":"// before: mutated node without loc\naddSpecifiers(mutatedStatement, specifiers);\n// after\nconst ast = parse(input);\nconst stmt = ast.program.body.find(isImportDeclaration);\naddSpecifiers(stmt, specifiers);","handlingStrategy":"type-guard","validationCode":"if (!statement.loc) {\n  throw new Error('ImportDeclaration lacks loc; re-parse before rewriting');\n}","typeGuard":"const isLocatableImport = (s: t.Node): s is t.ImportDeclaration & {loc: NonNullable<t.ImportDeclaration['loc']>} => s.type === 'ImportDeclaration' && s.loc != null;","tryCatchPattern":"try {\n  const edits = getInsertImportSourceEdits({statement, input, addedSpecifiers});\n} catch (err) {\n  // fall back: emit a new import statement edit\n}","preventionTips":["Take import statements directly from the recast-parsed AST","Re-parse printed output after any programmatic import construction","Preserve .loc when copying import nodes","Prefer adding specifiers in one pass before any other node replacement"],"tags":["codemod","import","missing-loc"],"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"}