Skip to content

Commit 917e090

Browse files
fix: accept a snapshot.managedPaths RegExp without a capture group
The schema documents a `snapshot.managedPaths` entry as "a RegExp matching a managed directory", but `FileSystemInfo` read the managed directory out of `match[1]`. A pattern written exactly as documented — with no capture group — therefore reached `getManagedItem(undefined, path)` and failed the build with `TypeError: Cannot read properties of undefined (reading 'length')` from inside the snapshot layer, naming nothing the user wrote. Fall back to the whole match, which is the directory the schema describes.
1 parent 67b8346 commit 917e090

9 files changed

Lines changed: 70 additions & 2 deletions

File tree

‎.changeset/010-persistent-cache.md‎

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+
Fix a crash on a `snapshot.managedPaths` RegExp without a capture group, and speed up cache writes.

‎lib/FileSystemInfo.js‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,16 @@ const getManagedItem = (managedPath, path) => {
12071207
return path.slice(0, i);
12081208
};
12091209

1210+
/**
1211+
* The managed directory a `snapshot.managedPaths` RegExp matched. The schema
1212+
* documents it as matching the directory itself, so a pattern without a capture
1213+
* group names it with the whole match.
1214+
* @param {RegExpExecArray} match a match of a managed path RegExp
1215+
* @returns {string} the managed directory
1216+
*/
1217+
const getManagedPathFromMatch = (match) =>
1218+
match[1] === undefined ? match[0] : match[1];
1219+
12101220
/**
12111221
* Gets resolved timestamp.
12121222
* @template {ContextFileSystemInfoEntry | ContextTimestampAndHash} T
@@ -2663,7 +2673,10 @@ class FileSystemInfo {
26632673
for (const managedPath of this.managedPathsRegExps) {
26642674
const match = managedPath.exec(path);
26652675
if (match) {
2666-
const managedItem = getManagedItem(match[1], path);
2676+
const managedItem = getManagedItem(
2677+
getManagedPathFromMatch(match),
2678+
path
2679+
);
26672680
if (managedItem) {
26682681
classificationCache.set(path, managedItem);
26692682
managedItems.add(managedItem);
@@ -3921,7 +3934,10 @@ class FileSystemInfo {
39213934
for (const managedPath of this.managedPathsRegExps) {
39223935
const match = managedPath.exec(path);
39233936
if (match) {
3924-
const managedItem = getManagedItem(match[1], path);
3937+
const managedItem = getManagedItem(
3938+
getManagedPathFromMatch(match),
3939+
path
3940+
);
39253941
if (managedItem) {
39263942
// construct timestampHash from managed info
39273943
return this.managedItemQueue.add(managedItem, (err, info) => {

‎test/configCases/cache-dependencies/managed-paths-regexp/extra.js‎

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "./loader!package";
2+
3+
it("should compile and run the test in config", () => {});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require("path");
2+
3+
/** @type {import("../../../../").LoaderDefinition} */
4+
module.exports = function (source) {
5+
this.addDependency(path.resolve(__dirname, "node_modules/package/extra.js"));
6+
this.addDependency(path.resolve(__dirname, "extra.js"));
7+
return source;
8+
};

‎test/configCases/cache-dependencies/managed-paths-regexp/node_modules/package/extra.js‎

Whitespace-only changes.

‎test/configCases/cache-dependencies/managed-paths-regexp/node_modules/package/index.js‎

Whitespace-only changes.

‎test/configCases/cache-dependencies/managed-paths-regexp/node_modules/package/package.json‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
// A `managedPaths` RegExp is documented as matching the managed directory
6+
// itself, so one without a capture group names it with the whole match.
7+
const managedPath = new RegExp(
8+
`^${path
9+
.resolve(__dirname, "node_modules")
10+
.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&")}[\\\\/]`
11+
);
12+
13+
/** @type {import("../../../../").Configuration} */
14+
module.exports = {
15+
snapshot: {
16+
managedPaths: [managedPath]
17+
},
18+
plugins: [
19+
(compiler) => {
20+
compiler.hooks.done.tap("Test", ({ compilation }) => {
21+
const fileDeps = [...compilation.fileDependencies];
22+
expect(fileDeps).toContain(
23+
path.resolve(__dirname, "node_modules/package/index.js")
24+
);
25+
expect(fileDeps).toContain(path.resolve(__dirname, "index.js"));
26+
});
27+
}
28+
],
29+
module: {
30+
unsafeCache: false
31+
}
32+
};

0 commit comments

Comments
 (0)