From c708c452370610fdce1d264a67db9261748b71f1 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 12 Mar 2024 16:04:04 +0300 Subject: [PATCH 1/4] fix: do not report directory as initial missing on the second watch --- lib/DirectoryWatcher.js | 1 + test/DirectoryWatcher.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/DirectoryWatcher.js b/lib/DirectoryWatcher.js index abbb142..32eb3cc 100644 --- a/lib/DirectoryWatcher.js +++ b/lib/DirectoryWatcher.js @@ -365,6 +365,7 @@ class DirectoryWatcher extends EventEmitter { !this.directories.has(filePath) && watcher.checkStartTime(this.initialScanFinished, false) ) { + console.log(watcher.checkStartTime(this.initialScanFinished, false)) process.nextTick(() => { if (this.closed) return; watcher.emit("initial-missing", "watch (missing on attach)"); diff --git a/test/DirectoryWatcher.js b/test/DirectoryWatcher.js index 8b8072d..f5cf8d8 100644 --- a/test/DirectoryWatcher.js +++ b/test/DirectoryWatcher.js @@ -169,6 +169,29 @@ describe("DirectoryWatcher", function() { }); }); + it("should not report directory as initial missing on the second watch", function(done) { + var wm = getWatcherManager({}); + testHelper.dir("dir1"); + wm.watchDirectory(path.join(fixtures, "dir1")); + + testHelper.tick(function() { + var initialMissing = false; + wm.watchDirectory(path.join(fixtures, "dir1")).on( + "initial-missing", + () => { + initialMissing = true; + } + ); + process.nextTick(() => { + for (const [, w] of wm.directoryWatchers) { + w.close(); + } + initialMissing.should.be.eql(false); + done(); + }); + }); + }); + if (!+process.env.WATCHPACK_POLLING) { it("should log errors emitted from watcher to stderr", function(done) { var error_logged = false; From 54427a1ab66e637217214a1873aa7d1adf090d9e Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 12 Mar 2024 16:08:31 +0300 Subject: [PATCH 2/4] refactor: remove debug code --- lib/DirectoryWatcher.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/DirectoryWatcher.js b/lib/DirectoryWatcher.js index 32eb3cc..abbb142 100644 --- a/lib/DirectoryWatcher.js +++ b/lib/DirectoryWatcher.js @@ -365,7 +365,6 @@ class DirectoryWatcher extends EventEmitter { !this.directories.has(filePath) && watcher.checkStartTime(this.initialScanFinished, false) ) { - console.log(watcher.checkStartTime(this.initialScanFinished, false)) process.nextTick(() => { if (this.closed) return; watcher.emit("initial-missing", "watch (missing on attach)"); From 9563a953b44b506571763d067e67b528aee37712 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 12 Mar 2024 16:18:58 +0300 Subject: [PATCH 3/4] fix: logic --- lib/DirectoryWatcher.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/DirectoryWatcher.js b/lib/DirectoryWatcher.js index abbb142..034447d 100644 --- a/lib/DirectoryWatcher.js +++ b/lib/DirectoryWatcher.js @@ -362,6 +362,7 @@ class DirectoryWatcher extends EventEmitter { }); } } else if ( + filePath !== this.path && !this.directories.has(filePath) && watcher.checkStartTime(this.initialScanFinished, false) ) { From 3c5eefdac2612a32aa7c94a029886fbdec0a0f87 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 12 Mar 2024 16:42:41 +0300 Subject: [PATCH 4/4] test: more --- test/DirectoryWatcher.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/test/DirectoryWatcher.js b/test/DirectoryWatcher.js index f5cf8d8..30c1757 100644 --- a/test/DirectoryWatcher.js +++ b/test/DirectoryWatcher.js @@ -169,7 +169,30 @@ describe("DirectoryWatcher", function() { }); }); - it("should not report directory as initial missing on the second watch", function(done) { + it("should report directory as initial missing on the second watch when directory doesn't exist", function(done) { + var wm = getWatcherManager({}); + testHelper.dir("dir1"); + wm.watchDirectory(path.join(fixtures, "dir1")); + + testHelper.tick(function() { + var initialMissing = false; + wm.watchDirectory(path.join(fixtures, "dir3")).on( + "initial-missing", + () => { + initialMissing = true; + } + ); + testHelper.tick(function() { + for (const [, w] of wm.directoryWatchers) { + w.close(); + } + initialMissing.should.be.eql(true); + done(); + }); + }); + }); + + it("should not report directory as initial missing on the second watch when directory exists", function(done) { var wm = getWatcherManager({}); testHelper.dir("dir1"); wm.watchDirectory(path.join(fixtures, "dir1")); @@ -182,7 +205,7 @@ describe("DirectoryWatcher", function() { initialMissing = true; } ); - process.nextTick(() => { + testHelper.tick(function() { for (const [, w] of wm.directoryWatchers) { w.close(); }