-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't implicitly load assemblies via MEF in Roslyn language server #73566
Don't implicitly load assemblies via MEF in Roslyn language server #73566
Conversation
This change adds AssemblyQualifiedName properties to the ILspService export attributes and LspServiceMetadataView that is computed from the service type. Then, LspServiceMetadataView.Type is resolved lazily.
This is a bit tricky, and potentially controversial. To avoid loading System.Types for IMethodHandlers, this change computes the necessary information in the Export attributes for ILspService and writes it to a stream to be re-hydrated within the LspServiceMetadataView.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I understood this, and I think its good. I reviewed commit at a time though, so might have missed something, but GitHub is not making it easy to do anything else 😛
...ures/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/HandlerMethodDetails.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I am happy with the approach. I'm requesting changes for two things
- The potential issue with typeref and assembly load context (see below)
- A test insertion to make sure we don't have unexpected regressions- I triggered one here https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=9604942&view=results
...anguageServer/Microsoft.CommonLanguageServerProtocol.Framework.Example/ExampleLspServices.cs
Show resolved
Hide resolved
...ures/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/HandlerMethodDetails.cs
Outdated
Show resolved
Hide resolved
...ures/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/HandlerMethodDetails.cs
Outdated
Show resolved
Hide resolved
...ures/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/HandlerMethodDetails.cs
Outdated
Show resolved
Hide resolved
...ures/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/HandlerMethodDetails.cs
Outdated
Show resolved
Hide resolved
src/Features/LanguageServer/Protocol/LspServices/LspServices.cs
Outdated
Show resolved
Hide resolved
src/Features/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/TypeRef.cs
Outdated
Show resolved
Hide resolved
src/Features/LanguageServer/Protocol/LspServices/AbstractExportLspServiceAttribute.cs
Outdated
Show resolved
Hide resolved
2122c48
to
b6fc8c2
Compare
...s/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/TypeRef.DefaultResolver.cs
Outdated
Show resolved
Hide resolved
7d20582
to
b6766fc
Compare
var responseType = _typeRefResolver.Resolve(metadata.ResponseTypeRef.GetValueOrDefault()) ?? NoValue.Instance.GetType(); | ||
var noValueType = NoValue.Instance.GetType(); | ||
|
||
var requestType = metadata.RequestTypeRef is TypeRef requestTypeRef |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm probably just being dumb here - when is the RequestTypeRef not a TypeRef?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When it's null. This is just a null check. Previously, TypeRef
was a struct and RequestTypeRef
is defined as TypeRef?
. So, when it was a struct, is TypeRef
would pull out the value. When I changed TypeRef
to a class, I decided to leave TypeRef
here rather than switching over to metadata.RequestTypeRef is { } requestTypeRef
, since the type name is pretty short.
Now that Razor is moving to a "co-hosting" model in which it contributes method handlers and services into the main Roslyn language server, it turns out that
Microsoft.VisualStudio.LanguageServices.Razor.dll
gets loaded for C#/VB projects. None of the Razor services are actually instantiated, but the language server's MEF registration causes the assembly containing the Razor services to load. This is because the MEF metadata view for each LSP service includes the service'sSystem.Type
, which requires the service's assembly to be loaded. The language server uses that type for reflection, probing implemented interfaces and collecting information about request handler methods.This pull request makes significant changes to CLaSP and the Roslyn language server's service registration. Instead of capturing the
System.Type
, the necessary reflection is performed up front in the export attributes and the reflected information is loaded into the MEF metadata view. UIn addition, a helperTypeRef
class has been added to CLaSP to replace anywhere in the API that theType
is exposed.TypeRef
avoids loading the actualSystem.Type
until it's requested.In addition, I've made several other improvements to CLaSP and LSP services:
ILspServices.SupportsGetRegisteredServices()
andILspServices.GetRegisteredServices()
. This functionality is now available in CLaSP asIMethodHandlerProvider
, which anyILspServices
can optionally implement. (Fixes Remove Lazy Handler requirement from Roslyn LSP #63555)ImmutableDictionaries
withFrozenDictionaries
inLspServices
, since the dictionaries are computed once for the lifetime of the language server.TelemetryService
), but I decided to fix this now rather than get bitten by it later.ILspServices.TryGetService(...)
follow the .NET TryGet* pattern.PR Validation Build: https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=9648175&view=results
VS Insertion Build: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/554387
cc @dibarbet and @davidwengier