Skip to content

fix: prevent infinite loop when HMRing a component with an await - #17380

Merged
Rich-Harris merged 6 commits into
mainfrom
hmr-without-loop
Dec 22, 2025
Merged

Rich-Harris merged 6 commits into
mainfrom
hmr-without-loop

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

Alternative to #17374. I think this is a more correct fix — it makes the $.hmr block effect a no-op when it isn't running as a result of a hot.accept callback, rather than excluding all block effects from re-running after the first await is encountered during traversal.

It also makes the HMR implementation simpler, both in terms of the generated code...

if (import.meta.hot) {
	A = $.hmr(A, () => A[$.HMR].source);

	import.meta.hot.accept((module) => {
		module.default[$.HMR].source = A[$.HMR].source;
		$.set(A[$.HMR].source, module.default[$.HMR].original);
	});
}
if (import.meta.hot) {
	A = $.hmr(A);

	import.meta.hot.accept((module) => {
		A[$.HMR](module.default);
	});
}

...and the dance that needs to happen inside $.hmr.

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

@changeset-bot

changeset-bot Bot commented Dec 20, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 819442e

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
Copy Markdown
Contributor

Playground

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

@ghost

ghost commented Dec 20, 2025

Copy link
Copy Markdown

/ecosystem-ci run vite-plugin-svelte

@svelte-ecoystem-ci

Copy link
Copy Markdown

📝 Ran ecosystem CI on 92fcc14: Open

✅ vite-plugin-svelte

@ghost

ghost commented Dec 20, 2025

Copy link
Copy Markdown

vite-plugin-svelte contains an e2e test for hmr modificactions but the components in there are not using await so this bug wasn't caught there.

Updating the test components there would help if ecosystem-ci was triggered in PRs, otherwise the svelte repo would need hmr tests itself where a devserver is started, files edited and result of hmr is asserted.

v-p-s tests can only be updated after a svelte release so its not ideal for testing changes to the generated hmr code. For this case i think it would be best if the svelte repo itself had an e2e test that edits files in a "kitchensink" app and tests all behaviors for hmr.

@dummdidumm

dummdidumm commented Dec 21, 2025 •

Copy link
Copy Markdown
Member

This wasn't quite the same as before: module.default in A[$.HMR](module.default); is already the HMR-wrapped version, so subsequent updates have one needless block in-between. Furthermore this created one extra block effect each update. I therefore reverted this to the old logic (but using the update function) and adding a bunch of comments so we don't regress again in the future.

Might need another review now but good to go from my side

@Rich-Harris

Copy link
Copy Markdown
Member Author

Ah, good catch. Made a couple of small tweaks — original_component is a bit of a misnomer since we reassign it to the new component function (I get that 'original' here is supposed to mean 'not the wrapper' but it's non-obvious) so I just renamed it to fn. I also hoisted the source(...) call out so that it's clearer that get(current) inside wrapper is always referring to the original source.

Finally, there's no need to update wrapper.original_component (or wrapper.fn) as it's never referenced after update runs

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.

3 participants