{"record":{"id":"1b5b71fb53abf717","repo":"remotion-dev/remotion","slug":"cannot-reorder-effect-target-index-not-found","errorCode":null,"errorMessage":"Cannot reorder effect: target index not-found","messagePattern":"Cannot reorder effect: target index not-found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/studio-codemods/src/effect-operations.ts","lineNumber":600,"sourceCode":"\tlogLine: number;\n}> =>\n\tPromise.resolve().then(() => {\n\t\tconst ast = parseAst(input);\n\t\tconst snapshots = captureEffectSourceSnapshots(ast);\n\t\tconst jsx = getJsx({action: 'reorder', ast, sequenceNodePath});\n\t\tconst attr = findEffectsAttr(jsx.attributes);\n\t\tif (!attr) {\n\t\t\tthrow new Error('Could not find effects on the target JSX element');\n\t\t}\n\n\t\tconst array = getEffectsArray(attr, 'reorder');\n\t\tconst elements = enumerateEffectArrayElements(array);\n\t\tif (fromIndex < 0 || fromIndex >= elements.length) {\n\t\t\tthrow new Error('Cannot reorder effect: source index not-found');\n\t\t}\n\n\t\tif (toIndex < 0 || toIndex >= elements.length) {\n\t\t\tthrow new Error('Cannot reorder effect: target index not-found');\n\t\t}\n\n\t\tconst target = elements[fromIndex];\n\t\tif (target.kind !== 'call') {\n\t\t\tthrow new Error(\n\t\t\t\t'Cannot reorder effect: source effect is not-call-expression',\n\t\t\t);\n\t\t}\n\n\t\ttrackOpeningElement({jsx, snapshots});\n\t\tif (fromIndex !== toIndex) {\n\t\t\tconst [moved] = array.elements.splice(fromIndex, 1);\n\t\t\tarray.elements.splice(toIndex, 0, moved as never);\n\t\t}\n\n\t\treturn {\n\t\t\toutput: getEffectSourceOutput({\n\t\t\t\tast,","sourceCodeStart":582,"sourceCodeEnd":618,"githubUrl":"https://github.com/remotion-dev/remotion/blob/46a3a6bf13ca367376ff18bbb06dae2e87e0d068/packages/studio-codemods/src/effect-operations.ts#L582-L618","documentation":"reorderEffect() moves the effect at fromIndex to toIndex inside the target element's effects={[]} array. After enumerating the array elements, it validates that toIndex points at an existing slot; if toIndex is negative or >= the number of elements, it throws this error before mutating the AST. The array is not modified when this throws.","triggerScenarios":"Calling reorderEffect({fromIndex, toIndex, input, sequenceNodePath}) where toIndex < 0 or toIndex >= elements.length — e.g. toIndex equal to the array length (off-by-one when appending), or toIndex computed from a stale UI list after effects were added/removed in the source file.","commonSituations":"Studio drag-and-drop reorder where the drop index was captured against a cached effect list; source edited manually (an effect removed) so the target index no longer exists; passing toIndex = elements.length intending 'move to end'.","solutions":["Clamp toIndex into [0, elements.length - 1] before calling reorderEffect.","Re-read the current effect list from the source file to refresh indices before reordering.","For moving to the end, pass elements.length - 1, not elements.length.","Verify you resolved the correct sequenceNodePath so you are counting the effects array you actually intend to reorder."],"exampleFix":"// before\nawait reorderEffect({input, fromIndex: 1, toIndex: 5, sequenceNodePath});\n// after\nconst count = getEffectCount(input, sequenceNodePath); // e.g. 3\nawait reorderEffect({input, fromIndex: 1, toIndex: Math.min(5, count - 1), sequenceNodePath});","handlingStrategy":"validation","validationCode":"const count = enumerateEffects(input, sequenceNodePath).length;\nif (!Number.isInteger(toIndex) || toIndex < 0 || toIndex >= count) {\n  throw new RangeError(`toIndex ${toIndex} out of range (0..${count - 1})`);\n}","typeGuard":"const isValidIndex = (i: number, length: number): i is number =>\n  Number.isInteger(i) && i >= 0 && i < length;","tryCatchPattern":"try {\n  await reorderEffect({input, fromIndex, toIndex, sequenceNodePath});\n} catch (e) {\n  if (e instanceof Error && e.message.includes('target index not-found')) {\n    // refresh effect list / clamp index and retry once\n  } else throw e;\n}","preventionTips":["Always derive indices from a freshly parsed effect list, never a cached UI model.","Clamp drop indices to elements.length - 1 in drag-and-drop handlers."],"tags":["index-out-of-range","ast","studio"],"backgroundTag":"index-out-of-range","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"}