From e68524a8d2f26efdcc123ff58aa42bf8f6a81bf5 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 9 Mar 2020 16:30:52 -0700 Subject: [PATCH] Handle WatchCompilerHost without timeout methods to retrieve correct Program (#37308) --- src/compiler/watchPublic.ts | 12 ++++++---- src/testRunner/unittests/tscWatch/watchApi.ts | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/compiler/watchPublic.ts b/src/compiler/watchPublic.ts index 21464f6929fe4..c5f3928adef9d 100644 --- a/src/compiler/watchPublic.ts +++ b/src/compiler/watchPublic.ts @@ -320,8 +320,8 @@ namespace ts { watchConfigFileWildCardDirectories(); return configFileName ? - { getCurrentProgram: getCurrentBuilderProgram, getProgram: synchronizeProgram, close } : - { getCurrentProgram: getCurrentBuilderProgram, getProgram: synchronizeProgram, updateRootFileNames, close }; + { getCurrentProgram: getCurrentBuilderProgram, getProgram: updateProgram, close } : + { getCurrentProgram: getCurrentBuilderProgram, getProgram: updateProgram, updateRootFileNames, close }; function close() { resolutionCache.clear(); @@ -553,7 +553,7 @@ namespace ts { host.clearTimeout(timerToUpdateProgram); } writeLog("Scheduling update"); - timerToUpdateProgram = host.setTimeout(updateProgram, 250); + timerToUpdateProgram = host.setTimeout(updateProgramWithWatchStatus, 250); } function scheduleProgramReload() { @@ -562,10 +562,13 @@ namespace ts { scheduleProgramUpdate(); } - function updateProgram() { + function updateProgramWithWatchStatus() { timerToUpdateProgram = undefined; reportWatchDiagnostic(Diagnostics.File_change_detected_Starting_incremental_compilation); + updateProgram(); + } + function updateProgram() { switch (reloadLevel) { case ConfigFileProgramReloadLevel.Partial: perfLogger.logStartUpdateProgram("PartialConfigReload"); @@ -581,6 +584,7 @@ namespace ts { break; } perfLogger.logStopUpdateProgram("Done"); + return getCurrentBuilderProgram(); } function reloadFileNamesFromConfigFile() { diff --git a/src/testRunner/unittests/tscWatch/watchApi.ts b/src/testRunner/unittests/tscWatch/watchApi.ts index 716fc162261f0..8d33825fa7461 100644 --- a/src/testRunner/unittests/tscWatch/watchApi.ts +++ b/src/testRunner/unittests/tscWatch/watchApi.ts @@ -63,4 +63,27 @@ namespace ts.tscWatch { assert.equal(watchedErrorCount, 2, "The error count was expected to be 2 for the file change"); }); }); + + describe("unittests:: tsc-watch:: watchAPI:: when watchHost does not implement setTimeout or clearTimeout", () => { + it("verifies that getProgram gets updated program if new file is added to the program", () => { + const config: File = { + path: `${projectRoot}/tsconfig.json`, + content: "{}" + }; + const mainFile: File = { + path: `${projectRoot}/main.ts`, + content: "const x = 10;" + }; + const sys = createWatchedSystem([config, mainFile, libFile]); + const watchCompilerHost = createWatchCompilerHost(config.path, {}, sys); + watchCompilerHost.setTimeout = undefined; + watchCompilerHost.clearTimeout = undefined; + const watch = createWatchProgram(watchCompilerHost); + checkProgramActualFiles(watch.getProgram().getProgram(), [mainFile.path, libFile.path]); + // Write new file + const barPath = `${projectRoot}/bar.ts`; + sys.writeFile(barPath, "const y =10;"); + checkProgramActualFiles(watch.getProgram().getProgram(), [mainFile.path, barPath, libFile.path]); + }); + }); }