Skip to content

Better manage Program options lifetimes - #64519

Open
Jake Bailey (jakebailey) wants to merge 5 commits into
microsoft:mainfrom
jakebailey:program-lifetime-boundaries
Open

Jake Bailey (jakebailey) wants to merge 5 commits into
microsoft:mainfrom
jakebailey:program-lifetime-boundaries

Conversation

@jakebailey

@jakebailey Jake Bailey (jakebailey) commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

This is intended to replace #64466 with something that prevents us from making this mistake in the future.

ProgramOptions is split into three pieces:

  • ProgramConfig - data only bits like the compiler options, SingleThreaded, etc, that can be carried between programs as they are reused.
  • ProgramHosts - hosts like CompilerHost and Tracing that are specific to a program but should not be carried over as new programs have different contexts.
  • ProgramFactories - funcs to create checker pools or module resolvers which are only called on construction

Splitting these up allows us to only persist the right ones over time and discard them when no longer needed, making it harder to accidentally retain old state.

Additionally, this PR fixes diags that are kept alive between programs (which can end up referencing old data), and tweaks the module resolver so it too has a data holding value that can be carried between.

Fixes #64465
Closes #64466

Cloned programs share inclusion records, but diagnostics derived from
those records can refer to a particular program's files. Caching them on
the shared records lets later generations reuse diagnostics from an old
program and prolong the lifetime of its source files.

Keep derived diagnostics with their program and make the shared payloads
explicit so the reusable records cannot hide arbitrary owners.
Resolver registrations survive the request that creates them. Retaining
that request's context in their factory can send later callbacks through
an already canceled context and keep request-scoped state alive.

Pass the current construction request's context when creating a resolver
instead of retaining it in the reusable factory.
Shared program data must not retain factory closures or hosts from an
older generation. In particular, project-reference mappings outlive the
construction callbacks that populate them.

Keep those lifetimes in separate types while preserving the flat
ProgramOptions literal used by callers. Only reusable configuration and
the current hosts belong to the finished program; construction factories
and mapper-building state do not.
Reusable program data must not retain a resolver that keeps its creating
host and snapshot alive. Retain only host-free resolution data, and bind
lazy queries to the current program host.

Package lookups can populate caches after construction, so each program
needs its own package-json cache table. Construction-only resolution
caches do not need to survive in completed programs.

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

Broad cross-cutting lifetime and cache-ownership changes warrant final human validation despite strong targeted tests.

Review effort: Balanced
Findings: None

What changed in this PR

Splits program configuration, hosts, and factories to prevent cloned programs from retaining prior program generations and associated runtime state.

Changes:

  • Separates reusable program and module-resolution data from generation-specific hosts and factories.
  • Makes diagnostic caches program-local and project-reference mapping host-independent.
  • Adds lifetime, cache-isolation, context, and project-reference regression tests.
File Description
tsc/​internal/​testutil/​testutil.go Adds retained-data type validation.
tsc/​internal/​project/​snapshot.go Passes context to resolver factories.
tsc/​internal/​project/​snapshot_test.go Tests collection of old generations.
tsc/​internal/​project/​project.go Uses current build context for resolvers.
tsc/​internal/​packagejson/​cache.go Adds cache cloning.
tsc/​internal/​module/​types.go Exposes reusable resolution data.
tsc/​internal/​module/​staticresolver.go Delegates resolution-data access.
tsc/​internal/​module/​staticresolver_test.go Updates internal resolver tests.
tsc/​internal/​module/​resolver.go Separates resolver data from runtime caches.
tsc/​internal/​module/​resolver_test.go Tests cache and data isolation.
tsc/​internal/​module/​cache.go Defines cloneable resolution data.
tsc/​internal/​compiler/​projectreferenceparser.go Builds host-free reference mappings.
tsc/​internal/​compiler/​projectreferencefilemapper.go Splits persistent mapping from builder state.
tsc/​internal/​compiler/​projectreferencedtsfakinghost.go Rebinds mappings to current hosts.
tsc/​internal/​compiler/​program.go Separates program options and clone lifetimes.
tsc/​internal/​compiler/​program_test.go Tests ownership and cloning behavior.
tsc/​internal/​compiler/​processingDiagnostic.go Makes diagnostic payloads data-only.
tsc/​internal/​compiler/​includeprocessor.go Separates shared include data from caches.
tsc/​internal/​compiler/​filesparser.go Produces reusable include data.
tsc/​internal/​compiler/​fileloader.go Avoids retaining complete program options.
tsc/​internal/​compiler/​fileInclude.go Moves diagnostic caching onto programs.
tsc/​internal/​compiler/​emitHost.go Removes obsolete resolver delegation.
tsc/​internal/​api/​session.go Defers resolver context selection.
tsc/​internal/​api/​session_module_resolution_test.go Tests current-context propagation and cleanup.
tsc/​internal/​api/​module_resolution.go Uses per-construction callback contexts.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

The collection test intermittently exceeds its deadline under concurrent
coverage runs, even after multiple completed GC cycles. Keep the
deterministic ownership and cache-isolation checks without making CI
depend on collection timing.

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

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

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Language server retains the pre-edit program and its checkers for the rest of the session after the first edit

2 participants