Skip to content

Commit 86dca7b

Browse files
authored
Merge pull request #22090 from Microsoft/fileDeletedWatchClose
In tsc--watch, fix the leaking watch when old source file is not part of program any more
2 parents a299d2d + 2777c3a commit 86dca7b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/compiler/watch.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,9 @@ namespace ts {
749749
(missingFilePathsRequestedForRelease || (missingFilePathsRequestedForRelease = [])).push(oldSourceFile.path);
750750
}
751751
else if ((hostSourceFileInfo as FilePresentOnHost).sourceFile === oldSourceFile) {
752+
if ((hostSourceFileInfo as FilePresentOnHost).fileWatcher) {
753+
(hostSourceFileInfo as FilePresentOnHost).fileWatcher.close();
754+
}
752755
sourceFilesCache.delete(oldSourceFile.path);
753756
resolutionCache.removeResolutionsOfFile(oldSourceFile.path);
754757
}

src/harness/unittests/tscWatchMode.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,34 @@ namespace ts.tscWatch {
11131113
checkProgramActualFiles(watch(), files.map(file => file.path));
11141114
checkOutputErrors(host, [], ExpectedOutputErrorsPosition.AfterFileChangeDetected);
11151115
});
1116+
1117+
it("watched files when file is deleted and new file is added as part of change", () => {
1118+
const projectLocation = "/home/username/project";
1119+
const file: FileOrFolder = {
1120+
path: `${projectLocation}/src/file1.ts`,
1121+
content: "var a = 10;"
1122+
};
1123+
const configFile: FileOrFolder = {
1124+
path: `${projectLocation}/tsconfig.json`,
1125+
content: "{}"
1126+
};
1127+
const files = [file, libFile, configFile];
1128+
const host = createWatchedSystem(files);
1129+
const watch = createWatchOfConfigFile(configFile.path, host);
1130+
verifyProgram();
1131+
1132+
file.path = file.path.replace("file1", "file2");
1133+
host.reloadFS(files);
1134+
host.runQueuedTimeoutCallbacks();
1135+
verifyProgram();
1136+
1137+
function verifyProgram() {
1138+
checkProgramActualFiles(watch(), mapDefined(files, f => f === configFile ? undefined : f.path));
1139+
checkWatchedDirectories(host, [], /*recursive*/ false);
1140+
checkWatchedDirectories(host, [projectLocation, `${projectLocation}/node_modules/@types`], /*recursive*/ true);
1141+
checkWatchedFiles(host, files.map(f => f.path));
1142+
}
1143+
});
11161144
});
11171145

11181146
describe("tsc-watch emit with outFile or out setting", () => {

0 commit comments

Comments
 (0)