@@ -197,41 +197,64 @@ class DiagnosticsWatcher {
197
197
this . updateWatchedDirectories ( ) ;
198
198
}
199
199
200
+ private isSubdir ( candidate : string , parent : string ) {
201
+ const c = path . resolve ( candidate ) ;
202
+ const p = path . resolve ( parent ) ;
203
+ return c === p || c . startsWith ( p + path . sep ) ;
204
+ }
205
+
206
+ private minimizeDirs ( dirs : string [ ] ) : string [ ] {
207
+ const sorted = [ ...new Set ( dirs . map ( ( d ) => path . resolve ( d ) ) ) ] . sort ( ) ;
208
+ const result : string [ ] = [ ] ;
209
+ for ( const dir of sorted ) {
210
+ if ( ! result . some ( ( p ) => this . isSubdir ( dir , p ) ) ) {
211
+ result . push ( dir ) ;
212
+ }
213
+ }
214
+ return result ;
215
+ }
216
+
200
217
addWatchDirectory ( dir : string ) {
201
- if ( ! dir || this . currentWatchedDirs . has ( dir ) ) {
202
- return ;
218
+ if ( ! dir ) return ;
219
+ // Skip if already covered by an existing watched directory
220
+ for ( const existing of this . currentWatchedDirs ) {
221
+ if ( this . isSubdir ( dir , existing ) ) {
222
+ return ;
223
+ }
224
+ }
225
+ // If new dir is a parent of existing ones, unwatch children
226
+ const toRemove : string [ ] = [ ] ;
227
+ for ( const existing of this . currentWatchedDirs ) {
228
+ if ( this . isSubdir ( existing , dir ) ) {
229
+ toRemove . push ( existing ) ;
230
+ }
231
+ }
232
+ if ( toRemove . length ) {
233
+ this . watcher . unwatch ( toRemove ) ;
234
+ for ( const r of toRemove ) this . currentWatchedDirs . delete ( r ) ;
203
235
}
204
236
this . watcher . add ( dir ) ;
205
237
this . currentWatchedDirs . add ( dir ) ;
206
- // New files might now be visible; schedule a run
207
- this . scheduleDiagnostics ( ) ;
208
238
}
209
239
210
240
private async updateWatchedDirectories ( ) {
211
241
const watchDirs = await this . svelteCheck . getWatchDirectories ( ) ;
212
- const dirsToWatch = watchDirs || [ { path : this . workspaceUri . fsPath , recursive : true } ] ;
213
- const newDirs = new Set ( dirsToWatch . map ( ( d ) => d . path ) ) ;
242
+ const desired = this . minimizeDirs (
243
+ ( watchDirs ?. map ( ( d ) => d . path ) || [ this . workspaceUri . fsPath ] ) . map ( ( p ) =>
244
+ path . resolve ( p )
245
+ )
246
+ ) ;
214
247
215
- // Fast diff: find directories to add and remove
216
- const toAdd = [ ...newDirs ] . filter ( ( dir ) => ! this . currentWatchedDirs . has ( dir ) ) ;
217
- const toRemove = [ ...this . currentWatchedDirs ] . filter ( ( dir ) => ! newDirs . has ( dir ) ) ;
248
+ const current = new Set ( [ ...this . currentWatchedDirs ] . map ( ( p ) => path . resolve ( p ) ) ) ;
249
+ const desiredSet = new Set ( desired ) ;
218
250
219
- // Add new directories
220
- if ( toAdd . length > 0 ) {
221
- this . watcher . add ( toAdd ) ;
222
- }
251
+ const toAdd = desired . filter ( ( d ) => ! current . has ( d ) ) ;
252
+ const toRemove = [ ...current ] . filter ( ( d ) => ! desiredSet . has ( d ) ) ;
223
253
224
- // Remove old directories
225
- if ( toRemove . length > 0 ) {
226
- this . watcher . unwatch ( toRemove ) ;
227
- }
254
+ if ( toAdd . length ) this . watcher . add ( toAdd ) ;
255
+ if ( toRemove . length ) this . watcher . unwatch ( toRemove ) ;
228
256
229
- // Update current set
230
- this . currentWatchedDirs = newDirs ;
231
-
232
- if ( this . ignoreInitialAdd ) {
233
- this . scheduleDiagnostics ( ) ;
234
- }
257
+ this . currentWatchedDirs = new Set ( desired ) ;
235
258
}
236
259
237
260
private async updateDocument ( path : string , isNew : boolean ) {
@@ -319,7 +342,8 @@ parseOptions(async (opts) => {
319
342
watcher . updateWatchers ( ) ;
320
343
watcher . scheduleDiagnostics ( ) ;
321
344
} ;
322
- svelteCheckOptions . onSnapshotCreated = ( dirPath : string ) => {
345
+ svelteCheckOptions . onFileSnapshotCreated = ( filePath : string ) => {
346
+ const dirPath = path . dirname ( filePath ) ;
323
347
watcher . addWatchDirectory ( dirPath ) ;
324
348
} ;
325
349
watcher = new DiagnosticsWatcher (
0 commit comments