Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions packages/typescript/src/api/async/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,7 @@ export class Program<Id extends ProjectId = ProjectId> implements FormatDiagnost
private readonly project: Project<Id>;
private readonly toPath: (fileName: string, basePath?: string) => PathKey;
private readonly decoder = new Wtf8Decoder();
private readonly sourceFileRequests = new Map<PathKey, Promise<SourceFile | undefined>>();
private readonly sourceFileMetadataCache = new Map<PathKey, Promise<SourceFileMetadata | undefined>>();
private ownedSnapshot: Snapshot | undefined;
private disposePromise: Promise<void> | undefined;
Expand Down Expand Up @@ -1823,7 +1824,21 @@ export class Program<Id extends ProjectId = ProjectId> implements FormatDiagnost
async getSourceFile(file: DocumentIdentifier): Promise<SourceFile | undefined> {
const fileName = resolveFileName(file);
const path = this.pathKeyForFileName(fileName);
return this.getSourceFileWorker(file, path);
return this.getSourceFileRequest(file, path);
}

private getSourceFileRequest(file: DocumentIdentifier, path: PathKey): Promise<SourceFile | undefined> {
const retained = this.sourceFileCache.getRetained(path, this.snapshotId, this.project.id);
if (retained) {
return Promise.resolve(retained as unknown as SourceFile);
}

let request = this.sourceFileRequests.get(path);
if (request === undefined) {
request = this.getSourceFileWorker(file, path).finally(() => this.sourceFileRequests.delete(path));
this.sourceFileRequests.set(path, request);
}
return request;
}

/**
Expand All @@ -1832,16 +1847,10 @@ export class Program<Id extends ProjectId = ProjectId> implements FormatDiagnost
getSourceFileByPath(path: PathKey): Promise<SourceFile | undefined> {
// The wire format is a string, but the cache key remains the supplied
// PathKey and is never treated as a RootedPath.
return this.getSourceFileWorker(path, path);
return this.getSourceFileRequest(path, path);
}

private async getSourceFileWorker(file: DocumentIdentifier, path: PathKey): Promise<SourceFile | undefined> {
// Check if we already have a retained cache entry for this (snapshot, project) pair
const retained = this.sourceFileCache.getRetained(path, this.snapshotId, this.project.id);
if (retained) {
return retained as unknown as SourceFile;
}

// Fetch from server
const binaryData = await this.client.apiRequestBinary("getSourceFile", {
snapshot: this.snapshotId,
Expand Down
28 changes: 28 additions & 0 deletions packages/typescript/test/async/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ import {
test,
} from "node:test";
import { fileURLToPath } from "node:url";
import { Program as AsyncProgram } from "../../src/api/async/api.ts";
import {
getNodeId,
parseNodeHandleFromCompiler,
Expand All @@ -169,6 +170,33 @@ import {

const concurrency = areTestsFiltered();

describe("Program source file requests", { concurrency }, () => {
test("concurrent getSourceFile calls share an in-flight request", async () => {
let resolveFetch!: (value: undefined) => void;
const fetch = new Promise<undefined>(resolve => resolveFetch = resolve);
let requestCount = 0;
const client = {
apiRequestBinary: async () => {
requestCount++;
return fetch;
},
};
const sourceFileCache = { getRetained: () => undefined };
const project = {
id: "project",
handle: "project",
api: { client, sourceFileCache },
} as unknown as Project;
const program = new AsyncProgram(1, project, fileName => fileName as PathKey);

const calls = Array.from({ length: 10 }, () => program.getSourceFile("/same.ts"));
assert.equal(requestCount, 1);
resolveFetch(undefined);
assert.deepEqual(await Promise.all(calls), Array(10).fill(undefined));
assert.equal(requestCount, 1);
});
});

describe("API", { concurrency }, () => {
test("getCurrentLanguageServerSnapshot is LSP-only", async () => {
if (!!false) {
Expand Down