Skip to content

Instantly share code, notes, and snippets.

@andrewagain
Created June 3, 2015 22:16
Show Gist options
  • Save andrewagain/fd0ddeb1692dc11cda80 to your computer and use it in GitHub Desktop.
Save andrewagain/fd0ddeb1692dc11cda80 to your computer and use it in GitHub Desktop.
Chokidar alwaysStat issue

Chokidar alwaysStat issue

Project for reproducing issue 9 in the chokidar library.

Running

First install the packages with:

npm install

To run this test, just type:

npm run start

What it does

Quickly adds and removes a file called hi in the current directory. See remove_when_added.js and write_every_second.js scripts.

Simultaneously another script (index.js) uses chokidar to watch the current directory. alwaysStat is true, but the script will occasionally print Stats is undefined., indicating that chokidar has returned undefined as the stats object.

var chokidar = require('chokidar');
var watcher = chokidar.watch('.', {
ignored: /[\/\\]\./,
ignoreInitial: true,
persistent: true,
alwaysStat: true
});
var log = console.log.bind(console);
watcher.on('add', function(path, stats) {
//process.stdout.write('a.');
if (typeof stats === 'undefined') {
console.log('Stats is undefined.');
}
}).on('raw', function(event, path, details) {
//process.stdout.write('r.');
}).on('error', function(error) {
log('Error: ', error);
});
{
"name": "chokidar-bug",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "./node_modules/.bin/parallelshell 'node remove_when_added.js' 'node write_every_second.js' 'node index.js'"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chokidar": "^1.0.2",
"fsevents": "^0.3.6",
"parallelshell": "^1.1.1",
"sleep": "^2.0.0"
}
}
var fsevents = require('fsevents');
var path = require('path');
var fs = require('fs');
var testFilePath = path.join(__dirname, 'hi');
var watcher = fsevents(testFilePath);
// tmp file must not exist at the outset
fs.unlink(testFilePath, function(err) {});
watcher.on('fsevent', function(path, flags, id) {
var rootChanged = (flags & 0x00000020) > 0;
// var created = (flags & 0x00000100) > 0;
// var removed = (flags & 0x00000200) > 0;
// console.log('fsevent. flags:',flags);
// console.log('root changed?:', rootChanged, 'created?:', created, 'removed?', removed);
if (rootChanged) {
fs.unlink(testFilePath, function(err) {
if (!err) {
//process.stdout.write('d.');
//console.log('Deleted \'hi\'');
}
// ignore errors - the file is not always present
});
}
});
watcher.start();
// NOTE: Actually writes the file 100 times per second.
var fs = require('fs');
var sleep = require('sleep');
var path = require('path');
var testFilePath = path.join(__dirname, 'hi');
while (true) {
//console.log('Writing \'hi\'');
//process.stdout.write('w.');
fs.writeFileSync(testFilePath, 'contents', {encoding:'utf8'});
// Sleep for 1/100th of a second
var microsecondsPerSecond = 1000000;
sleep.usleep(microsecondsPerSecond / 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment