{"record":{"id":"461cc17ff2297be8","repo":"sveltejs/svelte","slug":"derived-references-self","errorCode":"derived_references_self","errorMessage":"derived_references_self\nA derived value cannot reference itself recursively\nhttps://svelte.dev/e/derived_references_self","messagePattern":"derived_references_self\nA derived value cannot reference itself recursively\nhttps://svelte\\.dev/e/derived_references_self","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":125,"sourceCode":"\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/component_api_invalid_new`);\n\t}\n}\n\n/**\n * A derived value cannot reference itself recursively\n * @returns {never}\n */\nexport function derived_references_self() {\n\tif (DEV) {\n\t\tconst error = new Error(`derived_references_self\\nA derived value cannot reference itself recursively\\nhttps://svelte.dev/e/derived_references_self`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/derived_references_self`);\n\t}\n}\n\n/**\n * Keyed each block has duplicate key `%value%` at indexes %a% and %b%\n * @param {string} a\n * @param {string} b\n * @param {string | undefined | null} [value]\n * @returns {never}\n */\nexport function each_key_duplicate(a, b, value) {\n\tif (DEV) {\n\t\tconst error = new Error(`each_key_duplicate\\n${value\n\t\t\t? `Keyed each block has duplicate key \\`${value}\\` at indexes ${a} and ${b}`\n\t\t\t: `Keyed each block has duplicate key at indexes ${a} and ${b}`}\\nhttps://svelte.dev/e/each_key_duplicate`);\n\n\t\terror.name = 'Svelte error';\n","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L107-L143","documentation":"Runtime error `derived_references_self`: a `$derived(...)` expression reads its own result during its own computation, which would create an infinite loop (the derived depends on itself). Svelte detects the cycle and throws immediately rather than spinning.","triggerScenarios":"At runtime (DEV) when a `$derived` body references the variable it is being assigned to, e.g. `let x = $derived(x + 1)` or transitively through a function called from the derived that closes over the same binding.","commonSituations":"Refactoring reactive statements (`$: x = x + 1` — itself a smell) into runes; recursive computations written without a base case; derived bodies that read a state variable they then assign via an `$effect`.","solutions":["Break the cycle: introduce a separate `$state` input and a `$derived` that reads it without self-reference.","If you need accumulation, use an `$effect` that reads and writes a `$state` (not a `$derived`).","Rename the derived binding so its initializer does not close over the same name."],"exampleFix":"// before\nlet total = $derived(total + delta);\n\n// after\nlet delta = $state(1);\nlet total = $state(0);\n$effect(() => { total += delta; });","handlingStrategy":"validation","validationCode":"// Detect self-referential $derived expressions.\nfunction selfReferentialDerived(source) {\n  return /\\b(let|const)\\s+(\\w+)\\s*=\\s*\\$derived\\([\\s\\S]*?\\b\\2\\b/.test(source);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never read the binding being defined inside its own `$derived` body.","Use `$effect` writing to `$state` for accumulator-style logic.","Decouple inputs from outputs: a separate `$state` input feeds a pure `$derived`."],"tags":["svelte5","runtime","derived","circular","infinite-loop"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}