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

Chokidar does not keep watching files after a delete/rewrite on some systems #972

Closed
lukastaegert opened this issue Jan 17, 2020 · 1 comment

Comments

@lukastaegert
Copy link

lukastaegert commented Jan 17, 2020

Describe the bug

On some Linux systems, Chokidar will correctly report an immediate "unlink" and then "add" of a file as a "change" event once but then no longer report change events for that file.

Versions (please complete the following information):

  • Chokidar version: 3.3.1
  • Node version: 10.7.0 (but also confirmed on Node 12)
  • OS version: Ubuntu 16.04.4 (in docker container)

To Reproduce:
This problem occurred on our CI in a docker container. To reproduce the problem, you can directly run a test script against our docker image. You need the following two files:

package.json

{
  "name": "chokidar-issue",
  "scripts": {
    "test": "node test.js",
    "test-docker": "docker run -v `pwd`:/test -w /test rollupcabal/circleci-node-v10:latest node test.js"
  },
  "dependencies": {
    "chokidar": "^3.3.1"
  }
}

test.js

const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');

const wait = () => new Promise(resolve => setTimeout(resolve, 500));
const file = path.resolve('test.txt');
fs.writeFileSync(file, 'Initial');

const events = [];
const watcher = chokidar
  .watch([], {ignoreInitial: true, disableGlobbing: true})
  .on('all', event => events.push(event))
  .add(file);

return wait()
  .then(() => {
    fs.unlinkSync(file);
    fs.writeFileSync(file, 'First');
  })
  .then(wait)
  .then(() => {
    console.log('Events after first update (should not be empty):', events);
    assert.strictEqual(events[0], 'change');
    events.length = 0;
    // Unwatching and adding the file again on any change is a workaround to fix the issue
    // watcher.unwatch(file);
    // watcher.add(file);
  })
  .then(wait)
  .then(() => {
    fs.unlinkSync(file);
    fs.writeFileSync(file, 'Second');
  })
  .then(wait)
  .then(() => {
    console.log('Events after second update (should not be empty):', events);
    watcher.close();
    if (events[0] !== 'change') {
      console.error('Did not receive expected "change" event');
      process.exit(1);
    }
  });

To test, just run npm install and then npm run test-docker. npm test will instead run the test on the local system.

Expected behavior
If a watched file is unlinked and then immediately rewritten, we receive a "change" event (already happens).
If this happens again after some time, another "change" event should be generated (this does NOT happen).

Additional context
This was uncovered while preparing to ship Rollup@2 with bundled chokidar@3. While bundling itself was rather painless, one of our watch test cases was consistently red after the update, hence the issue. We were able to work around this on our side by doing a watcher.unwatch(file); watcher.add(file) as response to any change event but I assume this should not be necessary.

I can confirm from my local system and our CI that the issue occurs neither on MacOS (with or without fsevents) nor on Windows.

See here for more context on the Chokidar integration in Rollup@2: rollup/rollup/#3331

@paulmillr
Copy link
Owner

The script doesn't work. Also, it eats errors. test-docker also doesn't launch with fresh Ubuntu w docker, it crashes with EACCES.

Anyway, duplicate of #591.

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

No branches or pull requests

2 participants