Skip to content

@typescript/vfs: createVirtualTypeScriptEnvironment stores a duplicate SourceFile per file that the language service never reads #3614

Description

@MorganDavidInf

Page URL: https://github.com/microsoft/TypeScript-Website/blob/v2/packages/typescript-vfs/src/index.ts (@typescript/vfs)

Issue:

createVirtualTypeScriptEnvironment permanently stores a second parsed AST for every file, roughly doubling per-file memory.

Where the duplicates are created:

  • The env's createFile/updateFile parse a SourceFile (ts.createSourceFile / ts.updateSourceFile) and pass it down to createVirtualCompilerHost's updateFile, which saves it in its internal sourceFiles map.
  • The language service never reads that map — its host's getScriptSnapshot reads text from sys, and the language service parses and caches its own SourceFiles from those snapshots.

So the ASTs in the compiler-host map are write-only: every file that goes through env.createFile/env.updateFile is held as two full ASTs (the language service's copy plus the map's copy), and the map's copy is never released for the lifetime of the env.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions