You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an $state that contains an array, if a value computed from it is $inspect-ed and the array is mutated with an operation like splice or shift, the $inspect (and therefore value computation) will be re-triggered for each 'step' of the splice operation as implemented by the browser (move each value up one place, clear the last value, pop it). One of these steps results in undefined being present in the array, which can break code that otherwise wouldn't see an undefined.
I understand this isn't necessarily a bug in Svelte, but rather the culmination of how $inspect functions, the use of proxies for deep reactivity, and how the browser implements these array mutation operations. My problem is that this is unexpected behaviour, and developers (like me!) may spend time stuck on this problem trying to fix their code. Similarly to how adding console.log shouldn't cause code to break, adding $inspect shouldn't cause code to break.
It'd be great to see some kind of runtime warning to help direct users to understand why they're suddenly seeing an error when they use $inspect. I'm not sure what the conditions of this warning should be (computed array which is $inspected and mutated?).
I'd be more than happy to submit a PR if that's required, depending on what the outcome may be.
Describe the bug
Hi,
This issue has been created from some discussion on the Svelte Discord server.
Given an
$state
that contains an array, if a value computed from it is$inspect
-ed and the array is mutated with an operation likesplice
orshift
, the$inspect
(and therefore value computation) will be re-triggered for each 'step' of the splice operation as implemented by the browser (move each value up one place, clear the last value, pop it). One of these steps results inundefined
being present in the array, which can break code that otherwise wouldn't see an undefined.I understand this isn't necessarily a bug in Svelte, but rather the culmination of how
$inspect
functions, the use of proxies for deep reactivity, and how the browser implements these array mutation operations. My problem is that this is unexpected behaviour, and developers (like me!) may spend time stuck on this problem trying to fix their code. Similarly to how addingconsole.log
shouldn't cause code to break, adding$inspect
shouldn't cause code to break.It'd be great to see some kind of runtime warning to help direct users to understand why they're suddenly seeing an error when they use
$inspect
. I'm not sure what the conditions of this warning should be (computed array which is$inspected
and mutated?).I'd be more than happy to submit a PR if that's required, depending on what the outcome may be.
Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE3VUTYvbMBD9K4NZiAOu0m0PhTQJFHYvPbSHQC_xgh17nKirSEYaJ4Tg_1592LG9u70Y2Xrz3rz58C2quEATLXe3SOYnjJbRj7qOkoiutXsxZxSE9t2oRhfuy8oUmte0SWVKVSML4kpCoTEn3JJ9xFxymsPN3ackkOCciwYNrOHBDIDv4T48NVKjZR-T0qGPiufDx5QWC3hSckZwySUBHREagxpIwUmVvLpCFqQyOPPc3nMDlolQTyieDwwynwnzcFY35hgzxuYZmKNqROkkOkZPovZ_saCBpEs32GFG5rWNoziI351Rm9jTyNFF6Vcs_3jUe2MNOiPOVPbApamtYpYE_QsXAvYIRS4ElnA5cmHobnZCY9F1rolb5PUTHbVqDsdAamrBC8xA1ahz17QEclmCkgiqcpAJT9ezXjlrZIkVl1hmbMAVSholkAl1iNNobC-Nkv-VZ6hPOiqpmxOJFx9tJ2X3MkJVSkPstLqhcPl2ZOMipsQrCCqwXq_hnvIbVEquLhcnB89aKx3P7lColD3PJjlSO4kenfuEwwR55XFk-25i-oAPR6RUW9-i6WgEoyx0L_6cwOObAeuF2sAU6nS6-l20lRxv5u4xgS8JfE3g24tnsfh-1uIuhI3b6ECrxbDwclWoEv3q9_Cu3nD7uf39ixnSXB7s4sST-3nribrg1V4vAtteQ39qiOx_RMnC-nxd32wR1pveBhtK03rxJ7Tzgo4yxG3sD8ovLMcyWpJusH1p_wFots5s2wQAAA==
Logs
No response
System Info
`svelte@5.0.0-next.260`
Severity
annoyance
The text was updated successfully, but these errors were encountered: