Skip to content

Commit

Permalink
Fix document registry cache key calculation for paths compiler opti…
Browse files Browse the repository at this point in the history
…on (#48389)

* Fix document registry cache key calculation for `paths` compiler option

* PR feedback
weswigham authored Mar 23, 2022
1 parent 20c01cd commit 26c701c
Showing 2 changed files with 67 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/services/documentRegistry.ts
Original file line number Diff line number Diff line change
@@ -357,7 +357,23 @@ namespace ts {
};
}

function compilerOptionValueToString(value: unknown): string {
if (value === null || typeof value !== "object") { // eslint-disable-line no-null/no-null
return "" + value;
}
if (isArray(value)) {
return `[${map(value, e => compilerOptionValueToString(e))?.join(",")}]`;
}
let str = "{";
for (const key in value) {
if (ts.hasOwnProperty.call(value, key)) { // eslint-disable-line @typescript-eslint/no-unnecessary-qualifier
str += `${key}: ${compilerOptionValueToString((value as any)[key])}`;
}
}
return str + "}";
}

function getKeyForCompilationSettings(settings: CompilerOptions): DocumentRegistryBucketKey {
return sourceFileAffectingCompilerOptions.map(option => getCompilerOptionValue(settings, option)).join("|") as DocumentRegistryBucketKey;
return sourceFileAffectingCompilerOptions.map(option => compilerOptionValueToString(getCompilerOptionValue(settings, option))).join("|") + (settings.pathsBasePath ? `|${settings.pathsBasePath}` : undefined) as DocumentRegistryBucketKey;
}
}
51 changes: 50 additions & 1 deletion src/testRunner/unittests/tsserver/languageService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace ts.projectSystem {
describe("unittests:: tsserver:: Language service", () => {
describe("unittests:: tsserver:: languageService", () => {
it("should work correctly on case-sensitive file systems", () => {
const lib = {
path: "/a/Lib/lib.d.ts",
@@ -15,5 +15,54 @@ namespace ts.projectSystem {
projectService.checkNumberOfProjects({ inferredProjects: 1 });
projectService.inferredProjects[0].getLanguageService().getProgram();
});

it("should support multiple projects with the same file under differing `paths` settings", () => {
const files = [
{
path: "/project/shared.ts",
content: Utils.dedent`
import {foo_a} from "foo";
`
},
{
path: `/project/a/tsconfig.json`,
content: `{ "compilerOptions": { "paths": { "foo": ["./foo.d.ts"] } }, "files": ["./index.ts", "./foo.d.ts"] }`
},
{
path: `/project/a/foo.d.ts`,
content: Utils.dedent`
export const foo_a = 1;
`
},
{
path: "/project/a/index.ts",
content: `import "../shared";`
},
{
path: `/project/b/tsconfig.json`,
content: `{ "compilerOptions": { "paths": { "foo": ["./foo.d.ts"] } }, "files": ["./index.ts", "./foo.d.ts"] }`
},
{
path: `/project/b/foo.d.ts`,
content: Utils.dedent`
export const foo_b = 1;
`
},
{
path: "/project/b/index.ts",
content: `import "../shared";`
}
];

const host = createServerHost(files, { executingFilePath: "/project/tsc.js", useCaseSensitiveFileNames: true });
const projectService = createProjectService(host);
projectService.openClientFile(files[3].path);
projectService.openClientFile(files[6].path);
projectService.checkNumberOfProjects({ configuredProjects: 2 });
const proj1Diags = projectService.configuredProjects.get(files[1].path)!.getLanguageService().getProgram()!.getSemanticDiagnostics();
Debug.assertEqual(proj1Diags.length, 0);
const proj2Diags = projectService.configuredProjects.get(files[4].path)!.getLanguageService().getProgram()!.getSemanticDiagnostics();
Debug.assertEqual(proj2Diags.length, 1);
});
});
}

0 comments on commit 26c701c

Please sign in to comment.