Skip to content

Commit 6695b9f

Browse files
Merge pull request #350 from webpack/fix/directory-fs-accuracy
fix: learn fs accuracy from directory mtimes too
2 parents 26f591c + 67324a2 commit 6695b9f

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"watchpack": patch
3+
---
4+
5+
fix: do not report directories created shortly before `startTime` as changed when the initial scan sees only directories (#295).
6+
7+
Timestamp accuracy was learned only from file mtimes, so a scan that found only directories kept the 2000ms default and flagged anything created in the 2s before `startTime`. Directory mtimes now lower it the same way.

‎lib/DirectoryWatcher.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ class DirectoryWatcher extends EventEmitter {
687687
}
688688
this.setMissing(target, false, eventType);
689689
} else if (stats.isDirectory()) {
690+
if (stats.mtime) ensureFsAccuracy(+stats.mtime);
690691
this.setDirectory(target, +stats.birthtime || 1, false, eventType);
691692
} else if (stats.isFile() || stats.isSymbolicLink()) {
692693
if (stats.mtime) {
@@ -1000,6 +1001,9 @@ class DirectoryWatcher extends EventEmitter {
10001001
stats.isDirectory() &&
10011002
(!initial || !this.directories.has(itemPath))
10021003
) {
1004+
// mtime, not birthtime: FAT keeps creation times finer than
1005+
// modification times, so only mtime shows the real accuracy
1006+
if (stats.mtime) ensureFsAccuracy(+stats.mtime);
10031007
this.setDirectory(
10041008
itemPath,
10051009
+stats.birthtime || 1,
@@ -1045,6 +1049,9 @@ class DirectoryWatcher extends EventEmitter {
10451049
// Treat a symlink whose target is a directory as a
10461050
// nested watched directory so files inside the target
10471051
// propagate change events to the symlink path.
1052+
if (targetStats.mtime) {
1053+
ensureFsAccuracy(+targetStats.mtime);
1054+
}
10481055
this.setDirectory(
10491056
itemPath,
10501057
+targetStats.birthtime || +stats.birthtime || 1,

‎test/Watchpack.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,37 @@ describe("Watchpack", () => {
12681268
});
12691269
});
12701270

1271+
it("should not report directories created before start time when only directories are scanned (#295)", (done) => {
1272+
// A fresh module starts at the default 2000ms FS_ACCURACY, which only the
1273+
// stats seen during the scan can lower
1274+
/** @type {typeof WatchpackTest} */
1275+
let FreshWatchpack;
1276+
jest.isolateModules(() => {
1277+
FreshWatchpack = require("../lib");
1278+
});
1279+
testHelper.dir("dir");
1280+
testHelper.dir(path.join("dir", "sub"));
1281+
testHelper.tick(1100, () => {
1282+
const w = new FreshWatchpack({
1283+
aggregateTimeout: 100,
1284+
});
1285+
/** @type {string[]} */
1286+
const changes = [];
1287+
w.on("change", (file) => {
1288+
changes.push(file);
1289+
});
1290+
w.watch({
1291+
directories: [path.join(fixtures, "dir")],
1292+
startTime: Date.now(),
1293+
});
1294+
testHelper.tick(1000, () => {
1295+
w.close();
1296+
expect(changes).toEqual([]);
1297+
done();
1298+
});
1299+
});
1300+
});
1301+
12711302
it("should not report changes to a folder watched as file when items are added", (done) => {
12721303
const w = new WatchpackTest({
12731304
aggregateTimeout: 100,

0 commit comments

Comments
 (0)