Skip to content

Commit

Permalink
lib: fix recursive watch in linux
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenpad committed Dec 4, 2023
1 parent d4bcdd8 commit 501f1df
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 46 deletions.
38 changes: 4 additions & 34 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
ArrayPrototypePush,
SafePromiseAllReturnVoid,
Promise,
PromisePrototypeThen,
SafeMap,
SafeSet,
StringPrototypeStartsWith,
Expand Down Expand Up @@ -42,31 +41,8 @@ function lazyLoadFsSync() {
internalSync ??= require('fs');
return internalSync;
}
let kResistStopPropagation;

async function traverse(dir, files = new SafeMap(), symbolicLinks = new SafeSet()) {
const { opendir } = lazyLoadFsPromises();

const filenames = await opendir(dir);
const subdirectories = [];

for await (const file of filenames) {
const f = pathJoin(dir, file.name);

files.set(f, file);

// Do not follow symbolic links
if (file.isSymbolicLink()) {
symbolicLinks.add(f);
} else if (file.isDirectory()) {
ArrayPrototypePush(subdirectories, traverse(f, files));
}
}

await SafePromiseAllReturnVoid(subdirectories);

return files;
}
let kResistStopPropagation;

class FSWatcher extends EventEmitter {
#options = null;
Expand Down Expand Up @@ -219,15 +195,9 @@ class FSWatcher extends EventEmitter {

if (file.isDirectory()) {
this.#files.set(filename, file);

PromisePrototypeThen(
traverse(filename, this.#files, this.#symbolicFiles),
() => {
for (const f of this.#files.keys()) {
this.#watchFile(f);
}
},
);
this.#watchFile(filename);
this.#watchFolder(filename);

} else {
this.#watchFile(filename);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(childrenAbsolutePath, 'world');

process.once('exit', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.mkdirSync(filePath);
await setTimeout(common.platformTimeout(100));
fs.writeFileSync(childrenAbsolutePath, 'world');

process.once('exit', function() {
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-fs-watch-recursive-add-file-with-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(filePath, 'world');

process.on('exit', function() {
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-fs-watch-recursive-add-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(testFile, 'world');

process.once('exit', function() {
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-fs-watch-recursive-add-folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.mkdirSync(testFile);

process.once('exit', function() {
Expand Down
3 changes: 0 additions & 3 deletions test/parallel/test-fs-watch-recursive-assert-leaks.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ tmpdir.refresh();
let watcherClosed = false;
const watcher = fs.watch(testDirectory, { recursive: true });
watcher.on('change', common.mustCallAtLeast(async (event, filename) => {
await setTimeout(common.platformTimeout(100));
if (filename === path.basename(filePath)) {
watcher.close();
watcherClosed = true;
}
await setTimeout(common.platformTimeout(100));
assert(!process._getActiveHandles().some((handle) => handle.constructor.name === 'StatWatcher'));
}));

process.on('exit', function() {
assert(watcherClosed, 'watcher Object was not closed');
});
await setTimeout(common.platformTimeout(100));
fs.writeFileSync(filePath, 'content');
})().then(common.mustCall());
3 changes: 0 additions & 3 deletions test/parallel/test-fs-watch-recursive-symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(filePath, 'world');

process.once('exit', function() {
Expand Down Expand Up @@ -89,9 +88,7 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(forbiddenFile, 'world');
await setTimeout(common.platformTimeout(100));
fs.writeFileSync(acceptableFile, 'acceptable');

process.once('exit', function() {
Expand Down

0 comments on commit 501f1df

Please sign in to comment.