paulmillr / chokidar

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

On Windows, a change event is fired after an add event when a file is created #1332

Closed valadaptive closed 2 months ago

valadaptive commented 2 months ago

Versions (please complete the following information):

To Reproduce:

On a Windows system, run .\node_modules\.bin\mocha -g "previously deleted" (for v3) or node --test --test-name-pattern "previously deleted" (for v4) to run a test which expects change events to not be fired. You may need to run it quite a few times.

Here's a test case for the v4 node --test setup:

    it('should not emit `change` events for added files', async () => {
      const addSpy = sinon.spy(function add(){});
      const changeSpy = sinon.spy(function change(){});
      const testPath = getFixturePath('add.txt');
      watcher
        .on(EV.ADD, addSpy)
        .on(EV.CHANGE, changeSpy);
      await write(testPath, 'hello');
      await waitFor([[addSpy.withArgs(testPath), 1]]);
      changeSpy.should.not.have.been.called;
    });

Expected behavior The change event should not be fired. Chokidar already deduplicates successive change events that happen within 5ms of each other, but does not deduplicate change requests that happen within 5ms of an add event.

Additional context This seems to be causing the test failure in #1329. I can only guess that it's not failing in the current branches because of some complex timing conditions or just because it got very lucky.

valadaptive commented 2 months ago

Ah, looks like this is already implemented as awaitWriteFinish. Maybe it's worth defaulting it to true in the next major release and adjusting the timing?