diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 1b16efe4a7a..e8ad3c309fd 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -8,7 +8,7 @@ namespace ts { write(s: string): void; readFile(path: string, encoding?: string): string; writeFile(path: string, data: string, writeByteOrderMark?: boolean): void; - watchFile?(path: string, callback: (path: string) => void): FileWatcher; + watchFile?(path: string, callback: (path: string, removed?: boolean) => void): FileWatcher; watchDirectory?(path: string, callback: (path: string) => void, recursive?: boolean): FileWatcher; resolvePath(path: string): string; fileExists(path: string): boolean; @@ -23,7 +23,7 @@ namespace ts { interface WatchedFile { fileName: string; - callback: (fileName: string) => void; + callback: (fileName: string, removed?: boolean) => void; mtime: Date; } @@ -235,7 +235,7 @@ namespace ts { } else if (watchedFile.mtime.getTime() !== stats.mtime.getTime()) { watchedFile.mtime = WatchedFileSet.getModifiedTime(watchedFile.fileName); - watchedFile.callback(watchedFile.fileName); + watchedFile.callback(watchedFile.fileName, watchedFile.mtime.getTime() === 0); } }); } @@ -263,7 +263,7 @@ namespace ts { }, this.interval); } - addFile(fileName: string, callback: (fileName: string) => void): WatchedFile { + addFile(fileName: string, callback: (fileName: string, removed?: boolean) => void): WatchedFile { var file: WatchedFile = { fileName, callback, diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index d5682d8860e..a6d51542989 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -291,7 +291,7 @@ namespace ts { let sourceFile = hostGetSourceFile(fileName, languageVersion, onError); if (sourceFile && compilerOptions.watch) { // Attach a file watcher - sourceFile.fileWatcher = sys.watchFile(sourceFile.fileName, () => sourceFileChanged(sourceFile)); + sourceFile.fileWatcher = sys.watchFile(sourceFile.fileName, (fileName: string, removed?: boolean) => sourceFileChanged(sourceFile, removed)); } return sourceFile; } @@ -313,9 +313,15 @@ namespace ts { } // If a source file changes, mark it as unwatched and start the recompilation timer - function sourceFileChanged(sourceFile: SourceFile) { + function sourceFileChanged(sourceFile: SourceFile, removed?: boolean) { sourceFile.fileWatcher.close(); sourceFile.fileWatcher = undefined; + if (removed) { + var index = rootFileNames.indexOf(sourceFile.fileName); + if (index !== -1) { + rootFileNames.splice(index, 1); + } + } startTimer(); }