Skip to content

Commit e68524a

Browse files
authored
Handle WatchCompilerHost without timeout methods to retrieve correct Program (#37308)
1 parent 5d6b385 commit e68524a

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/compiler/watchPublic.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ namespace ts {
320320
watchConfigFileWildCardDirectories();
321321

322322
return configFileName ?
323-
{ getCurrentProgram: getCurrentBuilderProgram, getProgram: synchronizeProgram, close } :
324-
{ getCurrentProgram: getCurrentBuilderProgram, getProgram: synchronizeProgram, updateRootFileNames, close };
323+
{ getCurrentProgram: getCurrentBuilderProgram, getProgram: updateProgram, close } :
324+
{ getCurrentProgram: getCurrentBuilderProgram, getProgram: updateProgram, updateRootFileNames, close };
325325

326326
function close() {
327327
resolutionCache.clear();
@@ -553,7 +553,7 @@ namespace ts {
553553
host.clearTimeout(timerToUpdateProgram);
554554
}
555555
writeLog("Scheduling update");
556-
timerToUpdateProgram = host.setTimeout(updateProgram, 250);
556+
timerToUpdateProgram = host.setTimeout(updateProgramWithWatchStatus, 250);
557557
}
558558

559559
function scheduleProgramReload() {
@@ -562,10 +562,13 @@ namespace ts {
562562
scheduleProgramUpdate();
563563
}
564564

565-
function updateProgram() {
565+
function updateProgramWithWatchStatus() {
566566
timerToUpdateProgram = undefined;
567567
reportWatchDiagnostic(Diagnostics.File_change_detected_Starting_incremental_compilation);
568+
updateProgram();
569+
}
568570

571+
function updateProgram() {
569572
switch (reloadLevel) {
570573
case ConfigFileProgramReloadLevel.Partial:
571574
perfLogger.logStartUpdateProgram("PartialConfigReload");
@@ -581,6 +584,7 @@ namespace ts {
581584
break;
582585
}
583586
perfLogger.logStopUpdateProgram("Done");
587+
return getCurrentBuilderProgram();
584588
}
585589

586590
function reloadFileNamesFromConfigFile() {

src/testRunner/unittests/tscWatch/watchApi.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,27 @@ namespace ts.tscWatch {
6363
assert.equal(watchedErrorCount, 2, "The error count was expected to be 2 for the file change");
6464
});
6565
});
66+
67+
describe("unittests:: tsc-watch:: watchAPI:: when watchHost does not implement setTimeout or clearTimeout", () => {
68+
it("verifies that getProgram gets updated program if new file is added to the program", () => {
69+
const config: File = {
70+
path: `${projectRoot}/tsconfig.json`,
71+
content: "{}"
72+
};
73+
const mainFile: File = {
74+
path: `${projectRoot}/main.ts`,
75+
content: "const x = 10;"
76+
};
77+
const sys = createWatchedSystem([config, mainFile, libFile]);
78+
const watchCompilerHost = createWatchCompilerHost(config.path, {}, sys);
79+
watchCompilerHost.setTimeout = undefined;
80+
watchCompilerHost.clearTimeout = undefined;
81+
const watch = createWatchProgram(watchCompilerHost);
82+
checkProgramActualFiles(watch.getProgram().getProgram(), [mainFile.path, libFile.path]);
83+
// Write new file
84+
const barPath = `${projectRoot}/bar.ts`;
85+
sys.writeFile(barPath, "const y =10;");
86+
checkProgramActualFiles(watch.getProgram().getProgram(), [mainFile.path, barPath, libFile.path]);
87+
});
88+
});
6689
}

0 commit comments

Comments
 (0)