watcher
watcher copied to clipboard
Windows FileWatcher misses modifications made after watcher.ready fires
On Windows I see missed events if we wait for watcher.ready and then modify a file that was very recently created. This code watches for ready then modifies the file, but the watch event is never printed. Adding a fake delay before modifying the file "fixes" the issue.
import 'dart:io';
import 'package:watcher/watcher.dart';
Future<void> main(List<String> arguments) async {
Directory('tmp').createSync(recursive: true);
final file = File('tmp/file1')
..createSync()
..writeAsString('one');
final watcher = FileWatcher(file.path);
final sub = watcher.events.listen((event) {
print(event.type);
});
// Wait for watcher to be ready so we do not miss events.
await watcher.ready;
// Uncommenting this fixes the issue.
// await Future.delayed(const Duration(seconds: 5));
// Write to the file - this should be detected.
file.writeAsString('two');
await Future.delayed(const Duration(seconds: 5));
await sub.cancel();
}