Skip to content

Commit 5bc0405

Browse files
fix(copy): never read a file from inside output.path
A pattern whose base contains `output.path` copied the compilation's own output back into itself, one directory deeper on every build: the watch never settled and the output grew without bound. Exclude the output directory from the walk, and do not watch a base that sits inside it.
1 parent 03453eb commit 5bc0405

3 files changed

Lines changed: 165 additions & 15 deletions

File tree

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+
Stop `output.copy` reading files from inside `output.path`.

‎lib/CopyPlugin.js‎

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,35 @@ const notIgnored = () => false;
321321
/** @type {IgnoreMatcher} */
322322
const NOTHING_IGNORED = { file: notIgnored, directory: notIgnored };
323323

324+
/**
325+
* Whether a path is `output.path` or sits below it.
326+
* @callback InOutputPathMatcher
327+
* @param {string} normalizedPath path with `/` separators
328+
* @returns {boolean} true when the path is the output directory or inside it
329+
*/
330+
331+
/**
332+
* @param {string} outputPath `output.path` with `/` separators
333+
* @returns {InOutputPathMatcher} what is inside the output directory
334+
*/
335+
const createInOutputPathMatcher = (outputPath) => {
336+
const inside = `${outputPath}/`;
337+
return (normalizedPath) =>
338+
normalizedPath === outputPath || normalizedPath.startsWith(inside);
339+
};
340+
341+
/**
342+
* @param {InOutputPathMatcher} isInOutputPath what is inside the output directory
343+
* @param {IgnoreMatcher} ignore what the pattern itself does not copy
344+
* @returns {IgnoreMatcher} the same, with the output directory added
345+
*/
346+
const ignoringOutputPath = (isInOutputPath, ignore) => ({
347+
file: (normalizedPath) =>
348+
isInOutputPath(normalizedPath) || ignore.file(normalizedPath),
349+
directory: (normalizedPath) =>
350+
isInOutputPath(normalizedPath) || ignore.directory(normalizedPath)
351+
});
352+
324353
// the tail of a glob naming everything inside a directory, which names the
325354
// directory itself once it is gone
326355
const IGNORED_DIRECTORY_CONTENTS_REGEXP = /\/\*\*(?:\/\*)?$/;
@@ -474,10 +503,14 @@ class CopyPlugin {
474503
caseSensitive: globOptions.caseSensitive,
475504
requireLiteralLeadingDot: globOptions.dot === false
476505
};
477-
const ignore = this._createIgnoreMatcher(
478-
context,
479-
globOptions.ignore,
480-
matchOptions
506+
// a copy reading its own output copies it one directory deeper on every
507+
// build, which invalidates the watch and grows the output without bound
508+
const isInOutputPath = createInOutputPathMatcher(
509+
normalizePathSeparatorsForPath(compiler.outputPath)
510+
);
511+
const ignore = ignoringOutputPath(
512+
isInOutputPath,
513+
this._createIgnoreMatcher(context, globOptions.ignore, matchOptions)
481514
);
482515
/** @type {Set<string>} */
483516
const links = new Set();
@@ -489,6 +522,7 @@ class CopyPlugin {
489522
globOptions,
490523
matchOptions,
491524
ignore,
525+
isInOutputPath,
492526
links
493527
})
494528
);
@@ -570,14 +604,15 @@ class CopyPlugin {
570604
* @param {CopyGlobOptions} options.globOptions options of the glob
571605
* @param {GlobMatchOptions} options.matchOptions options the glob is matched with
572606
* @param {IgnoreMatcher} options.ignore what is not copied
607+
* @param {InOutputPathMatcher} options.isInOutputPath what is inside the output directory
573608
* @param {Set<string>} options.links absolute paths of the symlinks copied as links, filled by the walk
574609
* @returns {Promise<ResolvedFrom>} the files it reaches
575610
*/
576611
async _resolveFrom(
577612
compiler,
578613
compilation,
579614
logger,
580-
{ from, context, globOptions, matchOptions, ignore, links }
615+
{ from, context, globOptions, matchOptions, ignore, isInOutputPath, links }
581616
) {
582617
const fs = /** @type {InputFileSystem} */ (compiler.inputFileSystem);
583618
const followSymlinks = globOptions.followSymlinks !== false;
@@ -657,16 +692,20 @@ class CopyPlugin {
657692
);
658693
});
659694

