Skip to content

Commit 26c701c

Browse files
authored
Fix document registry cache key calculation for paths compiler option (microsoft#48389)
* Fix document registry cache key calculation for `paths` compiler option * PR feedback
1 parent 20c01cd commit 26c701c

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/services/documentRegistry.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,23 @@ namespace ts {
357357
};
358358
}
359359

360+
function compilerOptionValueToString(value: unknown): string {
361+
if (value === null || typeof value !== "object") { // eslint-disable-line no-null/no-null
362+
return "" + value;
363+
}
364+
if (isArray(value)) {
365+
return `[${map(value, e => compilerOptionValueToString(e))?.join(",")}]`;
366+
}
367+
let str = "{";
368+
for (const key in value) {
369+
if (ts.hasOwnProperty.call(value, key)) { // eslint-disable-line @typescript-eslint/no-unnecessary-qualifier
370+
str += `${key}: ${compilerOptionValueToString((value as any)[key])}`;
371+
}
372+
}
373+
return str + "}";
374+
}
375+
360376
function getKeyForCompilationSettings(settings: CompilerOptions): DocumentRegistryBucketKey {
361-
return sourceFileAffectingCompilerOptions.map(option => getCompilerOptionValue(settings, option)).join("|") as DocumentRegistryBucketKey;
377+
return sourceFileAffectingCompilerOptions.map(option => compilerOptionValueToString(getCompilerOptionValue(settings, option))).join("|") + (settings.pathsBasePath ? `|${settings.pathsBasePath}` : undefined) as DocumentRegistryBucketKey;
362378
}
363379
}

src/testRunner/unittests/tsserver/languageService.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace ts.projectSystem {
2-
describe("unittests:: tsserver:: Language service", () => {
2+
describe("unittests:: tsserver:: languageService", () => {
33
it("should work correctly on case-sensitive file systems", () => {
44
const lib = {
55
path: "/a/Lib/lib.d.ts",
@@ -15,5 +15,54 @@ namespace ts.projectSystem {
1515
projectService.checkNumberOfProjects({ inferredProjects: 1 });
1616
projectService.inferredProjects[0].getLanguageService().getProgram();
1717
});
18+
19+
it("should support multiple projects with the same file under differing `paths` settings", () => {
20+
const files = [
21+
{
22+
path: "/project/shared.ts",
23+
content: Utils.dedent`
24+
import {foo_a} from "foo";
25+
`
26+
},
27+
{
28+
path: `/project/a/tsconfig.json`,
29+
content: `{ "compilerOptions": { "paths": { "foo": ["./foo.d.ts"] } }, "files": ["./index.ts", "./foo.d.ts"] }`
30+
},
31+
{
32+
path: `/project/a/foo.d.ts`,
33+
content: Utils.dedent`
34+
export const foo_a = 1;
35+
`
36+
},
37+
{
38+
path: "/project/a/index.ts",
39+
content: `import "../shared";`
40+
},
41+
{
42+
path: `/project/b/tsconfig.json`,
43+
content: `{ "compilerOptions": { "paths": { "foo": ["./foo.d.ts"] } }, "files": ["./index.ts", "./foo.d.ts"] }`
44+
},
45+
{
46+
path: `/project/b/foo.d.ts`,
47+
content: Utils.dedent`
48+
export const foo_b = 1;
49+
`
50+
},
51+
{
52+
path: "/project/b/index.ts",
53+
content: `import "../shared";`
54+
}
55+
];
56+
57+
const host = createServerHost(files, { executingFilePath: "/project/tsc.js", useCaseSensitiveFileNames: true });
58+
const projectService = createProjectService(host);
59+
projectService.openClientFile(files[3].path);
60+
projectService.openClientFile(files[6].path);
61+
projectService.checkNumberOfProjects({ configuredProjects: 2 });
62+
const proj1Diags = projectService.configuredProjects.get(files[1].path)!.getLanguageService().getProgram()!.getSemanticDiagnostics();
63+
Debug.assertEqual(proj1Diags.length, 0);
64+
const proj2Diags = projectService.configuredProjects.get(files[4].path)!.getLanguageService().getProgram()!.getSemanticDiagnostics();
65+
Debug.assertEqual(proj2Diags.length, 1);
66+
});
1867
});
1968
}

0 commit comments

Comments
 (0)