Skip to content

$effect does not run nested inside $derived (with repro) #16594

@mtsgrd

Description

@mtsgrd

Describe the bug

An $effect inside a $derived statement does not run when the derived variable is accessed by two (or more) lines in a template, where the first one "unsubscribes" immediately. The second reference still gets the right return value, it's just that the effect never executed.

This was first reported by users of GitButler, and I managed to distill it down into a reproducible playground example. The setup might not be an ideal way of doing things, but it represents what you might end up with as a code base slowly evolves.

Reproduction

This does not work: https://svelte.dev/playground/719a2a834fd34d02a204ac4b7c65973b?version=5.35.4
This works as expected: https://svelte.dev/playground/719a2a834fd34d02a204ac4b7c65973b?version=5.35.3

Speculation

When the first template immediately updates with toggle = true the dependency on result is removed from the component's render effect, trigger a call to destroy_derived_effects, and its status is set to MAYBE_DIRTY.

When the dependency is re-established is_dirty returns false since the dependencies haven't changed, and the effect is not run. If this were true, it would perhaps explain why $effect.pre is unaffected by the same bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions