Skip to content

fix: increment signal versions when discarding forks#17577

Merged
Rich-Harris merged 6 commits intomainfrom
init-derived-on-read
Jan 28, 2026
Merged

fix: increment signal versions when discarding forks#17577
Rich-Harris merged 6 commits intomainfrom
init-derived-on-read

Conversation

@Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Jan 28, 2026

Extracts part of #17335 and builds on it. If a derived is read for the first time inside a fork, we don't want to set derived.v (unless it has no dependencies, in which case it can be cached forever), but we do want the derived to correctly update when it's subsequently read outside the fork. The easiest way to achieve that is to always treat a derived as dirty if its value is UNINITIALIZED on read, which is what currently happens on this PR.

Though maybe the correct fix looks more like this? Not 100% sure just yet Edit: yes, it is, added tests that confirm it

diff --git a/packages/svelte/src/internal/client/reactivity/batch.js b/packages/svelte/src/internal/client/reactivity/batch.js
index 134ebe53a2..e2f10f2101 100644
--- a/packages/svelte/src/internal/client/reactivity/batch.js
+++ b/packages/svelte/src/internal/client/reactivity/batch.js
@@ -972,6 +972,10 @@ export function fork(fn) {
 			await settled;
 		},
 		discard: () => {
+			for (var [source] of batch.current) {
+				source.wv = increment_write_version();
+			}
+
 			if (!committed && batches.has(batch)) {
 				batches.delete(batch);
 				batch.discard();

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Rich-Harris and others added 2 commits January 28, 2026 14:49
Co-authored-by: David Roizenman <hmnd@users.noreply.github.com>
@changeset-bot
Copy link

changeset-bot bot commented Jan 28, 2026

🦋 Changeset detected

Latest commit: 1898a0e

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

@github-actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@17577

Rich-Harris added a commit to hmnd/svelte that referenced this pull request Jan 28, 2026
@Rich-Harris Rich-Harris changed the title fix: always update an UNINITIALIZED derived on read fix: increment signal versions when discarding forks Jan 28, 2026
@Rich-Harris Rich-Harris merged commit ece2e83 into main Jan 28, 2026
15 of 19 checks passed
@Rich-Harris Rich-Harris deleted the init-derived-on-read branch January 28, 2026 21:09
@github-actions github-actions bot mentioned this pull request Jan 28, 2026
Rich-Harris added a commit that referenced this pull request Jan 29, 2026
* fix: prevent reactivity loss during fork

fixes #17197, fixes #17304, fixes #17301, fixes #17309

* add samples

* add changeset

* fix var casing in tests

* don't remove reactions during fork

* add sample for derived dep tracking in fork

* fix sample type check error

* set derived.v on first eval in fork

* add sample for derived.v remaining UNINITIALIZED

* lost current_batch import in runtime.js

* Delete how

* Update packages/svelte/src/internal/client/reactivity/deriveds.js

* delete test in favour of #17577

* extract runtime.js changes into separate PR

* alternative approach

* revert

* clear skipped branches when deferring

* fix

* fix

* changeset

* rename test

* update test

* unused test

---------

Co-authored-by: David Roizenman <david@hmnd.io>
Co-authored-by: Paolo Ricciuti <ricciutipaolo@gmail.com>
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
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.

2 participants