From b22e80215d04ec57c38cffcca7ef802a77d059ae Mon Sep 17 00:00:00 2001 From: zodern Date: Fri, 15 Oct 2021 12:14:56 -0500 Subject: [PATCH] Avoid creating nested recursive watchers --- tools/fs/safe-watcher.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tools/fs/safe-watcher.ts b/tools/fs/safe-watcher.ts index 467127acaa..4c8bd9f6d6 100644 --- a/tools/fs/safe-watcher.ts +++ b/tools/fs/safe-watcher.ts @@ -6,7 +6,8 @@ import { convertToOSPath, watchFile, unwatchFile, - toPosixPath + toPosixPath, + pathRelative } from "./files"; import { @@ -439,6 +440,23 @@ export function addWatchRoot(absPath: string) { } watchRoots.add(absPath); + + // If there already is a watcher for a parent directory, there is no need + // to create this watcher. + for (const path of watchRoots) { + let relativePath = pathRelative(path, absPath); + if ( + path !== absPath && + !relativePath.startsWith('..') && + !relativePath.startsWith('/') + ) { + return; + } + } + + // TODO: check if there are any existing watchers that are children of this + // watcher and stop them + nsfw( convertToOSPath(absPath), (events) => {