Skip to content

Commit 4f802e7

Browse files
committed
When checking sourceFile is from external library, use sourceFile.resolvedPath since thats how the source files are queried and thats the real path
Fixes #32086
1 parent b32afb5 commit 4f802e7

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,5 +674,80 @@ fn5();
674674
);
675675
});
676676
});
677+
678+
it("reusing d.ts files from composite and non composite projects", () => {
679+
const projectLocation = "/user/username/projects/myproject";
680+
const configA: File = {
681+
path: `${projectLocation}/compositea/tsconfig.json`,
682+
content: JSON.stringify({
683+
compilerOptions: {
684+
composite: true,
685+
outDir: "../dist/",
686+
rootDir: "../",
687+
baseUrl: "../",
688+
paths: { "@ref/*": ["./dist/*"] }
689+
}
690+
})
691+
};
692+
const aTs: File = {
693+
path: `${projectLocation}/compositea/a.ts`,
694+
content: `import { b } from "@ref/compositeb/b";`
695+
};
696+
const a2Ts: File = {
697+
path: `${projectLocation}/compositea/a2.ts`,
698+
content: `export const x = 10;`
699+
};
700+
const configB: File = {
701+
path: `${projectLocation}/compositeb/tsconfig.json`,
702+
content: configA.content
703+
};
704+
const bTs: File = {
705+
path: `${projectLocation}/compositeb/b.ts`,
706+
content: "export function b() {}"
707+
};
708+
const bDts: File = {
709+
path: `${projectLocation}/dist/compositeb/b.d.ts`,
710+
content: "export declare function b(): void;"
711+
};
712+
const configC: File = {
713+
path: `${projectLocation}/compositec/tsconfig.json`,
714+
content: JSON.stringify({
715+
compilerOptions: {
716+
composite: true,
717+
outDir: "../dist/",
718+
rootDir: "../",
719+
baseUrl: "../",
720+
paths: { "@ref/*": ["./*"] }
721+
},
722+
references: [{ path: "../compositeb" }]
723+
})
724+
};
725+
const cTs: File = {
726+
path: `${projectLocation}/compositec/c.ts`,
727+
content: aTs.content
728+
};
729+
const files = [libFile, aTs, a2Ts, configA, bDts, bTs, configB, cTs, configC];
730+
const host = createServerHost(files);
731+
const service = createProjectService(host);
732+
service.openClientFile(aTs.path);
733+
service.checkNumberOfProjects({ configuredProjects: 1 });
734+
735+
// project A referencing b.d.ts without project reference
736+
const projectA = service.configuredProjects.get(configA.path)!;
737+
assert.isDefined(projectA);
738+
checkProjectActualFiles(projectA, [aTs.path, a2Ts.path, bDts.path, libFile.path, configA.path]);
739+
740+
// reuses b.d.ts but sets the path and resolved path since projectC has project references
741+
// as the real resolution was to b.ts
742+
service.openClientFile(cTs.path);
743+
service.checkNumberOfProjects({ configuredProjects: 2 });
744+
const projectC = service.configuredProjects.get(configC.path)!;
745+
checkProjectActualFiles(projectC, [cTs.path, bDts.path, libFile.path, configC.path]);
746+
747+
// Now new project for project A tries to reuse b but there is no filesByName mapping for b's source location
748+
host.writeFile(a2Ts.path, `${a2Ts.content}export const y = 30;`);
749+
assert.isTrue(projectA.dirty);
750+
projectA.updateGraph();
751+
});
677752
});
678753
}

0 commit comments

Comments
 (0)