Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
caghand opened this issue Mar 25, 2024 · 0 comments

Comments

@caghand
Copy link

caghand commented Mar 25, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants