Skip to content

[Bug]: HMR re-executes a module before the new async chunk its imports moved to is loaded ("Cannot read properties of undefined (reading 'call')") #16288

Open

Description

@Sheraff

Investigated and written with the help of an AI coding agent (Claude); reproduced locally.

System Info

System: Linux 6.12 x64 (Debian 13)
Binaries: Node 26.10.0, npm 12.2.0
Browsers: headless Chromium (Playwright 1.64.1)
npmPackages: @rspack/core 2.2.9 (via @rsbuild/core 2.2.14); also 2.2.7 (via @rsbuild/core 2.2.5)

Details

A module in a loaded async chunk self-accepts. An edit gives it its first node_modules import. splitChunks puts the package in a new async chunk (a sibling of the module's chunk). The hot update re-executes the module before anything loads that chunk:

TypeError: Cannot read properties of undefined (reading 'call')
    at __webpack_require__ (http://127.0.0.1:3618/static/js/index.js:53:29)
    at fn (http://127.0.0.1:3618/static/js/index.js:232:10)
    at ./src/lazy.js (http://127.0.0.1:3618/src_lazy_js.a9f1d732bcbbc318.hot-update.js:5:44)

The page keeps the old code and does not reload. The new vendors chunk is only fetched afterwards.

// src/index.js
document.body.innerHTML = '<div id="out">loading</div>'
import('./lazy.js')
if (import.meta.webpackHot) {
  import.meta.webpackHot.accept()
}
// src/lazy.js, after the edit (the lodash import is new)
import _ from 'lodash'

document.getElementById('out').textContent = 'lazy-v1 ' + typeof _.chunk
if (import.meta.webpackHot) {
  import.meta.webpackHot.accept()
}
  • Fails with Rsbuild's default splitChunks and with splitChunks: { preset: 'none', chunks: 'async' }, with dev.lazyCompilation on or off.
  • Works when cacheGroups: { default: false, defaultVendors: false } (lodash stays in src_lazy_js, no new chunk).

Expected: the page shows lazy-v1 function: the new chunk is loaded before the module re-executes. At minimum, the error should lead to a full reload.

Related: #11324 and #11922 (same mechanism). They were resolved with reloadOnRuntimeErrors in @rspack/plugin-react-refresh, but that matches error.message.startsWith('RuntimeError: factory is undefined') (2.0.2), and this case throws the bare TypeError above, so it does not reload either. webpack/webpack#19494 fixed the analogous case for new initial chunks by loading them during the update.

Suspected fix (untested): before re-executing outdated modules, load the not-yet-installed chunks of the updated chunks' chunk groups (e.g. listed in the hot-update manifest), as __webpack_require__.e would for a fresh import().

Reproduce link

https://github.com/Sheraff/rspack-hmr-new-async-chunk

Reproduce Steps

  1. npm install && npx playwright install chromium (or set CHROME_PATH)
  2. npm run repro: starts the Rsbuild dev server, opens the page, edits src/lazy.js, prints FAILED and the page error
  3. Control: node repro.mjs no-groups prints OK: the page shows lazy-v1 function

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions