sveltejs / svelte

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

Svelte 5: state_unsafe_mutation on trying to mix stores with runes #13840

Closed ceifa closed 3 weeks ago

ceifa commented 3 weeks ago

Describe the bug

It throws an error when set the value of a rune inside the start notifier of a writable store:

state_unsafe_mutation Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without $state in {expression} in App.svelte

Reproduction

https://svelte.dev/playground/hello-world?version=5.0.5#H4sIAAAAAAAACm2OwWrDMAyGX0UThSQQFno1jmG3vcO8g9Mq1ODYxlbaDeN3H-7W7bLj_-nXJxVcraOM4q0gf0ZCcQc4ojdbSy8xPucrOW5sMZn-46fgmTxnFCjzKdnISnvNdoshMRS4JctmcQQV1hQ26L43p8whUad9KztiuGeY4ZDZMPWrcZmGx7AdhvnX1Xe3kNy5G6EfYFZQWk_zQ8Fpp0bqoL2c_p7y8nJUr-RcgHJoyvokp8tRaS-tjzvDYv1ZXI3baf4pKByR6YNRNGl9r18XIK7LNQEAAA==

Logs

No response

System Info

Not relevant

Severity

blocking an upgrade

7nik commented 3 weeks ago

The first read (and, as a result, the store initialization) happens in the markup, so the error is correct.

Either read the store in the script (e.g. simple $store;) or better wrap writing to the stateful variables with untrack.

ceifa commented 3 weeks ago

The first read (and, as a result, the store initialization) happens in the markup, so the error is correct.

You are right. But the error message isn't. If not a bug, at least the error message should be more meaningful in this case.