Skip to content

Commit 21af4f5

Browse files
fix(css): terminate a string or url() the tokenizer closed at EOF (#21624)
* test(css): read the equivalence probe's style with no transition in flight A declaration carrying `transition-*` started a CSS transition on the shared probe, so `getComputedStyle` returned a value interpolating away from the previous rule's rather than the one the declaration sets. The walk order is deterministic, so the two arms read different points of the same animation and one fixture was filed as a printer defect it never had. * fix(css): terminate a string or url() the tokenizer closed at EOF CSS Syntax 4.3.5 and 4.3.6 end a string and a url-token at EOF, so their content stops there. The printer re-emitted the token as written and then wrote the enclosing `}`, which the still-open token took into its content: `.a{content:"abc` printed as `.a{content:"abc}`. Write the terminator back so the token ends where it ended for the tokenizer. A terminator that is itself escaped (`"x\\"`) does not close the token, so the check counts trailing backslashes rather than reading the last character. * fix: register ModuleDependencyError as serializable A module that records one could not be written to the persistent cache — "No serializer registered for ModuleDependencyError" — so the pack was invalidated and the module rebuilt on every run. Its structurally identical sibling ModuleDependencyWarning was already registered. * fix: guard ModuleDependencyError against deserialization with no error Deserialization constructs the class with no arguments, so `err.message` threw and every restore of a cached compilation error failed. With the guard the `html/srcset` case restores from the persistent cache and its `test.filter.js` skip can go. * test(html): run the three error cases under the persistent cache suite webpack always rebuilds a module that carries a build error, so the pack is written to on every run. That is expected, and the repo already states it with an `infrastructure-log.js` expectation; these three cases skipped the whole cache suite instead. * fix(css): keep the url() replacement character when closing at EOF A dangling `\` is dropped when a string is closed at EOF (4.3.5 has it contribute nothing) but not when a url is: 4.3.6 reads it as an escape and 4.3.7 ends one at EOF with U+FFFD, so dropping it changed the url's value. Write that code point back instead. * feat(css): resolve `true` / `false` and nested `@custom-media` values Media Queries 5 3 defines `true` and `false` as always- and never-matching, and lets a value name another custom media. Both were classified unsupported and left as written. No `<media-in-parens>` is unconditionally true on every engine, so a constant is folded into the query around it instead of written out: it drops out of an `and` / `or` chain, takes the whole query with it when it decides one, and a list with nothing left prints `not all`. A name standing for another resolves transitively, with a cycle falling back to the old leave-as-written warning. Definitions now hold their classification rather than their tokens — node ids are recycled per top-level rule, so an at-rule's AST cannot outlive the walk that reaches it, and a name may be defined below its use. * feat(html): reconcile the <head> on hot update instead of reloading Any `<head>` edit beyond `<title>` reloaded the page, because replacing `document.head.innerHTML` would tear down the runtime scripts and stylesheet links webpack injects there on load. Applying only the delta between the two authored heads cannot: neither string contains an injected node. A `<script>` in the delta still reloads — one parsed out of a string carries the already-started flag, so inserting it would never run it — as does a removal whose element is no longer in the head. FakeDocument now parses an `innerHTML` fragment (through webpack's own HTML parser) and exposes `children` / `outerHTML`, which the reconciliation walks. * docs: treat a persistent-cache invalidation as a defect, not noise `Pack got invalid because of write to: X` names something that was rebuilt rather than restored — work a user's incremental build redoes every time. The guide had no rule for it, so the reflex was to drop the case from the cache suite with a `test.filter.js`, which removes every cache assertion the case had. Name the diagnostics that point at the real cause, allow exactly the one expected write webpack ships (a module carrying a build error is rebuilt unconditionally), and require any other expectation to carry its reason. `define-plugin-runtime-value-errors` was skipping the cache suite for that one expected write; it states it and runs cached now. The other two opt-outs assert a throw from `webpack()` itself, which the pre-compile runs have nowhere to report — they keep the filter, with the reason written down. * perf(css): stop allocating while capturing a media prelude Capturing walked node arrays, so every level sliced a fresh one and every `@media` in a file that defines a custom media built a tree even with no reference in it. Walk index ranges instead, and check for a reference first — `childAt` reaches the nested blocks without materializing a child list. On a stylesheet with one `@custom-media` and 200 unrelated `@media` rules the capture allocates 1 node instead of 801 and 0 arrays instead of 1604. Emitted bytes and warnings are unchanged on that file and on both custom-media cases. * feat(html): run a <script> added to the <head> instead of reloading An element parsed out of a string carries the already-started flag, so appending it never runs it — the reconciliation reloaded on any <script> in the delta. Building a fresh element from its attributes and body does run it, which is what the reload it replaces would have done. Removing one still reloads: a script that has run cannot be un-run. FakeDocument grew what that walks — attributes off the parsed fragment, an outerHTML serialized from the element's own state rather than the source that spelled it, and a load that fires only for a script that has a src, matching when a browser fetches one. * fix: compare memory-cache etags by value, not by object identity A miss in the memory cache returns `null`, and `null` bails `Cache.hooks.get` — so it is not "ask the next stage", it is "definitively not cached", and the file cache is never consulted. Etags were compared by identity, so any asset whose `Source` is rebuilt each compilation missed on an etag that stringifies identically to the stored one, then recomputed and stored: re-minified every build, and the pack invalidated with it. Three cases already recorded the symptom as unavoidable — "each time returns different OriginalSource … this prevents hit in inmemory cache". Identity still decides a hit first, so a warm hit costs no hashing; only a mismatch falls back to the value the rest of the cache compares etags by. Ten `infrastructure-log.js` expectations go with it, and stand as the regression test: they fail again the moment the pack is written to. * fix: fall through on a memory-cache etag mismatch instead of reporting a miss Comparing the stringified etags was treating the symptom. A lazy etag is interned per source object (`getLazyHashedEtag` keys a WeakMap by it), so equal content reached through a second object is a different etag by construction — and hashing to tell those apart would cost every hit exactly what laziness saves. The defect is that the memory layer answered `null` there. `hooks.get` is an `AsyncSeriesBailHook`, so `null` bails the chain and the file cache — which compares etags by value — is never asked. It also buys the caller nothing: `Cache.get` maps `null` to `undefined` either way. So a mismatch now falls through, and only an identifier nothing was known about records a miss: an entry reached with a different etag still answers its own. Measured on a two-run build of `benchmarkCases/cache-filesystem`: 1029 identity hits out of 2556 gets and zero fall-throughs, so the added stage costs nothing outside the pattern it fixes. * fix: keep a nested custom-media reference, and place a head addition in order Two review findings, both real. `classifyCustomMedia` read a leading `(--name)` as the whole value, so `@custom-media --a (--b) and (min-width: 1px)` resolved to `--b` and dropped the rest. Only a block that *is* the whole value is an alias now; a reference anywhere else makes the value unresolvable, because nothing outside a `@media` prelude scans for one — the same hole was open in `not (--b)` and in a comma segment, and both are closed too. The head reconciliation appended every addition, so an element authored between two others landed after them and changed the cascade. Retained elements are now paired with their live nodes scanning only forward, which places each addition before the element it precedes and turns a reordering the delta cannot express into the reload it needs. FakeDocument's `insertBefore` ignored its reference argument, and parsing into a detached container registered its children document-wide.
1 parent 57177cc commit 21af4f5

57 files changed

Lines changed: 1437 additions & 341 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Let a memory-cache etag mismatch fall through to the file cache instead of reporting a miss.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Resolve `@custom-media` values that are `true` / `false` or name another custom media.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Patch the HTML `<head>` in place on hot update instead of forcing a full reload.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Terminate a CSS string or `url()` closed at EOF when printing, and register `ModuleDependencyError` as serializable.

‎AGENTS.md‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,30 @@ Say what it reported in the PR when the numbers moved.
503503

504504
The `lint` job runs Prettier (`fmt:check`) and cspell (`lint:spellcheck`) across the **whole repo** — Markdown and this guide too, not just `lib/`. Run `yarn fix` before pushing even a docs-only change: an unaligned Markdown table or a word cspell doesn't know fails `lint` on its own. For a new/unusual word, add it to the `words` list in `cspell.json` (or reword); Prettier reformats Markdown tables, so hand-written columns must match its output.
505505

506-
### Register serializable classes
506+
### The persistent cache has to keep working
507507

508-
Persistent caching serializes the module graph, so any new serializable class (a `Module`, `Dependency`, or error subclass, a cached value, …) must call `makeSerializable(...)` — the pattern is used across ~140 files. Run `yarn fix:serializables` to regenerate `internalSerializables`; forgetting silently breaks the persistent cache.
508+
> [!REQUIRED]
509+
510+
Persistent caching is a shipped feature, not a test mode. `ConfigCacheTestCases` re-runs **every** `configCases/` case with `cache.type: "filesystem"` and fails it if the second or third run writes anything back into the pack. The log line is:
511+
512+
```
513+
Pack got invalid because of write to: <identifier>
514+
```
515+
516+
and `<identifier>` is the thing that was **not** restored — it was rebuilt instead. On a user's machine that is work redone on every incremental build, so **treat this as a defect and find the cause**. Do not silence it.
517+
518+
Persistent caching serializes the module graph, so any new serializable class (a `Module`, `Dependency`, or error subclass, a cached value, …) must call `makeSerializable(...)` — the pattern is used across ~140 files — and `yarn fix:serializables` regenerates `internalSerializables`. Forgetting is the most common cause, and it is silent apart from the line above.
519+
520+
The cache suite runs with `infrastructureLogging.debug`, so the log usually names the real cause a few lines earlier. What each one means:
521+
522+
- `No serializer registered for <Class>` — that class never called `makeSerializable(...)`.
523+
- `Skipped not serializable cache item '<key>'` — something reachable from the value cannot be written.
524+
- `Restoring failed for <identifier> from pack: <err>` — it _was_ written, and deserialization threw. Deserialization re-enters the constructor with **no arguments**, so a constructor that dereferences a parameter (`err.message`) must guard (`err ? err.message : ""`).
525+
- Nothing at all — the identifier is not stable between runs, or the module reports that it needs rebuilding.
526+
527+
**Never silence it with `test.filter.js`.** `module.exports = (config) => !config.cache` drops the case from the cache suite entirely, so nothing about that feature is cache-tested any more — including the parts that did work. A new case must pass under both suites.
528+
529+
The one expected write webpack ships today is a module carrying a **build error**: `NormalModule.needBuild` returns true whenever `this.error` is set, because webpack retries errors on every build. A case whose subject _is_ an error therefore invalidates the pack by design, and states so with an `infrastructure-log.js` returning `[/Pack got invalid because of write to/]` when `cache.type === "filesystem"` (~20 cases already do). That is the only mechanism that needs no further justification; any other expectation carries the reason it is not a bug, written next to it — and "it is noise here" is not a reason.
509530

510531
### Performance and memory
511532

‎lib/cache/MemoryCachePlugin.js‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,25 @@ class MemoryCachePlugin {
3030
{ name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY },
3131
(identifier, etag, gotHandlers) => {
3232
const cacheEntry = cache.get(identifier);
33-
if (cacheEntry === null) {
34-
return null;
35-
} else if (cacheEntry !== undefined) {
36-
return cacheEntry.etag === etag ? cacheEntry.data : null;
33+
// A recorded miss: the whole chain was asked for this identifier already.
34+
if (cacheEntry === null) return null;
35+
// Etags are compared by identity — a lazy one is interned per source
36+
// object, so equal content reached through a second object is a
37+
// different etag. Hashing to tell those apart would cost every hit what
38+
// laziness saves, so a mismatch falls through to the next stage instead:
39+
// the file cache compares etags by value and can still answer. Returning
40+
// `null` here would bail the hook and lose that (`Cache.get` maps it to
41+
// `undefined` regardless, so it buys the caller nothing).
42+
if (cacheEntry !== undefined && cacheEntry.etag === etag) {
43+
return cacheEntry.data;
3744
}
3845
gotHandlers.push((result, callback) => {
39-
if (result === undefined) {
40-
cache.set(identifier, null);
41-
} else {
46+
if (result !== undefined) {
4247
cache.set(identifier, { etag, data: result });
48+
} else if (cacheEntry === undefined) {
49+
// Record the miss only for an identifier nothing was known about:
50+
// an entry reached with a different etag still answers its own.
51+
cache.set(identifier, null);
4352
}
4453
return callback();
4554
});

‎lib/cache/MemoryWithGcCachePlugin.js‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,42 @@ class MemoryWithGcCachePlugin {
104104
{ name: PLUGIN_NAME, stage: Cache.STAGE_MEMORY },
105105
(identifier, etag, gotHandlers) => {
106106
const cacheEntry = cache.get(identifier);
107-
if (cacheEntry === null) {
108-
return null;
109-
} else if (cacheEntry !== undefined) {
110-
return cacheEntry.etag === etag ? cacheEntry.data : null;
111-
}
112-
const oldCacheEntry = oldCache.get(identifier);
113-
if (oldCacheEntry !== undefined) {
114-
const cacheEntry = oldCacheEntry.entry;
115-
if (cacheEntry === null) {
116-
oldCache.delete(identifier);
117-
cache.set(identifier, cacheEntry);
118-
return null;
107+
// A recorded miss: the whole chain was asked for this identifier already.
108+
if (cacheEntry === null) return null;
109+
// Etags are compared by identity — a lazy one is interned per source
110+
// object, so equal content reached through a second object is a
111+
// different etag. Hashing to tell those apart would cost every hit what
112+
// laziness saves, so a mismatch falls through to the next stage instead:
113+
// the file cache compares etags by value and can still answer. Returning
114+
// `null` here would bail the hook and lose that (`Cache.get` maps it to
115+
// `undefined` regardless, so it buys the caller nothing).
116+
let known = cacheEntry !== undefined;
117+
if (cacheEntry !== undefined) {
118+
if (cacheEntry.etag === etag) return cacheEntry.data;
119+
} else {
120+
const oldCacheEntry = oldCache.get(identifier);
121+
if (oldCacheEntry !== undefined) {
122+
const entry = oldCacheEntry.entry;
123+
if (entry === null) {
124+
oldCache.delete(identifier);
125+
cache.set(identifier, entry);
126+
return null;
127+
}
128+
known = true;
129+
if (entry.etag === etag) {
130+
oldCache.delete(identifier);
131+
cache.set(identifier, entry);
132+
return entry.data;
133+
}
119134
}
120-
if (cacheEntry.etag !== etag) return null;
121-
oldCache.delete(identifier);
122-
cache.set(identifier, cacheEntry);
123-
return cacheEntry.data;
124135
}
125136
gotHandlers.push((result, callback) => {
126-
if (result === undefined) {
127-
cache.set(identifier, null);
128-
} else {
137+
if (result !== undefined) {
129138
cache.set(identifier, { etag, data: result });
139+
} else if (!known) {
140+
// Record the miss only for an identifier nothing was known about:
141+
// an entry reached with a different etag still answers its own.
142+
cache.set(identifier, null);
130143
}
131144
return callback();
132145
});

0 commit comments

Comments
 (0)