Repository navigation
fix(hmr): hash only emitted bytes in code generation result - #14046
stormslowly wants to merge 4 commits into
Conversation
Align `CodeGenerationResult::set_hash` with webpack's `CodeGenerationResults.getHash`, which feeds `source.updateHash(hash)` and never mixes source-map metadata into the digest. In rspack, `Source::hash` for `SourceMapSource` folds in `source_map` (which carries the `sourcesContent` snapshot of the loader-input file). For multi-block source files such as Vue SFCs that means changing one block shifts the codegen hash of every sibling sub-module even when their emitted bytes are identical. The HMR plugin compares those hashes to decide which modules go into a hot-update chunk, so it ships byte-identical modules and downstream HMR flows that key off "this module changed" (e.g. vue-loader's self-accept on the SFC main wrapper) misfire. Switch to `source.buffer()` so the digest only reflects the actually emitted bytes plus `chunk_init_fragments` and `runtime_requirements`, matching webpack semantics.
There was a problem hiding this comment.
Pull request overview
Aligns CodeGenerationResult::set_hash with webpack's behavior by hashing only the emitted source bytes (via source.buffer()) instead of the full Source::hash, which for SourceMapSource also folded source-map metadata (including sourcesContent) into the digest. This prevents HMR from including byte-identical sibling sub-modules (e.g., from Vue SFCs) in hot-update chunks, fixing the downstream bug reported in #11635.
Changes:
- Replace
source.hash(&mut hasher)withsource.buffer().hash(&mut hasher)inCodeGenerationResult::set_hash. - Add an explanatory comment describing the rationale and the HMR implications.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d4bda8697a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // shifts the codegen hash of every sibling sub-module even when their | ||
| // emitted bytes are identical, which causes HMR to ship unchanged | ||
| // modules in hot-update chunks and breaks downstream HMR flows. | ||
| source.buffer().hash(&mut hasher); |
There was a problem hiding this comment.
Preserve inline source-map changes in content hashes
When devtool inlines maps (SourceMapDevToolPlugin's no-filename path appends source_map_json directly into the JS asset), the final emitted .js bytes still change when only the source map changes, e.g. a loader updates sourcesContent while generated module code stays byte-identical. Hashing only source.buffer() here means the JS content hash/version produced from CodeGenerationResult::hash no longer changes for that rebuild, so [contenthash].js can keep the old name despite different inline-map bytes (and the devtool mapped-asset cache can also treat the asset version as unchanged). External maps with their own [contenthash] are fine, but inline source maps need their map payload reflected somewhere in this hash.
Useful? React with 👍 / 👎.
📦 Binary Size-limit
🙈 Size remains the same at 61.99MB |
Merging this PR will not alter performance
Comparing Footnotes
|
Rsdoctor Bundle Diff AnalysisFound 6 projects in monorepo, 0 projects with changes. 📊 Quick Summary
Generated by Rsdoctor GitHub Action |
Code generation hashes shift with the previous commit because we now ignore source-map metadata in the digest. Update stats snapshots to reflect the new hashes; no behavioral change in the test expectations.
Deploying rspack with
|
| Latest commit: |
3651d9f
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://3e4f54d0.rspack-v2.pages.dev |
| Branch Preview URL: | https://fix-hmr-codegen-result-hash.rspack-v2.pages.dev |
Follow-up on the previous commit: keep `Source::hash` (which routes through `CachedSource`'s memoised u64) for non-SourceMapSource entries, and switch to `source.buffer()` only when the contained source is a `SourceMapSource`. That preserves the upstream codegen hashes for every source type that already hashed only the emitted bytes (RawStringSource, OriginalSource, etc.), and removes the CodSpeed regressions in `persistent_cache_restore` and `create_chunk_ids` that the prior unconditional-buffer hash caused. Also restore `chunk_init_fragments` to the digest; it was removed in the previous commit while exploring causes, but is unrelated to the SFC HMR bug and was already in the digest on main. Add unit tests covering the three relevant cases (RawStringSource, SourceMapSource, CachedSource<SourceMapSource>) so future refactors of the helper cannot silently regress the Vue 2 multi-block SFC behavior. Net diff vs main: the helper itself and its tests. Stats snapshots revert to their upstream values because non-SourceMapSource entries keep the same hash output as before.
|
Closing — the framing of this PR ("align with webpack's hash inputs") is factually wrong. webpack-sources |
Summary
Rspack routed through
Source::hash, which forSourceMapSourcealso folds in the wrappedsource_map— including itssourcesContentsnapshot of the loader-input file.For files split into multiple sub-modules (Vue SFCs, anything emitting
?type=...virtual modules), this means changing one block shifts the codegen hash of every sibling sub-module even when their emitted bytes are unchanged. The HMR plugin then ships byte-identical modules in hot-update chunks, and anything keyed off "this module changed" misfires.Source::hash(incl.source_map.sourcesContent) +chunk_init_fragments+runtime_requirementssource.buffer()+chunk_init_fragments+runtime_requirementsSide effect: fixes #11635
Vue 2
<script setup>parent/child HMR breakage is downstream of this divergence — editing a Vue template made the byte-identical script-setup sub-module ride into hot-update.js, which bubbled intoapi.reloadand brokerecord.Ctoralignment. With this patch the script-setup stays out of the update and the HMR runtime takes theapi.rerenderpath instead.Test plan
tests/rspack-test/hotCases/Local repro:
@rsbuild/core@2.0.6+@rsbuild/plugin-vue2@1.1.1Vue 2.7 minimal project, Playwright-driven edit sequence[app, app, child, child, app, child]. Before patch: final child template stays on the previous value. After patch: updates correctly, matching webpack 5.106 +vue-loader@15.11.1.