mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-04-13 18:14:48 -05:00
Fix missing lib files in reused programs (#63239)
This commit is contained in:
@@ -2555,6 +2555,9 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
|
||||
Debug.assert(newSourceFiles.length === oldProgram.getSourceFiles().length);
|
||||
for (const newSourceFile of newSourceFiles) {
|
||||
filesByName.set(newSourceFile.path, newSourceFile);
|
||||
if (oldProgram.isSourceFileDefaultLibrary(newSourceFile)) {
|
||||
libFiles.add(newSourceFile.path);
|
||||
}
|
||||
}
|
||||
const oldFilesByNameMap = oldProgram.getFilesByNameMap();
|
||||
oldFilesByNameMap.forEach((oldFile, path) => {
|
||||
|
||||
@@ -769,6 +769,35 @@ describe("unittests:: reuseProgramStructure:: General", () => {
|
||||
baselineDiagnostics(baselines, program4);
|
||||
runBaseline("handles file preprocessing dignostics when diagnostics are not queried", baselines);
|
||||
});
|
||||
|
||||
it("isSourceFileDefaultLibrary is preserved after program reuse", () => {
|
||||
const libFile = { name: "/lib.d.ts", text: SourceText.New("", "", "declare var console: any;") };
|
||||
const mainFile = { name: "/main.ts", text: SourceText.New("", "", "var x = 1;") };
|
||||
const files = [libFile, mainFile];
|
||||
|
||||
const host = createTestCompilerHost(files, target);
|
||||
host.getDefaultLibFileName = () => "/lib.d.ts";
|
||||
|
||||
const options: ts.CompilerOptions = { target };
|
||||
const program1 = ts.createProgram(["/main.ts"], options, host) as ProgramWithSourceTexts;
|
||||
program1.sourceTexts = files;
|
||||
program1.host = host;
|
||||
program1.version = 1;
|
||||
|
||||
const libSourceFile1 = program1.getSourceFile("/lib.d.ts")!;
|
||||
assert.isDefined(libSourceFile1, "lib file should exist in program 1");
|
||||
assert.isTrue(program1.isSourceFileDefaultLibrary(libSourceFile1), "lib file should be a default library in program 1");
|
||||
|
||||
// Update main file only (code change) -> should trigger complete structure reuse
|
||||
mainFile.text = mainFile.text.updateProgram("var x = 2;");
|
||||
const host2 = createTestCompilerHost(files, target, program1);
|
||||
host2.getDefaultLibFileName = () => "/lib.d.ts";
|
||||
const program2 = ts.createProgram(["/main.ts"], options, host2, program1);
|
||||
|
||||
const libSourceFile2 = program2.getSourceFile("/lib.d.ts")!;
|
||||
assert.isDefined(libSourceFile2, "lib file should exist in program 2");
|
||||
assert.isTrue(program2.isSourceFileDefaultLibrary(libSourceFile2), "lib file should still be a default library in program 2 after reuse");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unittests:: reuseProgramStructure:: host is optional", () => {
|
||||
|
||||
Reference in New Issue
Block a user