diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index da38d389f212f..b41e89e991567 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -1266,6 +1266,7 @@ namespace ts { let activeSession: import("inspector").Session | "stopping" | undefined; let profilePath = "./profile.cpuprofile"; + let hitSystemWatcherLimit = false; const Buffer: { new (input: string, encoding?: string): any; @@ -1619,6 +1620,11 @@ namespace ts { options = { persistent: true }; } } + + if (hitSystemWatcherLimit) { + sysLog(`sysLog:: ${fileOrDirectory}:: Defaulting to fsWatchFile`); + return watchPresentFileSystemEntryWithFsWatchFile(); + } try { const presentWatcher = _fs.watch( fileOrDirectory, @@ -1635,6 +1641,8 @@ namespace ts { // Catch the exception and use polling instead // Eg. on linux the number of watches are limited and one could easily exhaust watches and the exception ENOSPC is thrown when creating watcher at that point // so instead of throwing error, use fs.watchFile + hitSystemWatcherLimit ||= e.code === "ENOSPC"; + sysLog(`sysLog:: ${fileOrDirectory}:: Changing to fsWatchFile`); return watchPresentFileSystemEntryWithFsWatchFile(); } } @@ -1656,7 +1664,6 @@ namespace ts { * Eg. on linux the number of watches are limited and one could easily exhaust watches and the exception ENOSPC is thrown when creating watcher at that point */ function watchPresentFileSystemEntryWithFsWatchFile(): FileWatcher { - sysLog(`sysLog:: ${fileOrDirectory}:: Changing to fsWatchFile`); return watchFile( fileOrDirectory, createFileWatcherCallback(callback),