From f606283f346a1f36a571a96c9190781d7c1e59f0 Mon Sep 17 00:00:00 2001 From: um4tb Date: Sat, 21 Nov 2026 20:46:26 +0300 Subject: [PATCH] fix(typescript-vfs): avoid storing duplicate SourceFiles in language service host --- packages/typescript-vfs/src/index.ts | 18 ++++++++---------- packages/typescript-vfs/test/index.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index cf1b4decebb6..cf8115298e1a 100644 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -90,7 +90,7 @@ export function createVirtualTypeScriptEnvironment( getSourceFile: fileName => languageService.getProgram()?.getSourceFile(fileName), createFile: (fileName, content) => { - updateFile(ts.createSourceFile(fileName, content, mergedCompilerOpts.target!, false)) + updateFile({ fileName, text: content } as SourceFile) }, updateFile: (fileName, content, optPrevTextSpan) => { const prevSourceFile = languageService.getProgram()!.getSourceFile(fileName) @@ -105,12 +105,8 @@ export function createVirtualTypeScriptEnvironment( prevFullContents.slice(0, prevTextSpan.start) + content + prevFullContents.slice(prevTextSpan.start + prevTextSpan.length) - const newSourceFile = ts.updateSourceFile(prevSourceFile, newText, { - span: prevTextSpan, - newLength: content.length, - }) - updateFile(newSourceFile) + updateFile({ fileName, text: newText } as SourceFile) }, deleteFile(fileName) { const sourceFile = languageService.getProgram()!.getSourceFile(fileName) @@ -681,11 +677,13 @@ export function createVirtualLanguageServiceHost( customTransformers?: CustomTransformers ) { const fileNames = [...rootFiles] - const { compilerHost, updateFile, deleteFile } = createVirtualCompilerHost(sys, compilerOptions, ts) const fileVersions = new Map() let projectVersion = 0 const languageServiceHost: LanguageServiceHost = { - ...compilerHost, + ...sys, + getDefaultLibFileName: () => "/" + ts.getDefaultLibFileName(compilerOptions), + getNewLine: () => sys.newLine, + useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames, getProjectVersion: () => projectVersion.toString(), getCompilationSettings: () => compilerOptions, getCustomTransformers: () => customTransformers, @@ -725,7 +723,7 @@ export function createVirtualLanguageServiceHost( if (!fileNames.includes(sourceFile.fileName)) { fileNames.push(sourceFile.fileName) } - updateFile(sourceFile) + sys.writeFile(sourceFile.fileName, sourceFile.text) }, deleteFile: sourceFile => { projectVersion++ @@ -734,7 +732,7 @@ export function createVirtualLanguageServiceHost( if (index !== -1) { fileNames.splice(index, 1) } - deleteFile(sourceFile) + sys.deleteFile!(sourceFile.fileName) } } return lsHost diff --git a/packages/typescript-vfs/test/index.test.ts b/packages/typescript-vfs/test/index.test.ts index 0ba7ed6fde09..53a11691de69 100644 --- a/packages/typescript-vfs/test/index.test.ts +++ b/packages/typescript-vfs/test/index.test.ts @@ -245,3 +245,25 @@ it("moduleDetection options", async () => { program.emit() expect(fsMap.get("index.js")).toEqual(`define(["require", "exports"], function (require, exports) {\n "use strict";\n Object.defineProperty(exports, "__esModule", { value: true });\n let foo = 'foo';\n});\n`) }) + +it("createFile and updateFile do not parse duplicate SourceFiles outside the language service", () => { + const fsMap = createDefaultMapFromNodeModules({}) + const system = createSystem(fsMap) + const createSourceFile = jest.fn(ts.createSourceFile) + const updateSourceFile = jest.fn(ts.updateSourceFile) + const tsProxy = { ...ts, createSourceFile, updateSourceFile } + const env = createVirtualTypeScriptEnvironment(system, [], tsProxy, {}) + + env.createFile("a.ts", "const x: number = 1") + expect(createSourceFile).not.toHaveBeenCalled() + expect(env.getSourceFile("a.ts")?.text).toBe("const x: number = 1") + + env.updateFile("a.ts", "const x: number = 2") + expect(updateSourceFile).not.toHaveBeenCalled() + expect(env.getSourceFile("a.ts")?.text).toBe("const x: number = 2") + + env.updateFile("a.ts", "3", ts.createTextSpan("const x: number = ".length, 1)) + expect(updateSourceFile).not.toHaveBeenCalled() + expect(env.getSourceFile("a.ts")?.text).toBe("const x: number = 3") + expect(env.languageService.getSemanticDiagnostics("a.ts")).toHaveLength(0) +})