Skip to content

fix: wait on dependencies of async bindings#17120

Merged
Rich-Harris merged 5 commits into
mainfrom
bindings-async-fix
Nov 7, 2025
Merged

fix: wait on dependencies of async bindings#17120
Rich-Harris merged 5 commits into
mainfrom
bindings-async-fix

Conversation

@dummdidumm

@dummdidumm dummdidumm commented Nov 6, 2025

Copy link
Copy Markdown
Member

Correctly takes into account blockers for bindings. Also ensures it works for functional bindings.

During that I also noticed a bug in our nodes_end assignment logic, which the added test also regresses against.

Fixes #17092
Fixes #17090

Also discovered a related bug with static props also not awaiting the blockers, which is also fixed in this PR

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

Correctly takes into account blockers for bindings. Also ensures it works for functional bindings.

During that I also noticed a bug in our `nodes_end` assignment logic, which the added test also regresses against.

Fixes #17092
Fixes #17090
@changeset-bot

changeset-bot Bot commented Nov 6, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: bf356a1

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

github-actions Bot commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

Playground

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

@sillvva

sillvva commented Nov 6, 2025

Copy link
Copy Markdown

Testing this. This works:

<script lang="ts">
  await Promise.resolve();
	let search = $state("test");
</script>

<input type="text" bind:value={search} />

This does not:
https://svelte.dev/playground/7e4390edddab4574b9fb36eefde2bd7b?version=pr-17120

<script lang="ts">
  await Promise.resolve();
	let search = $state({ query: "test" });
</script>

<!-- works if you remove the binding below or the await above -->
<!-- also works if you move the await after the `$state()` -->
<!-- Cannot read properties of undefined (reading 'query') -->
<input type="text" bind:value={search.query} />

@dummdidumm dummdidumm marked this pull request as ready for review November 6, 2025 15:43
@dummdidumm

Copy link
Copy Markdown
Member Author

Thank you for the test! Uncovered a dev-only bug with validation (it needed to wait on blockers, too)

@dummdidumm

Copy link
Copy Markdown
Member Author

Found this was wrong for style directives, too

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so glad you figured all this out so i didn't have to 😆

@Rich-Harris Rich-Harris merged commit 68d4f9c into main Nov 7, 2025
17 of 18 checks passed
@Rich-Harris Rich-Harris deleted the bindings-async-fix branch November 7, 2025 22:40
@github-actions github-actions Bot mentioned this pull request Nov 7, 2025
dummdidumm added a commit that referenced this pull request Jun 3, 2026
If a block has a sole component child, we apply an optimization to not need a template. But if that sole child is wrapped in `$.async`, it can happens that the block's effect is assigned the wrong end node.

In the reproduction this happens because we have two components inside a Child which is rendered only after an async block resolves. In `$.append` we have logic to not reassign nodes when the active effect didn't already run, which it has by the time we come across it (because the async work had to resolve first; we added this in #17120 to prevent the opposite, nodes being "rewinded" to an earlier position) and so the second sibling component's `$.append` will not set its end node to the effect. As a result the end node is wrong (too early).

To fix this we assign the nodes in `$.async`. We could also use `compareDocumentPosition` in `$.append` to only ever go forward, but that feels a bit heavier performance-wise.
Rich-Harris added a commit that referenced this pull request Jun 4, 2026
If a block has a sole component child, we apply an optimization to not
need a template. But if that sole child is wrapped in `$.async`, it can
happens that the block's effect is assigned the wrong end node.

In the reproduction this happens because we have two components inside a
Child which is rendered only after an async block resolves. In
`$.append` we have logic to not reassign nodes when the active effect
didn't already run, which it has by the time we come across it (because
the async work had to resolve first; we added this in
#17120 to prevent the opposite,
nodes being "rewinded" to an earlier position) and so the second sibling
component's `$.append` will not set its end node to the effect. As a
result the end node is wrong (too early).

To fix this we assign the nodes in `$.async`. We could also use
`compareDocumentPosition` in `$.append` to only ever go forward, but
that feels a bit heavier performance-wise.

---------

Co-authored-by: paoloricciuti <ricciutipaolo@gmail.com>
Co-authored-by: Rich Harris <rich.harris@vercel.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.

Svelte 5.43.0+: Function bindings and await in a single component cause an error await order breaks bind:

3 participants