Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle WatchCompilerHost without timeout methods to retrieve correct program #37308

Merged
merged 1 commit into from Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/compiler/watchPublic.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -553,7 +553,7 @@ namespace ts {
host.clearTimeout(timerToUpdateProgram);
}
writeLog("Scheduling update");
timerToUpdateProgram = host.setTimeout(updateProgram, 250);
timerToUpdateProgram = host.setTimeout(updateProgramWithWatchStatus, 250);
}

function scheduleProgramReload() {
Expand All @@ -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");
Expand All @@ -581,6 +584,7 @@ namespace ts {
break;
}
perfLogger.logStopUpdateProgram("Done");
return getCurrentBuilderProgram();
}

function reloadFileNamesFromConfigFile() {
Expand Down
23 changes: 23 additions & 0 deletions src/testRunner/unittests/tscWatch/watchApi.ts
Expand Up @@ -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]);
});
});
}