svelte: `$derived(...)` argument expression is never executed

Describe the bug

What happenes

In my mental model Runes try their best to behave just like “normal variables”. Therefore it was my expectation that console.log("derive x") would be run, because I thought the value would/should be “initialized”.

<script lang="ts">
    let x = $derived(console.log("derive x")) // is never executed ...
    let y = $derived.by(() => {
        console.log("derive y") // is never executed ...
        return 0
    })
</script>

<!-- ... when not "observed somewhere" -->
<!-- {x} -->
<!-- {y} -->

I came across this, when using the workaround described here for solving #13249 manually:

<script lang="ts">
    let _events: Events | undefined = undefined
    let events = $derived.by(() => {
        console.log("Creating new Events")
         _events?.unsubscribe()
         _events = new Events(DEPENDENCY)
        _events.setup()
        return _events
    })
    // here nothing is executed
</script>

What I think should happen

I want to argue that the inner expression/function should always be evaluated, because it goes against how variables and function in JavaScript should behave.

1. An outter function can never stop execution of it’s argument expression:

// execution of `console.log(1)` can never be prevented by `anyNormalJsFunction` in any way
anyNormalJsFunction(console.log(1))

// then i argue this should NEVER stop execution either
$derived(console.log(2)) // this never executed, but should

2. This “wanted behaviour” can be created by simply adding any form of observer

<script lang="ts">
    let x: number = $derived.by(() => {
        console.log("derive x") // now this is executed
        return 0
    })

    $effect(() => {
        x // this
    })
</script>

<!-- ... or this -->
<!-- {x} -->

Subscribing provides the expected behaviour (with the exception of the execution order when having multiple $derives.

3. This is a feature!

I have been using Svelte for 3 years now and I loved it since the start. I also think the new Runes are amazing! I love the Svelte-way of writing applications.

In Svelte 4 I have been using rxjs a lot to create reactive behaviour. I believe that Svelte 5 is missing just a tiny bit of “power” to fully replace my need for rxjs. Such as this issue (or also #13249, which can be worked around in a more intuitive/sensible way).

Conclusion

I sincerely ask for a change in behaviour.

While I believe to understand why this happens, “the value is lazy” (“because it is not observed/subscribed to, and therefore is not generated/initialized”).

I want to ask you to think about the users expectation (especially as explained in section 1). And think about it as a feature, because it allows the user to create reactive behaviour as they might want to.

I understand that the ideom is “no sideeffects”, however this is distant from the reality of needs that a user often has.

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE5WSTU_CQBCG_8o48dBiW6x6IkAC3PXizfVQ26msLLvN7ixKmv53s3wImF44zkfeZ-adabGWihyO3lrUxZpwhLOmwQR524TAbUgxYYLOeFuGzNiVVjY8FVrwcAh5BryUDqQD-qHSM1UQfRu7clCEVEMlUxWHbkUMpfGa72ECt44Lpqg02hlFmTKfkcB9VWAcg9AHwMMJ8PzyegbRhoEN8JLAO7JHWMHS6EteHngVWbmhqo-YB-If8LFnI6l3nIHUG7KOBmBsRRZ4WfAOfmC7C-zsDJt9bKMohskU2tAjuGeMmcB4X7TE3mrIQ9Rd7jK_UnT-X_Qw2l2v-uJK9UWv-vxcfTw8PYwe36QpPGUg6z-PFdUMxnMSHNbBfekg3PZofgJf3jEouaLwDGk6Fbrd0ztMcG0qWUuqcMTWU_fe_QJofQiY0wIAAA==

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE22OzWqEQBCEX6XT5KDgT3IVV8hzZHJYndrdgbFHZkZXEd89DCaQQ45f81VX7XwzFoGbz53lOoIb_pgmLjhuU4KwwEZwwcHNfkiXNgzeTJHsVe4XxTEo7pSoaBFpbUjmsYenC71qeLNAV_2WZTldOtqTpuLgJDiLyrp7pvi0aFWcU12TCSRY4AkrhjlCU1VVZ84jzl7oLdGRK_kt3f4pzf6WvOe5krY-d6et0r6UZXpMzweExEVS7PoAv0BTcCOeD3goprLsfux9PRJxwaPT5maguYl-xvF1fAOg6eimRAEAAA==

Logs

No response

System Info

svelte 5 playground

Severity

blocking an upgrade

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 28 (14 by maintainers)

Most upvoted comments

Can it be that $effect learns in its first run (during first === true) that it does not depend on new Call(group)?

Oh i was just tired yesterday writing on the phone lol. It should be like this

let call = $state(new Call(group));
let first = true;

$effect.pre(()=>{
    if(first){
        first = false;
        group;
        return ()=> call.destroy();
    }
    call = new Call(group);
    return ()=> call.destroy();
});