Skip to content

Conversation

@JSerFeng
Copy link
Contributor

@JSerFeng JSerFeng commented Dec 8, 2022

Infinite HMR caused by circular dep and hot.invalidate()

Description

Image a module A has dep B, and B also has dep A, if B has hot.invalidate() in it, and B triggers hmr, then it obviously will send vite:invalidate to the server, then B's importers all starts to propagate this update, and B's importer is A, which has B as its dependency again. So it will cause infinite loop.

How to reprod

  • Create a new vite project.
  • Add 2 modules, main and dep, main import some stuff from dep, and dep also import some stuff from main, add import.meta.hot.accept(() => { import.meta.hot.invalidate() }) to the dep.
  • Go to browser and open that page.
  • Save main.ts file

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

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

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@patak-dev
Copy link
Member

@JSerFeng would you add a test case in the HMR playground for this issue?
https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md#extending-the-test-suite

@JSerFeng
Copy link
Contributor Author

JSerFeng commented Dec 9, 2022

@JSerFeng would you add a test case in the HMR playground for this issue? https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md#extending-the-test-suite

Done

import { value } from './child'

if (import.meta.hot) {
import.meta.hot.accept()
Copy link
Member

Choose a reason for hiding this comment

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

This PR does not work when this import.meta.hot.accept() is:

import.meta.hot.accept(() => {
  import.meta.hot.invalidate()
})

I guess we need to concatenate the previous chain (that called import.meta.invalidate) to the current chain (that happened by the import.meta.invalidate call).

@sapphi-red sapphi-red added feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority) labels Dec 19, 2022
@kajweb
Copy link

kajweb commented May 15, 2023

hi, bro. I'm running into a similar problem right now

@JSerFeng
Copy link
Contributor Author

JSerFeng commented May 16, 2023

Sorry, I'm busy in my school lately, and I don't have time to follow up currently, help wanted, or I'll continue some days later

@bluwy
Copy link
Member

bluwy commented May 18, 2024

Since the PR has gone a bit stale, I'll close this for now and track in this new issue #16709. That way it's easier to track if anyone would like to contribute a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vite 4.x Nested barrel imports cause infinite invalidating/hmr in certain conditions.

5 participants