Skip to content

[api] Give standalone created files a tracked identity and incorporate with the parse cache - #64434

Merged
Andrew Branch (andrewbranch) merged 2 commits into
microsoft:mainfrom
andrewbranch:create-source-file-uses-cache
Sep 24, 2026
Merged

Andrew Branch (andrewbranch) merged 2 commits into
microsoft:mainfrom
andrewbranch:create-source-file-uses-cache

Conversation

@andrewbranch

@andrewbranch Andrew Branch (andrewbranch) commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Changes api.createSourceFile and api.createSourceFileFromFile to acquire from the global parse cache (on both client and server) and return a disposable lease:

- const sourceFile = api.createSourceFile(fileName, text);
+ using lease = api.createSourceFile(fileName, text);
+ lease.sourceFile;
api.createSourceFile(fileName, text).sourceFile === api.createSourceFile(fileName, text);
// true

api.createProgram([fileName], {}).getSourceFile(fileName) === api.createSourceFile(fileName, text).sourceFile;
// true, assuming host's readFile returns same text as `text`

This does few things:

  1. Immediately reduces parsing cost for a program creation that sees the same file + options as one that's already been parsed (something David Sherret (@dsherret) asked about)
  2. Opens the door to providing specific file handles to program construction, like jakebailey@c3b4130 (possible follow-up)
  3. Opens the door to attaching binder Symbols to SourceFiles with proper lifetime and identity (definite follow-up)

Copilot AI balanced review requested due to automatic review settings September 24, 2026 21:41
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Sep 24, 2026
@typescript-automation typescript-automation Bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 24, 2026

This comment was marked as resolved.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Cross-language protocol, cache identity, and reference-lifetime changes warrant final human validation.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@jakebailey

Copy link
Copy Markdown
Member

What if you just want a throwaway AST and don't care about equality or reuse?

@andrewbranch

Copy link
Copy Markdown
Member Author

Then using it or otherwise dispose it when you're ready to throw it away, or don't and it will get released when you close the API

@andrewbranch

Copy link
Copy Markdown
Member Author

The problem is the binder symbol follow-up. With server identities, you can do things like

sourceFile.getSymbol();         // fetches if not already cached
api.bindSourceFile(sourceFile); // fetches and assigns all binder symbols

You can't do that with an untracked, throwaway SourceFile, which makes for a confusing bifurcation of types/capabilities.

@andrewbranch
Andrew Branch (andrewbranch) added this pull request to the merge queue Sep 24, 2026
Merged via the queue into microsoft:main with commit ce582a7 Sep 24, 2026
29 checks passed
@andrewbranch
Andrew Branch (andrewbranch) deleted the create-source-file-uses-cache branch September 24, 2026 22:39
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants