Skip to content

watch does not emit an event for a file created immediately after creating its parent directory #1014

Description

@caghand

You can reproduce the problem with the following test code:

const { fs } = require("memfs");
const path = require("path");

const watchedDirectory = path.join(__dirname, "watched");

fs.mkdirSync(watchedDirectory, { recursive: true });
fs.watch(watchedDirectory, { recursive: true }, (eventType, filename) => { console.log("Received: " + eventType + " " + filename); })

fs.mkdirSync(path.join(watchedDirectory, "new_dir"), { recursive: true });
fs.writeFileSync(path.join(watchedDirectory, "new_dir", "new_file"), "stuff");

You will see the following output:

Received: rename ../watched/new_dir

There is no event raised for new_file.

It looks like this is due to the following setTimeout() call, which sends critical watcher attachments to the back of the timer queue:

setTimeout(() => {

If I remove that setTimeout() wrapper, I see the following output:

Received: rename ../watched/new_dir
Received: rename ../watched/new_dir/new_file
Received: change ../watched/new_dir/new_file
Received: change ../watched/new_dir/new_file

This is much better.
Please note that, if you use Node's default fs instead of memfs, you will see the following output, which we could consider canonical:

Received: rename new_dir
Received: rename new_dir\new_file
Received: change new_dir\new_file
Received: change new_dir

There is still a small difference between fs and memfs. In Node's fs, the last change event is for the parent directory. In memfs, the last change event is for the file itself. From the point-of-view of the user, it just looks like a duplicate event. Maybe it's due to #116 ?

If I add any additional writeFileSync() calls, both fs and memfs raise duplicate change events for the file, so, they match perfectly there.

Thanks a lot for your consideration!

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions