From 068f1883a84708048c0541f412c87886d591ddac Mon Sep 17 00:00:00 2001 From: zhengdaodeguL <1159957339@qq.com> Date: Sun, 15 Nov 2026 20:29:10 +0800 Subject: [PATCH] fix: share in-flight getSourceFile requests in native-preview async API (#65503) --- packages/typescript/src/api/async/api.ts | 25 ++++++++++++------- packages/typescript/test/async/api.test.ts | 28 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/packages/typescript/src/api/async/api.ts b/packages/typescript/src/api/async/api.ts index 7256e47c0da36..aa608b7131bd7 100644 --- a/packages/typescript/src/api/async/api.ts +++ b/packages/typescript/src/api/async/api.ts @@ -1751,6 +1751,7 @@ export class Program implements FormatDiagnost private readonly project: Project; private readonly toPath: (fileName: string, basePath?: string) => PathKey; private readonly decoder = new Wtf8Decoder(); + private readonly sourceFileRequests = new Map>(); private readonly sourceFileMetadataCache = new Map>(); private ownedSnapshot: Snapshot | undefined; private disposePromise: Promise | undefined; @@ -1823,7 +1824,21 @@ export class Program implements FormatDiagnost async getSourceFile(file: DocumentIdentifier): Promise { 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 { + 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; } /** @@ -1832,16 +1847,10 @@ export class Program implements FormatDiagnost getSourceFileByPath(path: PathKey): Promise { // 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 { - // 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, diff --git a/packages/typescript/test/async/api.test.ts b/packages/typescript/test/async/api.test.ts index 3f303868a73cc..542db123351ac 100644 --- a/packages/typescript/test/async/api.test.ts +++ b/packages/typescript/test/async/api.test.ts @@ -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, @@ -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(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) {