You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When on-demand loading is enabled, opening a C# file starts discovery. Context lookup reuses documents in registered workspaces before requesting a load. For files not already represented, Roslyn searches for the nearest project-file ancestor within the workspace folder, loads those projects and their transitive references, then re-resolves the request context against the updated workspace.
Request dispatch captures the LSP text and relevant load operations. Waiting for loading and projecting the captured text onto the loaded solution happen outside serialized dispatch, so unrelated requests and text-sync notifications can continue. Shared context clearing, cancellation isolation, and shutdown draining preserve request and service lifetimes.
43766d3fb80: give canonical project cache initialization a single owner; joining and priority promotion do not repeat cache work.
322bf860cfc: make load capture/completion explicit with ProjectLoadSnapshot and document discovery/traversal ownership.
3998a166d64: use ValueTask<ValueTask<LspContext>?> for context capture, retain inline results or one shared resolution task in private clearable state, inject the loader into the workspace manager, and separate miscellaneous-document operations from deferred projection.
73817c3c602: use a mutable reference-path set and remove architectural explanations from the IDE instructions without adding replacement documentation.
Local validation
Both affected test projects built for net10.0 with RunAnalyzersDuringBuild=true, with zero warnings/errors.
After the request-context refactor: 203 protocol tests passed with one existing platform skip; 139 host tests passed with four existing platform skips. These focused runs cover request ordering, captured text, shared clearing/cancellation, workspace lookup, metadata-as-source, loaders, and miscellaneous/file-based programs.
After the final cleanup: 35 loader tests passed with no skips, including ProjectReferencePathsAreResolvedAndDeduplicated.
Formatting verification and whitespace checks passed. Counts above describe separate focused runs, not a full-suite run.
The reason will be displayed to describe this comment to others. Learn more.
Copilot review overview
🟡 Changes recommended
Five unresolved moderate findings remain in project filtering, path containment, snapshot resolution, and loose-file fallback.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review tier: Lite Findings: 3
New issues introduced by this change (3)
Severity
Finding
src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/LanguageServerProjectSystem.cs — Filter transitive references to supported languages
src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/WorkspaceProjectDiscoveryService.cs — Use platform-correct path comparison for discovery
src/LanguageServer/Protocol/Handler/RequestContext.cs — Do not change obsolete wrapper semantics to post-load state
What changed in this PR
Adds deferred, on-demand project discovery and loading for Roslyn LSP requests, including transitive references and asynchronous request-context resolution.
Changes:
Adds nearest-project discovery and on-demand project loading.
Adds lazy context resolution with request-state capture and cancellation.
Adds options, tests, design documentation, and IDE guidance.
DidOpenHandler, DidChangeHandler, and DidCloseHandler all provide a TextDocumentIdentifier but set RequiresLSPSolution to false, so this branch starts discovery and project loading for every open/change/close notification even though those handlers never access a solution or document context. That defeats the on-demand behavior and can load an entire project merely because a file was opened; gate this path on requiresLSPSolution (or otherwise only start it when a solution/document context will be consumed).
DidOpenHandler, DidChangeHandler, and DidCloseHandler all provide a text-document identifier but set RequiresLSPSolution to false; they only update tracking state and never access the solution/document context. With on-demand loading enabled, this branch still starts directory discovery and MSBuild project loading for every open/change/close of an unloaded file, defeating the deferred-loading behavior and adding project-system work to these notifications. Gate this branch on requiresLSPSolution (or otherwise only start it for handlers that can consume the loaded context).
DidOpenHandler, DidChangeHandler, and DidCloseHandler supply a document identifier but set RequiresLSPSolution to false; their contexts deliberately do not access solution or document state. This branch still starts discovery and MSBuild loading for every open/change/close of an unloaded file, defeating deferred loading and potentially loading a whole project solely because a file was opened. Gate this condition on requiresLSPSolution as well.
CaptureWorkspaceLoadSnapshotAsync is awaited while the request context is being created inside the serialized request queue, but it has no request cancellation token. If snapshot capture waits for the project-loader gate, canceling this request cannot stop that wait and can hold up all later LSP messages. Thread the queue token through capture for gate acquisition, while keeping the returned shared completion independent of per-accessor cancellation.
Refresh contexts when snapshots were initially pending
The completion status is checked only after GetInitialSolutionContextAsync has captured its solution. If the snapshot is pending at entry, the load can commit after that initial solution is read but before this status check; the code then sees RanToCompletion and returns the stale pre-load context without deferred re-resolution. Record whether the snapshot was already complete before capturing the initial context, or always defer refresh for snapshots that were initially pending.
This hook receives only the URI, so it cannot distinguish a C# document from the other files this server can open. DidOpenHandler provides a LanguageId, and the language provider recognizes F#/Razor/etc.; opening one of those files under a workspace therefore schedules discovery and can MSBuild-load every nearby C#/VB project, although this feature is described as on-demand C# project loading. Preserve the document language/extension and gate discovery to supported source documents, with a regression test for an unrelated language.
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't expect needing CaptureLspDocumentContextAsync and this to both be public. We should only have one entry point per sln or document. Does anyone still call this?
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't expect needing CaptureLspDocumentContextAsync and this to both be public. We should only have one entry point per sln or document. Does anyone still call this?
The reason will be displayed to describe this comment to others. Learn more.
nit - I think you could re-write as
// Task representing on demand in-progress loads.
Task? projectLoadTask = null
if (allowProjectLoading && _onDemandProjectLoader is not null)
{
var snapshot = await _onDemandProjectLoader.Capture...
if (snapshopt.Completion.Status != RanToCompletion)
{
// We have an uncompleted in progress load
projectLoadTask = snapshot.Completion;
}
}
...
if (!projectLoadTask)
return new ...
return new ... (projectLoadTask)
The reason will be displayed to describe this comment to others. Learn more.
Historically we haven't done anything on open. Fine with this but if it simplifies things I also don't mind loading starting on the first request for the document
The reason will be displayed to describe this comment to others. Learn more.
I think this entire type goes away if we just let this be a Task<LspWorkspaceManager.LspContext> instead? Or just replace all the value stuff with a Task (and yes we're allocating a bit more but it'll be a lot clearer) but keep the type, and then have this be what is returned from CaptureLspDocumentContextAsync, so that way you avoid the task-of-task confusion.
The reason will be displayed to describe this comment to others. Learn more.
This is just equivalent to cancellationSource.Token.ThrowIfCancellationRequested(); expectedSolution = initialSolution, which kinda doesn't make any sense here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds on-demand C# project loading to Roslyn LSP.
When on-demand loading is enabled, opening a C# file starts discovery. Context lookup reuses documents in registered workspaces before requesting a load. For files not already represented, Roslyn searches for the nearest project-file ancestor within the workspace folder, loads those projects and their transitive references, then re-resolves the request context against the updated workspace.
Request dispatch captures the LSP text and relevant load operations. Waiting for loading and projecting the captured text onto the loaded solution happen outside serialized dispatch, so unrelated requests and text-sync notifications can continue. Shared context clearing, cancellation isolation, and shutdown draining preserve request and service lifetimes.
Prerequisite stack
Review updates
ProjectLoadSnapshotand document discovery/traversal ownership.ValueTask<ValueTask<LspContext>?>for context capture, retain inline results or one shared resolution task in private clearable state, inject the loader into the workspace manager, and separate miscellaneous-document operations from deferred projection.Local validation
net10.0withRunAnalyzersDuringBuild=true, with zero warnings/errors.ProjectReferencePathsAreResolvedAndDeduplicated.Microsoft Reviewers: Open in CodeFlow