Skip to content

Commit

Permalink
Merge pull request #183 from webpack/bugfix/folders
Browse files Browse the repository at this point in the history
use birthtime for folders instead of modified time
  • Loading branch information
sokra authored Dec 8, 2020
2 parents 24e5acc + ea35b33 commit 40ddaa9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/DirectoryWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ class DirectoryWatcher extends EventEmitter {
});
}

setDirectory(directoryPath, mtime, initial, type) {
setDirectory(directoryPath, birthtime, initial, type) {
if (this.checkIgnore(directoryPath)) return;
if (directoryPath === this.path) {
if (!initial) {
this.forEachWatcher(this.path, w =>
w.emit("change", directoryPath, mtime, type, initial)
w.emit("change", directoryPath, birthtime, type, initial)
);
}
} else {
Expand All @@ -258,14 +258,14 @@ class DirectoryWatcher extends EventEmitter {

let safeTime;
if (initial) {
safeTime = Math.min(now, mtime) + FS_ACCURACY;
safeTime = Math.min(now, birthtime) + FS_ACCURACY;
} else {
safeTime = now;
}

this.forEachWatcher(directoryPath, w => {
if (!initial || w.checkStartTime(safeTime, false)) {
w.emit("change", mtime, type);
w.emit("change", birthtime, type);
}
});
this.forEachWatcher(this.path, w => {
Expand Down Expand Up @@ -437,7 +437,7 @@ class DirectoryWatcher extends EventEmitter {
} else if (stats.isDirectory()) {
this.setDirectory(
filePath,
+stats.mtime || +stats.ctime || 1,
+stats.birthtime || 1,
false,
eventType
);
Expand Down Expand Up @@ -669,7 +669,7 @@ class DirectoryWatcher extends EventEmitter {
if (!initial || !this.directories.has(itemPath))
this.setDirectory(
itemPath,
+stats.mtime || +stats.ctime || 1,
+stats.birthtime || 1,
initial,
"scan (dir)"
);
Expand Down
26 changes: 26 additions & 0 deletions test/Watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,32 @@ describe("Watchpack", function() {
});
});

it("should not report changes to a folder watched as file when items are added", function(done) {
var w = new Watchpack({
aggregateTimeout: 100
});
w.on("aggregated", () => {
done(new Error("should not fire"));
});
testHelper.dir("dir");
testHelper.file("dir/a");
testHelper.tick(1000, () => {
testHelper.file("dir/b");
w.watch({
files: [path.join(fixtures, "dir")],
startTime: Date.now()
});
testHelper.tick(1000, () => {
testHelper.file("dir/c");
testHelper.tick(1000, () => {
// no event fired
w.close();
done();
});
});
});
});

it("should report removal of file and directory if it is missing in initial scan", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
Expand Down

0 comments on commit 40ddaa9

Please sign in to comment.