From 7658331a44ae356628c57781332724bfd3917950 Mon Sep 17 00:00:00 2001 From: Aviv Keller <38299977+RedYetiDev@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:26:18 -0400 Subject: [PATCH] fs: fix ENOENT on file deletion --- lib/internal/fs/recursive_watch.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/internal/fs/recursive_watch.js b/lib/internal/fs/recursive_watch.js index 7d8b12eeb934..b16a1e3ed1cf 100644 --- a/lib/internal/fs/recursive_watch.js +++ b/lib/internal/fs/recursive_watch.js @@ -157,11 +157,20 @@ class FSWatcher extends EventEmitter { persistent: this.#options.persistent, }, (eventType, filename) => { const existingStat = this.#files.get(file); - const currentStats = statSync(file); + let currentStats; + try { + currentStats = statSync(file); + } catch (error) { + if (error.code === 'ENOENT') { + currentStats = false; + } else { + throw error; + } + } this.#files.set(file, currentStats); - if (currentStats.birthtimeMs === 0 && existingStat.birthtimeMs !== 0) { + if (!currentStats || (currentStats.birthtimeMs === 0 && existingStat.birthtimeMs !== 0)) { // The file is now deleted this.#files.delete(file); this.#watchers.delete(file);