paulmillr / chokidar

Minimal and efficient cross-platform file watching library
https://paulmillr.com
MIT License
10.8k stars 574 forks source link

change event doesn't work after unlink #1293

Closed qarlosalberto closed 2 months ago

qarlosalberto commented 9 months ago

Describe the bug

When I remove a file the unlink event is called. After that the change event doesn't work when I recreate the file

Versions:

ah-naf commented 3 months ago

It works on the latest (3.6.0) version.

ajgassner commented 2 months ago

I can confirm it's not working reliable. We use version 3.6.0. Most of the time add/change events don't get fired when they occur immediately after the unlink event. OS: Windows 11.

Tested with following code:

import { dirname } from 'path';
import { existsSync } from 'fs';
import { FSWatcher } from 'chokidar';
import { exit } from 'process';

const handleEvent = (path) => {
  console.log('event handled');
};

const fsWatcher = new FSWatcher({
    ignorePermissionErrors: true,
    ignoreInitial: true,
    awaitWriteFinish: {
        stabilityThreshold: 1000,
        pollInterval: 100,
    },
});

fsWatcher.on('all', (eventName, path) => {
    console.log(`fired event ${eventName}`);
    if (eventName === 'add' || eventName === 'change') {
        handleEvent(path);
    }
});

const filePath = '/Users/xxx/import/import.txt';
const parentDir = dirname(filePath);

if (!existsSync(parentDir)) {
    console.log(`will not start watching ${filePath}. parent directory ${parentDir} does not exist`);
    exit(1);
}

fsWatcher.add(filePath);
console.log('watching ' + filePath);