{"record":{"id":"5c8e572a916be686","repo":"sveltejs/svelte","slug":"can-t-migrate-to-rune-because-there","errorCode":null,"errorMessage":"can't migrate `${...}` to `$${rune}` because there's a variable named ${rune}.\n     Rename the variable and try again or migrate by hand.","messagePattern":"can't migrate `(.+?)` to `\\$(.+?)` because there's a variable named (.+?)\\.\n     Rename the variable and try again or migrate by hand\\.","errorType":"exception","errorClass":"MigrationError","httpStatus":null,"severity":"warning","filePath":"packages/svelte/src/compiler/migrate/index.js","lineNumber":717,"sourceCode":"\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tstate.props_insertion_point = /** @type {number} */ (declarator.end);\n\t\t\t\t}\n\n\t\t\t\tstate.str.update(start, end, '');\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t/**\n\t\t\t * @param {\"state\"|\"derived\"} rune\n\t\t\t */\n\t\t\tfunction check_rune_binding(rune) {\n\t\t\t\tconst has_rune_binding = !!state.scope.get(rune);\n\t\t\t\tif (has_rune_binding) {\n\t\t\t\t\tthrow new MigrationError(\n\t\t\t\t\t\t`can't migrate \\`${state.str.original.substring(/** @type {number} */ (node.start), node.end)}\\` to \\`$${rune}\\` because there's a variable named ${rune}.\\n     Rename the variable and try again or migrate by hand.`\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// state\n\t\t\tif (declarator.init) {\n\t\t\t\tlet { start, end } = /** @type {{ start: number, end: number }} */ (declarator.init);\n\n\t\t\t\tif (declarator.init.type === 'SequenceExpression') {\n\t\t\t\t\twhile (state.str.original[start] !== '(') start -= 1;\n\t\t\t\t\twhile (state.str.original[end - 1] !== ')') end += 1;\n\t\t\t\t}\n\n\t\t\t\tcheck_rune_binding('state');\n\n\t\t\t\tstate.str.prependLeft(start, '$state(');\n\t\t\t\tstate.str.appendRight(end, ')');","sourceCodeStart":699,"sourceCodeEnd":735,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/compiler/migrate/index.js#L699-L735","documentation":"Thrown by the reactive-declaration migrator when converting a `let` declaration into `$state`/`$derived` would collide with an in-scope variable already named `state` or `derived`. Same shape as the props-rune collision (error 1) but for the instance-script state transform. The offending source slice is included in the message via `state.str.original.substring(...)`.","triggerScenarios":"Running `migrate()` on a Svelte 4 component whose `<script>` declares `let state = ...` or `let derived = ...` (or otherwise introduces such a binding into the component scope), and the codemod needs to emit `$state(...)` or `$derived(...)` for another declaration.","commonSituations":"State-machine or XState-style components where `state` is a natural variable name; computed-cache components that name a memo `derived`. The collision is purely lexical — the rune name clashes with the user identifier.","solutions":["Rename the offending `state`/`derived` variable to something else (e.g. `currentState`, `memo`) and re-run migrate.","Migrate the file by hand and apply runes directly without renaming.","Pre-scan for `\\b(state|derived)\\b` declarations before running the codemod."],"exampleFix":"// before\n<script>\n  let state = $: computeInitial();\n  let count = 0;\n  $: doubled = count * 2;\n</script>\n\n// after (rename, then migrate)\n<script>\n  let currentState = computeInitial();\n  let count = $state(0);\n  let doubled = $derived(count * 2);\n</script>","handlingStrategy":"validation","validationCode":"// Detect instance-script state/derived identifier collisions.\nfunction hasStateNameCollision(source) {\n  const script = source.match(/<script\\b[^>]*>([\\s\\S]*?)<\\/script>/)?.[1] ?? '';\n  return /\\b(let|const|var)\\s+(state|derived)\\b/.test(script);\n}\nif (hasStateNameCollision(componentSource)) {\n  // rename before migrating\n}","typeGuard":null,"tryCatchPattern":"const { code } = migrate({ filename, source });\nif (code.startsWith('<!-- @migration-task') && /because there's a variable named (state|derived)/i.test(code)) {\n  // rename the offending variable and re-run, or hand-migrate\n}","preventionTips":["Reserve the names `state` and `derived` for runes in Svelte 5 code.","Avoid `let state`/`let derived` in Svelte 4 components destined for migration.","Run a naming-collision grep before each migrate batch."],"tags":["svelte5","migration","runes","naming-collision","codemod"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}