660-
await this._addPatternDependency(
661-
fs,
662-
compilation,
663-
typeOfFrom,
664-
absoluteFrom,
665-
base,
666-
// only a glob can name a base which is not there — a directory `from`
667-
// was stat'd above, and a file one watches itself
668-
entries.length === 0 && typeOfFrom === "glob"
669-
);
695+
// a base inside the output directory copies nothing, so watching it would
696+
// only rebuild on what this compilation itself wrote there
697+
if (!isInOutputPath(normalizedBase)) {
698+
await this._addPatternDependency(
699+
fs,
700+
compilation,
701+
typeOfFrom,
702+
absoluteFrom,
703+
base,
704+
// only a glob can name a base which is not there — a directory `from`
705+
// was stat'd above, and a file one watches itself
706+
entries.length === 0 && typeOfFrom === "glob"
707+
);
708+
}
670709

671710
return { from, typeOfFrom, glob, base, entries };
672711
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"use strict";
2+
3+
require("../helpers/warmup-webpack");
4+
5+
const path = require("path");
6+
const fs = require("graceful-fs");
7+
const { Volume, createFsFromVolume } = require("memfs");
8+
/** @type {(path: string, callback: (err?: unknown) => void) => void} */
9+
const rimraf = require("rimraf");
10+
11+
const tempFolderPath = path.join(
12+
path.resolve(__dirname, ".."),
13+
"CopyPluginTemp"
14+
);
15+
const staticPath = path.join(tempFolderPath, "static");
16+
// the output directory sits inside the directory the pattern copies, which is
17+
// what a project emitting its bundle into its own static folder looks like
18+
const outputPath = path.join(staticPath, "build");
19+
const stalePath = path.join(outputPath, "stale.txt");
20+
21+
/**
22+
* Lay out a project whose `output.path` is below what the pattern copies, with
23+
* a file an earlier build left in the output directory.
24+
* @returns {void}
25+
*/
26+
const createFiles = () => {
27+
fs.mkdirSync(path.join(staticPath, "nested"), { recursive: true });
28+
fs.mkdirSync(outputPath, { recursive: true });
29+
fs.writeFileSync(
30+
path.join(tempFolderPath, "index.js"),
31+
"module.exports = 1;"
32+
);
33+
fs.writeFileSync(path.join(staticPath, "keep.txt"), "keep");
34+
fs.writeFileSync(path.join(staticPath, "nested", "deep.txt"), "deep");
35+
fs.writeFileSync(stalePath, "stale");
36+
};
37+
38+
/**
39+
* @returns {Promise<import("../../").Compilation>} the compilation of one build
40+
*/
41+
const compile = () => {
42+
const webpack = require("../..");
43+
const compiler = webpack({
44+
mode: "development",
45+
context: tempFolderPath,
46+
entry: "./index.js",
47+
output: {
48+
path: outputPath,
49+
filename: "bundle.js",
50+
copy: [{ from: "static", to: "." }]
51+
}
52+
});
53+
54+
compiler.outputFileSystem = /** @type {EXPECTED_ANY} */ (
55+
createFsFromVolume(new Volume())
56+
);
57+
58+
return new Promise((resolve, reject) => {
59+
compiler.run((err, stats) => {
60+
if (err) {
61+
reject(err);
62+
return;
63+
}
64+
compiler.close(() => {
65+
resolve(/** @type {import("../../").Stats} */ (stats).compilation);
66+
});
67+
});
68+
});
69+
};
70+
71+
describe("CopyPlugin", () => {
72+
/** @type {import("../../").Compilation} */
73+
let compilation;
74+
75+
beforeAll(async () => {
76+
await new Promise((resolve) => {
77+
rimraf(tempFolderPath, resolve);
78+
});
79+
createFiles();
80+
compilation = await compile();
81+
});
82+
83+
afterAll((done) => {
84+
rimraf(tempFolderPath, done);
85+
}, 30000);
86+
87+
it("should not copy a file from inside the output path", () => {
88+
// copying it would write it one directory deeper on every build, which
89+
// invalidates the watch and grows the output without bound
90+
expect(compilation.getAsset("build/stale.txt")).toBeUndefined();
91+
});
92+
93+
it("should copy the rest of the directory the output path sits in", () => {
94+
expect(compilation.getAsset("keep.txt")).toBeDefined();
95+
expect(compilation.getAsset("nested/deep.txt")).toBeDefined();
96+
});
97+
98+
it("should not make a file inside the output path a dependency", () => {
99+
expect([...compilation.fileDependencies]).not.toContain(stalePath);
100+
});
101+
102+
it("should report no error or warning", () => {
103+
expect(compilation.errors).toHaveLength(0);
104+
expect(compilation.warnings).toHaveLength(0);
105+
});
106+
});

0 commit comments

Comments
 (0)