Skip to content

Avoid storing duplicate SourceFile in createVirtualTypeScriptEnvironment (#3614) - #3684

Open
Hrushikesh Ramilla (Hrushikesh-ramilla) wants to merge 1 commit into
microsoft:v2from
Hrushikesh-ramilla:fix/vfs-duplicate-sourcefile
Open

Hrushikesh Ramilla (Hrushikesh-ramilla) wants to merge 1 commit into
microsoft:v2from
Hrushikesh-ramilla:fix/vfs-duplicate-sourcefile

Conversation

@Hrushikesh-ramilla

Copy link
Copy Markdown

Fixes #3614

Problem

In createVirtualTypeScriptEnvironment, createFile and updateFile currently parse a full s.SourceFile AST and pass it to the compiler host (updateFile), which stores it in its internal sourceFiles map.
However, the language service never reads that map — its host's getScriptSnapshot reads raw text from sys, and the language service parses and caches its own SourceFile ASTs from those snapshots.
As a result, every file in the virtual TypeScript environment is held in memory as two duplicate ASTs.

Solution

  • In createVirtualTypeScriptEnvironment, pass { fileName, text } instead of parsing an unread duplicate s.createSourceFile() or s.updateSourceFile().
  • In createVirtualLanguageServiceHost, write directly to sys and avoid storing redundant SourceFiles in the compiler host map.
  • Added comprehensive unit tests in packages/typescript-vfs/test/index.test.ts verifying that s.createSourceFile and s.updateSourceFile are not parsed redundantly and that language service AST retrieval works on demand.

Verification

pm test in packages/typescript-vfs: 25 / 25 tests passed (including new tests).

  • dts lint: 0 errors.

@Hrushikesh-ramilla

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant