diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index 7a98c43dd04a7..ff21c75008903 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -749,6 +749,9 @@ namespace ts { (missingFilePathsRequestedForRelease || (missingFilePathsRequestedForRelease = [])).push(oldSourceFile.path); } else if ((hostSourceFileInfo as FilePresentOnHost).sourceFile === oldSourceFile) { + if ((hostSourceFileInfo as FilePresentOnHost).fileWatcher) { + (hostSourceFileInfo as FilePresentOnHost).fileWatcher.close(); + } sourceFilesCache.delete(oldSourceFile.path); resolutionCache.removeResolutionsOfFile(oldSourceFile.path); } diff --git a/src/harness/unittests/tscWatchMode.ts b/src/harness/unittests/tscWatchMode.ts index b7a477b04bf1f..17431d17da62b 100644 --- a/src/harness/unittests/tscWatchMode.ts +++ b/src/harness/unittests/tscWatchMode.ts @@ -1113,6 +1113,34 @@ namespace ts.tscWatch { checkProgramActualFiles(watch(), files.map(file => file.path)); checkOutputErrors(host, [], ExpectedOutputErrorsPosition.AfterFileChangeDetected); }); + + it("watched files when file is deleted and new file is added as part of change", () => { + const projectLocation = "/home/username/project"; + const file: FileOrFolder = { + path: `${projectLocation}/src/file1.ts`, + content: "var a = 10;" + }; + const configFile: FileOrFolder = { + path: `${projectLocation}/tsconfig.json`, + content: "{}" + }; + const files = [file, libFile, configFile]; + const host = createWatchedSystem(files); + const watch = createWatchOfConfigFile(configFile.path, host); + verifyProgram(); + + file.path = file.path.replace("file1", "file2"); + host.reloadFS(files); + host.runQueuedTimeoutCallbacks(); + verifyProgram(); + + function verifyProgram() { + checkProgramActualFiles(watch(), mapDefined(files, f => f === configFile ? undefined : f.path)); + checkWatchedDirectories(host, [], /*recursive*/ false); + checkWatchedDirectories(host, [projectLocation, `${projectLocation}/node_modules/@types`], /*recursive*/ true); + checkWatchedFiles(host, files.map(f => f.path)); + } + }); }); describe("tsc-watch emit with outFile or out setting", () => {