@@ -194,13 +194,17 @@ class DiagnosticsWatcher {
194
194
. on ( 'unlink' , ( path ) => this . removeDocument ( path ) )
195
195
. on ( 'change' , ( path ) => this . updateDocument ( path , false ) ) ;
196
196
197
- this . updateWatchedDirectories ( ) ;
198
- if ( this . ignoreInitialAdd ) {
199
- getDiagnostics ( this . workspaceUri , this . writer , this . svelteCheck ) ;
200
- }
197
+ this . updateWildcardWatcher ( ) . then ( ( ) => {
198
+ // ensuring the typescript program is built after wildcard watchers are added
199
+ // so that individual file watchers added from onFileSnapshotCreated
200
+ // run after the wildcard ones
201
+ if ( this . ignoreInitialAdd ) {
202
+ getDiagnostics ( this . workspaceUri , this . writer , this . svelteCheck ) ;
203
+ }
204
+ } ) ;
201
205
}
202
206
203
- private isSubdir ( candidate : string , parent : string ) {
207
+ private isSubDir ( candidate : string , parent : string ) {
204
208
const c = path . resolve ( candidate ) ;
205
209
const p = path . resolve ( parent ) ;
206
210
return c === p || c . startsWith ( p + path . sep ) ;
@@ -210,37 +214,37 @@ class DiagnosticsWatcher {
210
214
const sorted = [ ...new Set ( dirs . map ( ( d ) => path . resolve ( d ) ) ) ] . sort ( ) ;
211
215
const result : string [ ] = [ ] ;
212
216
for ( const dir of sorted ) {
213
- if ( ! result . some ( ( p ) => this . isSubdir ( dir , p ) ) ) {
217
+ if ( ! result . some ( ( p ) => this . isSubDir ( dir , p ) ) ) {
214
218
result . push ( dir ) ;
215
219
}
216
220
}
217
221
return result ;
218
222
}
219
223
220
224
addWatchDirectory ( dir : string ) {
221
- if ( ! dir ) return ;
225
+ if ( ! dir ) {
226
+ return ;
227
+ }
228
+
222
229
// Skip if already covered by an existing watched directory
223
230
for ( const existing of this . currentWatchedDirs ) {
224
- if ( this . isSubdir ( dir , existing ) ) {
231
+ if ( this . isSubDir ( dir , existing ) ) {
225
232
return ;
226
233
}
227
234
}
228
- // If new dir is a parent of existing ones, unwatch children
229
- const toRemove : string [ ] = [ ] ;
235
+
236
+ // Don't remove existing watchers, chokidar `unwatch` ignores future events from that path instead of closing the watcher in some cases
230
237
for ( const existing of this . currentWatchedDirs ) {
231
- if ( this . isSubdir ( existing , dir ) ) {
232
- toRemove . push ( existing ) ;
238
+ if ( this . isSubDir ( existing , dir ) ) {
239
+ this . currentWatchedDirs . delete ( existing ) ;
233
240
}
234
241
}
235
- if ( toRemove . length ) {
236
- this . watcher . unwatch ( toRemove ) ;
237
- for ( const r of toRemove ) this . currentWatchedDirs . delete ( r ) ;
238
- }
242
+
239
243
this . watcher . add ( dir ) ;
240
244
this . currentWatchedDirs . add ( dir ) ;
241
245
}
242
246
243
- private async updateWatchedDirectories ( ) {
247
+ private async updateWildcardWatcher ( ) {
244
248
const watchDirs = await this . svelteCheck . getWatchDirectories ( ) ;
245
249
const desired = this . minimizeDirs (
246
250
( watchDirs ?. map ( ( d ) => d . path ) || [ this . workspaceUri . fsPath ] ) . map ( ( p ) =>
@@ -249,15 +253,13 @@ class DiagnosticsWatcher {
249
253
) ;
250
254
251
255
const current = new Set ( [ ...this . currentWatchedDirs ] . map ( ( p ) => path . resolve ( p ) ) ) ;
252
- const desiredSet = new Set ( desired ) ;
253
256
254
257
const toAdd = desired . filter ( ( d ) => ! current . has ( d ) ) ;
255
- const toRemove = [ ...current ] . filter ( ( d ) => ! desiredSet . has ( d ) ) ;
256
-
257
- if ( toAdd . length ) this . watcher . add ( toAdd ) ;
258
- if ( toRemove . length ) this . watcher . unwatch ( toRemove ) ;
258
+ if ( toAdd . length ) {
259
+ this . watcher . add ( toAdd ) ;
260
+ }
259
261
260
- this . currentWatchedDirs = new Set ( desired ) ;
262
+ this . currentWatchedDirs = new Set ( [ ... current , ... toAdd ] ) ;
261
263
}
262
264
263
265
private async updateDocument ( path : string , isNew : boolean ) {
@@ -280,9 +282,9 @@ class DiagnosticsWatcher {
280
282
this . scheduleDiagnostics ( ) ;
281
283
}
282
284
283
- updateWatchers ( ) {
285
+ updateWildcardWatchers ( ) {
284
286
clearTimeout ( this . pendingWatcherUpdate ) ;
285
- this . pendingWatcherUpdate = setTimeout ( ( ) => this . updateWatchedDirectories ( ) , 1000 ) ;
287
+ this . pendingWatcherUpdate = setTimeout ( ( ) => this . updateWildcardWatcher ( ) , 1000 ) ;
286
288
}
287
289
288
290
scheduleDiagnostics ( ) {
@@ -342,7 +344,7 @@ parseOptions(async (opts) => {
342
344
// Wire callbacks that can reference the watcher instance created below
343
345
let watcher : DiagnosticsWatcher ;
344
346
svelteCheckOptions . onProjectReload = ( ) => {
345
- watcher . updateWatchers ( ) ;
347
+ watcher . updateWildcardWatchers ( ) ;
346
348
watcher . scheduleDiagnostics ( ) ;
347
349
} ;
348
350
svelteCheckOptions . onFileSnapshotCreated = ( filePath : string ) => {
0 commit comments