Skip to content

Commit af20e79

Browse files
authored
Merge pull request microsoft#32167 from microsoft/tryReuseProgram
When checking sourceFile is from external library, use sourceFile.resolvedPath since thats how the source files are queried and thats the real path
2 parents 4382d7e + 4f802e7 commit af20e79

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ namespace ts {
13951395
const filePath = newSourceFile.path;
13961396
addFileToFilesByName(newSourceFile, filePath, newSourceFile.resolvedPath);
13971397
// Set the file as found during node modules search if it was found that way in old progra,
1398-
if (oldProgram.isSourceFileFromExternalLibrary(oldProgram.getSourceFileByPath(filePath)!)) {
1398+
if (oldProgram.isSourceFileFromExternalLibrary(oldProgram.getSourceFileByPath(newSourceFile.resolvedPath)!)) {
13991399
sourceFilesFoundSearchingNodeModules.set(filePath, true);
14001400
}
14011401
}

src/testRunner/unittests/tsserver/projectReferences.ts

+75
Original file line numberDiff line numberDiff line change
@@ -706,5 +706,80 @@ fn5();
706706
verifyScenarios(/*withRefs*/ false);
707707
verifyScenarios(/*withRefs*/ true);
708708
});
709+
710+
it("reusing d.ts files from composite and non composite projects", () => {
711+
const projectLocation = "/user/username/projects/myproject";
712+
const configA: File = {
713+
path: `${projectLocation}/compositea/tsconfig.json`,
714+
content: JSON.stringify({
715+
compilerOptions: {
716+
composite: true,
717+
outDir: "../dist/",
718+
rootDir: "../",
719+
baseUrl: "../",
720+
paths: { "@ref/*": ["./dist/*"] }
721+
}
722+
})
723+
};
724+
const aTs: File = {
725+
path: `${projectLocation}/compositea/a.ts`,
726+
content: `import { b } from "@ref/compositeb/b";`
727+
};
728+
const a2Ts: File = {
729+
path: `${projectLocation}/compositea/a2.ts`,
730+
content: `export const x = 10;`
731+
};
732+
const configB: File = {
733+
path: `${projectLocation}/compositeb/tsconfig.json`,
734+
content: configA.content
735+
};
736+
const bTs: File = {
737+
path: `${projectLocation}/compositeb/b.ts`,
738+
content: "export function b() {}"
739+
};
740+
const bDts: File = {
741+
path: `${projectLocation}/dist/compositeb/b.d.ts`,
742+
content: "export declare function b(): void;"
743+
};
744+
const configC: File = {
745+
path: `${projectLocation}/compositec/tsconfig.json`,
746+
content: JSON.stringify({
747+
compilerOptions: {
748+
composite: true,
749+
outDir: "../dist/",
750+
rootDir: "../",
751+
baseUrl: "../",
752+
paths: { "@ref/*": ["./*"] }
753+
},
754+
references: [{ path: "../compositeb" }]
755+
})
756+
};
757+
const cTs: File = {
758+
path: `${projectLocation}/compositec/c.ts`,
759+
content: aTs.content
760+
};
761+
const files = [libFile, aTs, a2Ts, configA, bDts, bTs, configB, cTs, configC];
762+
const host = createServerHost(files);
763+
const service = createProjectService(host);
764+
service.openClientFile(aTs.path);
765+
service.checkNumberOfProjects({ configuredProjects: 1 });
766+
767+
// project A referencing b.d.ts without project reference
768+
const projectA = service.configuredProjects.get(configA.path)!;
769+
assert.isDefined(projectA);
770+
checkProjectActualFiles(projectA, [aTs.path, a2Ts.path, bDts.path, libFile.path, configA.path]);
771+
772+
// reuses b.d.ts but sets the path and resolved path since projectC has project references
773+
// as the real resolution was to b.ts
774+
service.openClientFile(cTs.path);
775+
service.checkNumberOfProjects({ configuredProjects: 2 });
776+
const projectC = service.configuredProjects.get(configC.path)!;
777+
checkProjectActualFiles(projectC, [cTs.path, bDts.path, libFile.path, configC.path]);
778+
779+
// Now new project for project A tries to reuse b but there is no filesByName mapping for b's source location
780+
host.writeFile(a2Ts.path, `${a2Ts.content}export const y = 30;`);
781+
assert.isTrue(projectA.dirty);
782+
projectA.updateGraph();
783+
});
709784
});
710785
}

0 commit comments

Comments
 (0)