@@ -428,17 +428,16 @@ class Watcher {
428
428
this . watchers = { } ;
429
429
}
430
430
431
- addWatcher ( win , watchPath , collectionUid , brunoConfig ) {
431
+ addWatcher ( win , watchPath , collectionUid , brunoConfig , forcePolling = false ) {
432
432
if ( this . watchers [ watchPath ] ) {
433
433
this . watchers [ watchPath ] . close ( ) ;
434
434
}
435
435
436
436
const ignores = brunoConfig ?. ignore || [ ] ;
437
- const self = this ;
438
437
setTimeout ( ( ) => {
439
438
const watcher = chokidar . watch ( watchPath , {
440
439
ignoreInitial : false ,
441
- usePolling : watchPath . startsWith ( '\\\\' ) ? true : false ,
440
+ usePolling : watchPath . startsWith ( '\\\\' ) || forcePolling ? true : false ,
442
441
ignored : ( filepath ) => {
443
442
const normalizedPath = filepath . replace ( / \\ / g, '/' ) ;
444
443
const relativePath = path . relative ( watchPath , normalizedPath ) ;
@@ -457,14 +456,35 @@ class Watcher {
457
456
depth : 20
458
457
} ) ;
459
458
459
+ let startedNewWatcher = false ;
460
460
watcher
461
461
. on ( 'add' , ( pathname ) => add ( win , pathname , collectionUid , watchPath ) )
462
462
. on ( 'addDir' , ( pathname ) => addDirectory ( win , pathname , collectionUid , watchPath ) )
463
463
. on ( 'change' , ( pathname ) => change ( win , pathname , collectionUid , watchPath ) )
464
464
. on ( 'unlink' , ( pathname ) => unlink ( win , pathname , collectionUid , watchPath ) )
465
- . on ( 'unlinkDir' , ( pathname ) => unlinkDir ( win , pathname , collectionUid , watchPath ) ) ;
466
-
467
- self . watchers [ watchPath ] = watcher ;
465
+ . on ( 'unlinkDir' , ( pathname ) => unlinkDir ( win , pathname , collectionUid , watchPath ) )
466
+ . on ( 'error' , ( error ) => {
467
+ // `ENOSPC` stands for "Error No space" but is also thrown if the file watcher limit is reached.
468
+ // To prevent loops `!forcePolling` is checked.
469
+ if ( error . code === 'ENOSPC' && ! startedNewWatcher && ! forcePolling ) {
470
+ // This callback is called for every file the watcher is trying to watch. To prevent a spam of messages and
471
+ // Multiple watcher being started `startedNewWatcher` is set to prevent this.
472
+ startedNewWatcher = true ;
473
+ watcher . close ( ) ;
474
+ console . error (
475
+ `\nCould not start watcher for ${ watchPath } :` ,
476
+ 'ENOSPC: System limit for number of file watchers reached!' ,
477
+ 'Trying again with polling, this will be slower!\n' ,
478
+ 'Update you system config to allow more concurrently watched files with:' ,
479
+ '"echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p"'
480
+ ) ;
481
+ this . addWatcher ( win , watchPath , collectionUid , brunoConfig , true ) ;
482
+ } else {
483
+ console . error ( `An error occurred in the watcher for: ${ watchPath } ` , error ) ;
484
+ }
485
+ } ) ;
486
+
487
+ this . watchers [ watchPath ] = watcher ;
468
488
} , 100 ) ;
469
489
}
470
490
0 commit comments