From 3ace11ac38027a4ab33ae93d35869c1a5daa814e Mon Sep 17 00:00:00 2001 From: emerard <113128214+emerardd@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:08:01 +0800 Subject: [PATCH] Coalesce concurrent async source file requests --- packages/typescript/src/api/async/api.ts | 15 +++++++++++ packages/typescript/src/api/sync/api.ts | 31 +++++++++++++++++----- packages/typescript/test/async/api.test.ts | 28 +++++++++++++++++++ 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/packages/typescript/src/api/async/api.ts b/packages/typescript/src/api/async/api.ts index 6042da5009d04..85b56e61bf014 100644 --- a/packages/typescript/src/api/async/api.ts +++ b/packages/typescript/src/api/async/api.ts @@ -1816,6 +1816,7 @@ export class Program implements FormatDiagnost private readonly toPath: (fileName: string, basePath?: string) => PathKey; private readonly decoder = new Wtf8Decoder(); private readonly sourceFileMetadataCache = new Map>(); + private readonly pendingSourceFiles = new Map>(); // @sync-skip private ownedSnapshot: Snapshot | undefined; private disposePromise: Promise | undefined; @@ -1906,6 +1907,20 @@ export class Program implements FormatDiagnost return retained as unknown as SourceFile; } + // @sync-skip-block-start + let pending = this.pendingSourceFiles.get(path); + if (!pending) { + pending = this.fetchSourceFile(file).finally(() => this.pendingSourceFiles.delete(path)); + this.pendingSourceFiles.set(path, pending); + } + return pending; + // @sync-skip-block-end + // @sync-only-start + // return this.fetchSourceFile(file); + // @sync-only-end + } + + private async fetchSourceFile(file: DocumentIdentifier): Promise { // Fetch from server const binaryData = await this.client.apiRequestBinary("getSourceFile", { snapshot: this.snapshotId, diff --git a/packages/typescript/src/api/sync/api.ts b/packages/typescript/src/api/sync/api.ts index adb09a2a9b2cc..67c31b3027c22 100644 --- a/packages/typescript/src/api/sync/api.ts +++ b/packages/typescript/src/api/sync/api.ts @@ -3246,6 +3246,29 @@ export class Program implements FormatDiagnost return retained as unknown as SourceFile; } + return owner.fetchSourceFile(file); + }, + function* (file: DocumentIdentifier, path: PathKey): Generator { + // Check if we already have a retained cache entry for this (snapshot, project) pair + const retained = owner.sourceFileCache.getRetained(path, owner.snapshotId, owner.project.id); + if (retained) { + return retained as unknown as SourceFile; + } + + return yield* owner.fetchSourceFile.gen(file); + }, + ); + } + + private get fetchSourceFile(): { + (file: DocumentIdentifier): SourceFile | undefined; + gen(file: DocumentIdentifier): Generator; + } { + const owner = this; + return cacheGeneratorMethod( + owner, + "fetchSourceFile", + function (file: DocumentIdentifier): SourceFile | undefined { // Fetch from server const binaryData = owner.client.apiRequestBinary("getSourceFile", { snapshot: owner.snapshotId, @@ -3260,13 +3283,7 @@ export class Program implements FormatDiagnost const decoded = new RemoteSourceFile(binaryData, owner.decoder, owner.client.getTimingCollector(), owner.project.api); return owner.sourceFileCache.set(decoded, owner.snapshotId, owner.project.id) as unknown as SourceFile; }, - function* (file: DocumentIdentifier, path: PathKey): Generator { - // Check if we already have a retained cache entry for this (snapshot, project) pair - const retained = owner.sourceFileCache.getRetained(path, owner.snapshotId, owner.project.id); - if (retained) { - return retained as unknown as SourceFile; - } - + function* (file: DocumentIdentifier): Generator { // Fetch from server const binaryData = sourceFileResponseToUint8Array( yield* apiRequest("getSourceFile", { diff --git a/packages/typescript/test/async/api.test.ts b/packages/typescript/test/async/api.test.ts index 5e7083d2be396..449af9795aa6e 100644 --- a/packages/typescript/test/async/api.test.ts +++ b/packages/typescript/test/async/api.test.ts @@ -1987,6 +1987,34 @@ declare module "augmentation" {}`, // @sync-skip-block-start describe("API - automatic batching", { concurrency }, () => { + test("shares concurrent Program source file requests and retries failures", async context => { + const file = "/src/main.ts"; + const text = "export const value = 1;"; + await using api = spawnAPI({ [file]: text }); + await using program = await api.createProgram([file], { noLib: true }); + // @ts-ignore -- Internal client requests are exercised by source tests. + const client = api["client"]; + const request = context.mock.method(client, "apiRequestBinary"); + const error = new Error("Source file request failed"); + request.mock.mockImplementationOnce(() => Promise.reject(error)); + + await Promise.all([ + assert.rejects(program.getSourceFile(file), error), + assert.rejects(program.getSourceFile("/src/./main.ts"), error), + ]); + assert.equal(request.mock.callCount(), 1); + + const [sourceFile, repeated] = await Promise.all([ + program.getSourceFile(file), + program.getSourceFile("/src/./main.ts"), + ]); + assert.equal(request.mock.callCount(), 2); + assert.equal(sourceFile?.text, text); + assert.strictEqual(repeated, sourceFile); + assert.strictEqual(await program.getSourceFile(file), sourceFile); + assert.equal(request.mock.callCount(), 2); + }); + test("fetches Program statistics for every request", async context => { await using api = spawnAPI({ "/src/main.ts": "export const value = 1;" }); await using program = await api.createProgram(["/src/main.ts"], { noLib: true });