Skip to content

Commit b6f5803

Browse files
authored
Merge pull request #12207 from webpack/bugfix/cache-without-build-deps
errors in initial cache no longer cause build dependencies to be ignored
2 parents c6582ce + c75a9ef commit b6f5803

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

lib/cache/PackFileCacheStrategy.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,10 +961,8 @@ class PackFileCacheStrategy {
961961
for (const dep of this.newBuildDependencies) {
962962
if (!this.buildDependencies.has(dep)) {
963963
newBuildDependencies.add(dep);
964-
this.buildDependencies.add(dep);
965964
}
966965
}
967-
this.newBuildDependencies.clear();
968966
if (newBuildDependencies.size > 0 || !this.buildSnapshot) {
969967
if (reportProgress) reportProgress(0.5, "resolve build dependencies");
970968
this.logger.debug(
@@ -1093,6 +1091,10 @@ class PackFileCacheStrategy {
10931091
logger: this.logger
10941092
})
10951093
.then(() => {
1094+
for (const dep of newBuildDependencies) {
1095+
this.buildDependencies.add(dep);
1096+
}
1097+
this.newBuildDependencies.clear();
10961098
this.logger.timeEnd(`store pack`);
10971099
this.logger.log(`Stored pack`);
10981100
})

test/BuildDependencies.test.js

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ const cacheDirectory = path.resolve(__dirname, "js/buildDepsCache");
99
const outputDirectory = path.resolve(__dirname, "js/buildDeps");
1010
const inputDirectory = path.resolve(__dirname, "js/buildDepsInput");
1111

12-
const exec = n => {
12+
const exec = (n, options = {}) => {
1313
return new Promise((resolve, reject) => {
1414
const p = child_process.fork(
1515
path.resolve(__dirname, "fixtures/buildDependencies/run.js"),
16-
[n],
17-
{ stdio: ["ignore", "pipe", "inherit", "ipc"] }
16+
[n, JSON.stringify(options)],
17+
{ stdio: ["ignore", "pipe", "pipe", "ipc"] }
1818
);
1919
const chunks = [];
20+
p.stderr.on("data", chunk => chunks.push(chunk));
2021
p.stdout.on("data", chunk => chunks.push(chunk));
2122
p.once("exit", code => {
2223
const stdout = Buffer.concat(chunks).toString("utf-8");
@@ -45,6 +46,15 @@ describe("BuildDependencies", () => {
4546
fs.mkdir(inputDirectory, { recursive: true }, done);
4647
});
4748
it("should capture loader and config dependencies", async () => {
49+
fs.writeFileSync(
50+
path.resolve(inputDirectory, "loader-dependency.js"),
51+
"module.exports = 0;"
52+
);
53+
fs.writeFileSync(
54+
path.resolve(inputDirectory, "config-dependency.js"),
55+
"module.exports = 0;"
56+
);
57+
await exec("0", { invalidBuildDepdencies: true, buildTwice: true });
4858
fs.writeFileSync(
4959
path.resolve(inputDirectory, "loader-dependency.js"),
5060
"module.exports = 1;"
@@ -53,48 +63,52 @@ describe("BuildDependencies", () => {
5363
path.resolve(inputDirectory, "config-dependency.js"),
5464
"module.exports = 1;"
5565
);
56-
await exec("0");
66+
await exec("1");
5767
fs.writeFileSync(
5868
path.resolve(inputDirectory, "loader-dependency.js"),
5969
"module.exports = Date.now();"
6070
);
6171
const now1 = Date.now();
62-
await exec("1");
6372
await exec("2");
73+
await exec("3");
6474
fs.writeFileSync(
6575
path.resolve(inputDirectory, "config-dependency"),
6676
"module.exports = Date.now();"
6777
);
6878
const now2 = Date.now();
69-
await exec("3");
70-
const now3 = Date.now();
7179
await exec("4");
72-
const results = Array.from({ length: 5 }).map((_, i) =>
80+
const now3 = Date.now();
81+
await exec("5");
82+
const results = Array.from({ length: 6 }).map((_, i) =>
7383
require(`./js/buildDeps/${i}/main.js`)
7484
);
7585
for (const r of results) {
7686
expect(typeof r.loader).toBe("number");
7787
expect(typeof r.config).toBe("number");
7888
expect(typeof r.uncached).toBe("number");
7989
}
80-
expect(results[0].loader).toBe(1);
81-
expect(results[0].config).toBe(1);
82-
expect(results[0].uncached).toBe(1);
83-
// 0 -> 1 should be invalidated
84-
expect(results[1].loader).toBeGreaterThan(now1);
90+
expect(results[0].loader).toBe(0);
91+
expect(results[0].config).toBe(0);
92+
expect(results[0].uncached).toBe(0);
93+
// 0 -> 1 should not cache at all because of invalid buildDeps
94+
expect(results[1].loader).toBe(1);
8595
expect(results[1].config).toBe(1);
8696
expect(results[1].uncached).toBe(1);
87-
// 1 -> 2 should stay cached
88-
expect(results[2].loader).toBe(results[1].loader);
97+
// 1 -> 2 should be invalidated
98+
expect(results[2].loader).toBeGreaterThan(now1);
8999
expect(results[2].config).toBe(1);
90100
expect(results[2].uncached).toBe(1);
91-
// 2 -> 3 should be invalidated
92-
expect(results[3].loader).toBeGreaterThan(now2);
93-
expect(results[3].config).toBeGreaterThan(now2);
94-
expect(results[3].uncached).toBe(results[3].config);
95-
// 3 -> 4 should stay cached, but uncacheable module still rebuilds
96-
expect(results[4].loader).toBe(results[3].loader);
97-
expect(results[4].config).toBe(results[3].config);
98-
expect(results[4].uncached).toBeGreaterThan(now3);
101+
// 2 -> 3 should stay cached
102+
expect(results[3].loader).toBe(results[2].loader);
103+
expect(results[3].config).toBe(1);
104+
expect(results[3].uncached).toBe(1);
105+
// 3 -> 4 should be invalidated
106+
expect(results[4].loader).toBeGreaterThan(now2);
107+
expect(results[4].config).toBeGreaterThan(now2);
108+
expect(results[4].uncached).toBe(results[4].config);
109+
// 4 -> 5 should stay cached, but uncacheable module still rebuilds
110+
expect(results[5].loader).toBe(results[4].loader);
111+
expect(results[5].config).toBe(results[4].config);
112+
expect(results[5].uncached).toBeGreaterThan(now3);
99113
}, 100000);
100114
});

test/fixtures/buildDependencies/run.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const value = require("../../js/buildDepsInput/config-dependency");
55

66
process.exitCode = 1;
77

8-
webpack(
8+
const options = JSON.parse(process.argv[3]);
9+
10+
const compiler = webpack(
911
{
1012
mode: "development",
1113
context: __dirname,
@@ -23,14 +25,18 @@ webpack(
2325
)
2426
})
2527
],
28+
infrastructureLogging: {
29+
level: "verbose"
30+
},
2631
cache: {
2732
type: "filesystem",
2833
cacheDirectory: path.resolve(__dirname, "../../js/buildDepsCache"),
2934
buildDependencies: {
3035
config: [
3136
__filename,
3237
path.resolve(__dirname, "../../../node_modules/.yarn-integrity")
33-
]
38+
],
39+
invalid: options.invalidBuildDepdencies ? ["should-fail-resolving"] : []
3440
}
3541
},
3642
snapshot: {
@@ -44,7 +50,20 @@ webpack(
4450
if (stats.hasErrors()) {
4551
return console.log(stats.toString({ all: false, errors: true }));
4652
}
47-
process.exitCode = 0;
48-
console.log("OK");
53+
if (options.buildTwice) {
54+
compiler.run((err, stats) => {
55+
if (err) {
56+
return console.log(err);
57+
}
58+
if (stats.hasErrors()) {
59+
return console.log(stats.toString({ all: false, errors: true }));
60+
}
61+
process.exitCode = 0;
62+
console.log("OK");
63+
});
64+
} else {
65+
process.exitCode = 0;
66+
console.log("OK");
67+
}
4968
}
5069
);

0 commit comments

Comments
 (0)