Skip to content

Commit aa22c56

Browse files
committed
Swallow the directory watcher exceptions
1 parent b9592d4 commit aa22c56

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

src/server/server.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,10 +753,21 @@ namespace ts.server {
753753
const sys = <ServerHost>ts.sys;
754754
// use watchGuard process on Windows when node version is 4 or later
755755
const useWatchGuard = process.platform === "win32" && getNodeMajorVersion() >= 4;
756+
const originalWatchDirectory = sys.watchDirectory;
757+
const noopWatcher: FileWatcher = { close: noop };
758+
function watchDirectorySwallowingException(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher {
759+
try {
760+
return originalWatchDirectory.call(sys, path, callback, recursive);
761+
}
762+
catch (e) {
763+
logger.info(`Exception when creating directory watcher: ${e.message}`);
764+
return noopWatcher;
765+
}
766+
}
767+
756768
if (useWatchGuard) {
757769
const currentDrive = extractWatchDirectoryCacheKey(sys.resolvePath(sys.getCurrentDirectory()), /*currentDriveKey*/ undefined);
758770
const statusCache = createMap<boolean>();
759-
const originalWatchDirectory = sys.watchDirectory;
760771
sys.watchDirectory = function (path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher {
761772
const cacheKey = extractWatchDirectoryCacheKey(path, currentDrive);
762773
let status = cacheKey && statusCache.get(cacheKey);
@@ -790,14 +801,17 @@ namespace ts.server {
790801
}
791802
if (status) {
792803
// this drive is safe to use - call real 'watchDirectory'
793-
return originalWatchDirectory.call(sys, path, callback, recursive);
804+
return watchDirectorySwallowingException(path, callback, recursive);
794805
}
795806
else {
796807
// this drive is unsafe - return no-op watcher
797-
return { close() { } };
808+
return noopWatcher;
798809
}
799810
};
800811
}
812+
else {
813+
sys.watchDirectory = watchDirectorySwallowingException;
814+
}
801815

802816
// Override sys.write because fs.writeSync is not reliable on Node 4
803817
sys.write = (s: string) => writeMessage(new Buffer(s, "utf8"));

0 commit comments

Comments
 (0)