@@ -123,8 +123,15 @@ class _WindowsDirectoryWatcher
123
123
void _startParentWatcher () {
124
124
var absoluteDir = p.absolute (path);
125
125
var parent = p.dirname (absoluteDir);
126
- // Check if [path] is already the root directory.
127
- if (FileSystemEntity .identicalSync (parent, path)) return ;
126
+ try {
127
+ // Check if [path] is already the root directory.
128
+ if (FileSystemEntity .identicalSync (parent, path)) return ;
129
+ } on FileSystemException catch (_) {
130
+ // Either parent or path or both might be gone due to concurrently
131
+ // occurring changes. Just ignore and continue. If we fail to
132
+ // watch path we will report an error from _startWatch.
133
+ return ;
134
+ }
128
135
var parentStream = Directory (parent).watch (recursive: false );
129
136
_parentWatchSubscription = parentStream.listen (
130
137
(event) {
@@ -185,7 +192,14 @@ class _WindowsDirectoryWatcher
185
192
186
193
if (_files.containsDir (path)) continue ;
187
194
188
- var stream = Directory (path).list (recursive: true );
195
+ // "Path not found" can be caused by creating then quickly removing
196
+ // a directory: continue without reporting an error. Nested files
197
+ // that get removed during the `list` are already ignored by `list`
198
+ // itself, so there are no other types of "path not found" that
199
+ // might need different handling here.
200
+ var stream = Directory (path)
201
+ .list (recursive: true )
202
+ .ignoring <PathNotFoundException >();
189
203
var subscription = stream.listen ((entity) {
190
204
if (entity is Directory ) return ;
191
205
if (_files.contains (entity.path)) return ;
@@ -198,14 +212,7 @@ class _WindowsDirectoryWatcher
198
212
});
199
213
subscription.onError ((Object e, StackTrace stackTrace) {
200
214
_listSubscriptions.remove (subscription);
201
- // "Path not found" can be caused by creating then quickly removing
202
- // a directory: continue without reporting an error. Nested files
203
- // that get removed during the `list` are already ignored by `list`
204
- // itself, so there are no other types of "path not found" that
205
- // might need different handling here.
206
- if (e is ! PathNotFoundException ) {
207
- _emitError (e, stackTrace);
208
- }
215
+ _emitError (e, stackTrace);
209
216
});
210
217
_listSubscriptions.add (subscription);
211
218
} else if (event is FileSystemModifyEvent ) {
@@ -435,7 +442,7 @@ class _WindowsDirectoryWatcher
435
442
436
443
_files.clear ();
437
444
var completer = Completer <void >();
438
- var stream = Directory (path).list (recursive : true );
445
+ var stream = Directory (path).listRecursivelyIgnoringErrors ( );
439
446
void handleEntity (FileSystemEntity entity) {
440
447
if (entity is ! Directory ) _files.add (entity.path);
441
448
}
0 commit comments