Skip to content

Conversation

@FrankFMY
Copy link
Contributor

@FrankFMY FrankFMY commented Feb 5, 2026

Closes #13569

Problem

When using {#each} with bind: on items from a $derived expression that wraps a store (e.g. {#each $derived($store) as item}), mutations through bind: silently fail — neither the input nor any expression reading the derived updates.

This happens because the compiler doesn't detect the store subscription hidden behind the derived binding:

  1. store_to_invalidate is empty — the each block's mutation handler never calls invalidate_store, so the store is never notified of changes
  2. EACH_ITEM_IMMUTABLE is set — items use strict === equality instead of safe_equals, so same-reference object mutations aren't detected
  3. The derived uses strict === equality — even if the store were invalidated, the derived returns the same array reference and === says "no change", preventing propagation to subscribers outside the each block

Fix

Compiler (EachBlock.js):

  • Added find_store_sub_name() helper that walks an AST expression tree to find store subscription bindings
  • Extended uses_store detection: when a dependency is a derived binding, traces through its initial (init expression) to find store subscriptions
  • Extended store_to_invalidate detection: same tracing for derived bindings
  • As a result, EACH_ITEM_IMMUTABLE is correctly not set when the expression depends on a store through a derived

Compiler (VariableDeclaration.js):

  • When generating code for $derived(expr) where expr depends on a store subscription, the compiler now emits $.derived_safe_equal() instead of $.derived(). This uses object-aware equality (safe_equals) rather than strict reference equality (===), so same-reference mutations are detected and propagated to all subscribers (including expressions outside the each block like <p>{items[0].text}</p>)

Test

Added each-bind-derived-store test that verifies:

  • bind:value on items from {#each $derived($store) as item} works
  • Changes propagate both inside the each block and to a <p> tag reading from the derived outside the each block

When the each block expression is a $derived that wraps a store subscription,
the compiler now traces through the derived binding's init expression to detect
the underlying store. This enables proper store invalidation on mutation and
prevents setting EACH_ITEM_IMMUTABLE, ensuring bind: works correctly.

Additionally, store-backed $derived declarations now use safe equality instead
of strict reference equality, so mutations to same-reference objects are detected
and propagated to all subscribers.

Closes sveltejs#13569
@changeset-bot
Copy link

changeset-bot bot commented Feb 5, 2026

🦋 Changeset detected

Latest commit: 3624db9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@FrankFMY FrankFMY requested a deployment to Publish pkg.pr.new (external contributors) February 5, 2026 22:09 — with GitHub Actions Waiting
@svelte-docs-bot
Copy link

@dummdidumm dummdidumm marked this pull request as draft February 5, 2026 22:26
@dummdidumm
Copy link
Member

Thanks - marking as draft because I'm not so sure anymore that we should actually fix this (nor is this a complete fix likely, since it can appear in other situations, too)

The === comparison already returns a boolean, so ?? false is unreachable.
Fixes lint and TSGo CI failures.
@FrankFMY FrankFMY requested a deployment to Publish pkg.pr.new (external contributors) February 5, 2026 22:33 — with GitHub Actions Waiting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#each may not update when passed $derived containing $store

2 participants