Skip to content

Avoid rebuilding later LSP project references - #64936

Merged
Andrew Branch (andrewbranch) merged 5 commits into
microsoft:mainfrom
chengchuu:fix/lsp-referenced-project-rebuild
Nov 25, 2026
Merged

Andrew Branch (andrewbranch) merged 5 commits into
microsoft:mainfrom
chengchuu:fix/lsp-referenced-project-rebuild

Conversation

@chengchuu

@chengchuu Cheng (chengchuu) commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #64862

Avoid repeatedly building later references when a clean, loaded project already contains the opened file. Earlier candidates still run in parallel, preserving reference order and breadth-first selection.

Remove later candidates after traversal, with a fast path in OrderedMap to keep pruning linear. Initial searches can still build unrelated projects; source or configuration changes invalidate the shortcut.

Reproduction

The standalone reproduction opens and closes three files belonging to tsconfig.app.json. Before the fix, the language server built and deleted the unrelated tsconfig.node.json project three times; afterward, it did so only once during initial discovery. All three textDocument/documentSymbol requests succeeded.

Validation

All checks below passed on macOS ARM64, run from the repository root.

go -C tsc test -mod=readonly \
  ./internal/core ./internal/project/... ./internal/lsp/... \
  ./internal/ls/... ./internal/api/... ./cmd/tsc
go -C tsc test -mod=readonly -race \
  ./internal/collections ./internal/core ./internal/project/... ./internal/lsp/...
go -C tsc test -mod=readonly -race ./internal/project \
  -run '^TestProjectCollectionBuilder' -count=10
npx hereby test
npx hereby lint
npx hereby check:format

Copilot AI balanced review requested due to automatic review settings October 23, 2026 15:19
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Oct 23, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Oct 23, 2026

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 was unable to review this pull request because the user who requested the review has reached their quota limit.

@chengchuu

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

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.

🟢 Approval recommended

No unresolved blocking issues were identified, and the reviewed changes include regression coverage for the affected behavior.

No open issues 🎉

Review effort: Lite

@jakebailey Jake Bailey (jakebailey) added the Domain: Performance Performance related label Oct 27, 2026

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.

🟡 Changes recommended

The tests can pass without exercising the new pruning and omit a warm-cache source-redirect case needed to catch incorrect pruning of the actual default project.

2 open issues

Review effort: Max

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes sense in theory, but I don't love how PreprocessLevel now does the cheap half of the visit function, but can't actually return its result. (I wasn't thrilled with having to introduce PreprocessLevel to begin with, and now it's doing even more.)

That said, I'm not sure I have a slam dunk suggestion to make it cleaner... I was considering making the visit function return something like { CheapWork Result; ExpensiveWork func() Result }, or allowing level.StopAfter(key) to be called in PreprocessLevel, but these are both arguably more opaque than what you have here.

I think at least extracting as much of the project-file inclusion logic as you can into a function or method shared between visit and PreprocessLevel would be good, but let me know if you have any other ideas on how to make this read a little better.

Cheng (chengchuu) and others added 2 commits November 24, 2026 12:04
Pass the stored key to Range callbacks so callers can reuse it directly.

Co-authored-by: Andrew Branch <andrewbranch@users.noreply.github.com>

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.

🔵 Needs a closer look

Removing earlier find entries breaks reference-order selection in mixed-load scenarios, selecting C instead of the earlier matching loaded project B.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

High severity Preserve loaded-candidate order in mixed find/create lookup

tsc/​internal/​project/​projectcollectionbuilder.go:1093

Removing both earlier finds changes reference-order selection for [A/find, B/find, C/create, A/create, B/create]. If A does not match and loaded B and C both directly contain the file, this now selects C instead of the earlier B. Preserve the earlier position of a loaded candidate while still allowing creation of unloaded projects, and add a mixed-load-kind regression test.

2 resolved since last review

Review effort: Max

@andrewbranch

Copy link
Copy Markdown
Member

Cheng (@chengchuu) can you take a look at the "previously missed issue" from the Copilot review? Looks plausible. Thanks!

@chengchuu

Copy link
Copy Markdown
Contributor Author

Andrew Branch (@andrewbranch) Thanks for the review! I’ve pushed the follow-up. Built projects keep their earlier Find position, and paired Find and Create requests share one refresh. The race tests and repository validation passed.

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.

🟡 Changes recommended

A review nit remains to document that keepFind requires a same-level Create request, since invoking it for a Find-only node would refresh a project meant only to be searched.

1 open issue

Review effort: Max (adaptive)

return true
}
createKey := searchNodeKey{configFileName: key.configFileName, loadKind: projectLoadKindCreate}
// Built projects can match at their earlier Find position.
@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Nov 25, 2026
@andrewbranch
Andrew Branch (andrewbranch) added this pull request to the merge queue Nov 25, 2026
Merged via the queue into microsoft:main with commit 3713de0 Nov 25, 2026
34 checks passed
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Nov 25, 2026
@chengchuu
Cheng (chengchuu) deleted the fix/lsp-referenced-project-rebuild branch November 26, 2026 04:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Domain: Performance Performance related For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

TS7 LSP: every didOpen rebuilds and deletes a non-matching referenced project

4 participants