joyent/node — Issue #2054: Mac: fs.watch triggered twice on fs.writeFile
Author: TrevorBurnham
Created: 2011-11-08 23:21:05+00:00
State: open

Related: #1970. Tested under Mac OS 10.7.2. Here's the code:

var fs = require('fs');
var prevStats = null;

fs.watch('file', function() {
fs.stat('file', function(err, stats) {
if (prevStats && (stats.size === prevStats.size) && (prevStats.mtime.getTime() === stats.mtime.getTime())) return;
prevStats = stats;
console.log(stats.size);
});
});
fs.writeFile('file', 'Arbitrary string');

The output I get is *usually*

0
16

though sometimes it's just the expected

16