Skip to content

Commit f3a5c82

Browse files
fix(output): anchor a copy glob on the base the walk resolves to
`_resolveGlob` returns an absolute pattern as written, so `..` survived in the glob while the walk resolved it away — `/repo/src/../assets/*.txt` matched nothing and warned that it copied no file. The glob is now re-anchored on the normalized base, so both sides name the same directory.
1 parent 1cdb19b commit f3a5c82

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎lib/CopyPlugin.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,17 +348,23 @@ class CopyPlugin {
348348
// everything is copied relative to what `from` names literally, so a
349349
// directory, a file and a glob all keep the structure below it
350350
const escapedBase = extractGlobBaseDir(glob);
351-
// the walk reads the base through the cache the watcher purges under the
352-
// watched directory's own name, so both have to be that one native path
353-
const base = trimTrailingSeparator(
354-
path.normalize(unescapeGlobPath(escapedBase))
355-
);
356351
// only `**` matches across depths, so any other glob names how deep a
357352
// match can sit and the walk never descends past it
358353
const segments = glob.slice(escapedBase.length).split("/");
359354
const maxDepth = segments.includes("**")
360355
? Number.POSITIVE_INFINITY
361356
: segments.length - 1;
357+
// the walk reads the base through the cache the watcher purges under the
358+
// watched directory's own name, so both have to be that one native path
359+
const base = trimTrailingSeparator(
360+
path.normalize(unescapeGlobPath(escapedBase))
361+
);
362+
// an absolute pattern keeps the `..` segments the walk resolves away, so
363+
// the glob is re-anchored on the base the walk reads
364+
glob = path.posix.join(
365+
escapeGlobPattern(normalizePathSeparatorsForPath(base)),
366+
...segments
367+
);
362368
logger.debug(`'${from}' is a ${typeOfFrom}, globbing '${glob}'`);
363369

364370
const entries =

‎test/configCases/copy/glob/index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ it("should copy a dot directory the glob names, no deeper than it reaches", () =
2424
it("should copy an absolute 'from'", () => {
2525
expect(read("absolute.txt")).toBe("keep");
2626
});
27+
28+
it("should copy an absolute 'from' which walks back up a directory", () => {
29+
expect(read("dots/keep.txt")).toBe("keep");
30+
});

‎test/configCases/copy/glob/webpack.config.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ module.exports = {
1111
{
1212
from: path.resolve(__dirname, "assets/keep.txt"),
1313
filename: "absolute.txt"
14+
},
15+
{
16+
from: [path.resolve(__dirname, "assets"), "sub", "..", "*.txt"].join(
17+
"/"
18+
),
19+
to: "dots"
1420
}
1521
]
1622
}

0 commit comments

Comments
 (0)