Skip to content

$derived doesn't update if dependent $state has nested objects #10930

@ampatspell

Description

@ampatspell

Describe the bug

If $state contains nested objects (that passes prototype === object_prototype) and they are mutated outside of $effect (?) and accessed, $derived doesn't return the latest value.

class {
  data = $state<{ position: { x: number }>();
  position = $derived(this.data?.position);
}

Reproduction

sorry for the messy repro. this is the simplest I could get it to.

data.name and name correctly returns 1
data.position.x correctly returns 1
position returns undefined

this happens only if position is accessed outside of an $effect.active() (?).

comment out this.content.map(e => e.position); in models.svelte.ts line 36, and position then correctly returns 1

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE51TwW6jMBD9lZEbqZAi6JkkrXrotXvJrVQqxUPjFdgID5QK-d9XtoEkTXPZU8LMm5k3741HVooKNUtfRybzGlnKnpqGRYy-G_uhe6wIWcS06trCRra6aEVDD5kEEHWjWoIR9gchPzUYKFtVw22c1IpjpWNffrvJpIVXSOASsAOJX1NVEG5scoVliQUFQQi7Bw-Ldfdhp31gEFrQNllmZ3K8wbw4TMBCSUJJkGsg29TYjlsueiiqXOtdxlr1lTFbSBnxnPLYbgs7GB0-tqFHFzMWs0246B36apPz-iuVpwMbpQUJJePh59hjxsDVHr-Vz7HHeDgbPiZWG5NJFrFacVEK5CyltkMTLT6fWXR0_K8-dRsH57ATwPsFo9XWEocdrDTlhJODQnK05O4nuyUOFIQeD1AoqWnB3N3RQejYfW48wAWmvlMNgOWawvuLFftmNTq8eY_m9CxAeqwAGFI_Zo4Y_8e4OcZzO7msuVQj7UWNqptvcGnpmPltJq4mgvvw2G-5hhXHVvTIg2UZf1QeO7O9ApzTodPPNb6UX3ta88EvFry-hZv_WM274q7p9FEui07LT-PiptOHwKHPAPLz9Klu_MFmNP8mCazXL3_2z-l6DaJ0LUFoKFRdoyTkoDqKYJLkKFNeaQUtlhUWpKGTHNvq2zLt86rDXwnWeROg3RDP5LzwzFy-jDfzDzS1mo0NBQAA

Logs

No response

System Info

System:
  OS: macOS 14.2.1
  CPU: (8) arm64 Apple M1
  Memory: 102.20 MB / 8.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
  Yarn: 1.22.21 - ~/.nvm/versions/node/v18.19.0/bin/yarn
  npm: 10.2.3 - ~/.nvm/versions/node/v18.19.0/bin/npm
  pnpm: 8.6.12 - ~/.nvm/versions/node/v18.19.0/bin/pnpm
  bun: 1.0.15 - ~/.bun/bin/bun
  Watchman: 2023.12.04.00 - /opt/homebrew/bin/watchman
Browsers:
  Chrome: 123.0.6312.59
  Safari: 17.2.1
npmPackages:
  rollup: ^4.9.5 => 4.13.0

Severity

blocking all usage of svelte

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions