sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
79.85k stars 4.24k forks source link

Svelte 5 backwards compatibility: Assigning store from props to local variable does not update subscribers #14183

Closed fkling closed 5 days ago

fkling commented 6 days ago

Describe the bug

We have the following situation: Component receives a store via prop a. The component also has a local variable b which is assigned the value of a in response to some event. However when this happens anything that accesses $b does not get updated. I've simplified the situation in the repl linked below and what stood out to me is that b is not initialized as $.mutable_state(). So this might be less about stores and more about b not being detected as mutable. Interestingly it works within a single component.


In our real world example we receive an observable from a SvelteKit page data loader and we assign this observable to another local variable once another promise is done loading.

This code works in Svelte 4.

Reproduction

https://svelte.dev/playground/a6ca4f7f1ed84860b98793a571f9ff4d?version=5.1.11

Logs

No response

System Info

System:
    OS: Linux 6.6 Gentoo Linux
    CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
    Memory: 29.83 GB / 62.75 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.8.1 - ~/.asdf/installs/nodejs/20.8.1/bin/node
    Yarn: 1.22.21 - ~/.asdf/installs/nodejs/20.8.1/bin/yarn
    npm: 10.1.0 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 8.9.2 - ~/.asdf/installs/pnpm/8.9.2/bin/pnpm
  npmPackages:
    svelte: ^5.1.9 => 5.1.9

Severity

blocking an upgrade

trueadm commented 6 days ago

I've simplified the situation in the repl linked below and what stood out to me is that b is not initialized as $.mutable_state(). So this might be less about stores and more about b not being detected as mutable. Interestingly it works within a single component.

What is b in your example?

paoloricciuti commented 6 days ago

This is because currentStore is actually never read so while it's reassigned it's mistakenly never considered a mutable.

paoloricciuti commented 6 days ago

Basically since that is a store you are always reading $currentStore and never currentStore. So we need to fix the logic for that.

fkling commented 6 days ago

@trueadm @paoloricciuti already mentioned it but for completeness: currentStore