{"record":{"id":"fcb8f7970b2f9552","repo":"remotion-dev/remotion","slug":"could-not-locate-the-folder-insertion-target","errorCode":null,"errorMessage":"Could not locate the folder insertion target","messagePattern":"Could not locate the folder insertion target","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/studio-codemods/src/folder-source-edits.ts","lineNumber":196,"sourceCode":"\t\t\t\tthis.traverse(path);\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tinserted.openingElement.name = recast.types.builders.jsxIdentifier(\n\t\t\t\tlocalName,\n\t\t\t) as never;\n\t\t\tconst root = jsxNode.loc\n\t\t\t\t? jsxNode\n\t\t\t\t: jsxNode.children.find(\n\t\t\t\t\t\t(child) =>\n\t\t\t\t\t\t\t(child.type === 'JSXElement' || child.type === 'JSXFragment') &&\n\t\t\t\t\t\t\tchild.loc,\n\t\t\t\t\t);\n\t\t\tif (\n\t\t\t\t!root ||\n\t\t\t\t(root.type !== 'JSXElement' && root.type !== 'JSXFragment')\n\t\t\t) {\n\t\t\t\tthrow new Error('Could not locate the folder insertion target');\n\t\t\t}\n\n\t\t\tinsertionEdit = getInsertionRootSourceEdit({\n\t\t\t\tinput,\n\t\t\t\tinsertion: printInsertedJsx({\n\t\t\t\t\telement: inserted as never,\n\t\t\t\t\tinput,\n\t\t\t\t\tprettierConfigOverride: null,\n\t\t\t\t}),\n\t\t\t\troot: root as never,\n\t\t\t\tnullRoot: null,\n\t\t\t\tprettierConfigOverride: null,\n\t\t\t\tinsertInside: Boolean(jsxNode.loc),\n\t\t\t});\n\t\t\treturn false;\n\t\t},\n\t});\n","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/remotion-dev/remotion/blob/46a3a6bf13ca367376ff18bbb06dae2e87e0d068/packages/studio-codemods/src/folder-source-edits.ts#L178-L214","documentation":"Thrown inside getNewFolderSourceEdits' visitNode callback (folder-source-edits.ts:196). After a `new-folder` codemod has grafted a new, location-less `<Folder>` node into the AST, the codemod needs an anchor node that carries source location (`loc`) to compute the text insertion edit. If neither the visited JSX node nor any of its located children provides a usable JSXElement/JSXFragment root, insertion is impossible and this error is thrown.","triggerScenarios":"A `new-folder` codemod runs on a file where applyCodemod grafted the new Folder into a JSX tree whose nodes have no recast source locations — typically because the enclosing JSX node itself lacks `loc` and no located child exists to anchor against.","commonSituations":"Root files with unusual formatting that confuses recast's location tracking, files where the composition tree is assembled from fragments/variables without positions, or heavily machine-generated code where the new node was inserted into an unlocatable subtree.","solutions":["Reformat/save the Root file so the JSX tree is written normally with stable source positions and retry the folder creation","Add the new <Folder> wrapper manually in the editor around the target compositions","Ensure the compositions live inside a directly-written JSXElement/JSXFragment rather than a spread or variable"],"exampleFix":"// before (unanchored, programmatically assembled tree)\nconst children = [<Composition id=\"A\" />];\nexport const RemotionRoot = () => <Root>{children}</Root>;\n\n// after (statically written, locatable JSX)\nexport const RemotionRoot = () => (\n  <Root>\n    <Folder name=\"My Folder\">\n      <Composition id=\"A\" />\n    </Folder>\n  </Root>\n);","handlingStrategy":"validation","validationCode":"if (!/<(Root|Composition|Folder)[\\s>]/.test(rootFileSource)) {\n  throw new Error('Root file has no locatable JSX elements; add folders manually');\n}","typeGuard":"const hasSourceLocation = (node: {loc: unknown} | null): node is {loc: nonNullable} =>\n  node !== null && node.loc !== null && node.loc !== undefined;","tryCatchPattern":"try {\n  editFolderInSource({codeMod, input});\n} catch (e) {\n  if (e instanceof Error && e.message === 'Could not locate the folder insertion target') {\n    // prompt user to create the <Folder> manually\n  }\n}","preventionTips":["Keep the JSX tree hand-formatted so recast tracks positions","Avoid inserting folders into programmatically assembled subtrees","Save/reformat the file before running Studio struct edits"],"tags":["ast","codemod","jsx","source-edit"],"backgroundTag":"internal-invariant-violation","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"}