dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.75k stars 3.99k forks source link

Test composition fails when a part is rejected #60690

Open tmat opened 2 years ago

tmat commented 2 years ago

PR https://github.com/dotnet/roslyn/pull/60687 added a new TypeScipt External Access language service, which imports implementation interface that's not implemented in the composition. The test composition should just ignore this service.

System.Composition.Hosting.CompositionFailedException : No export was found for the contract 'IVSTypeScriptCommentSelectionServiceImplementation'
-> required by import 'impl' of part 'VSTypeScriptCommentSelectionService'
-> required by import 'value' of part 'Lazy<ILanguageService, LanguageServiceMetadata>'
-> required by import 'item' of part 'Lazy`2[]'
-> required by initial request for contract 'IEnumerable<Lazy<ILanguageService, LanguageServiceMetadata>> { IsImportMany = True }'

Stack trace

...
Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.MefHostExportProvider.Microsoft.CodeAnalysis.Host.Mef.IMefHostExportProvider.GetExports[TExtension,TMetadata]() in /_/src/CodeStyle/Core/CodeFixes/Host/Mef/CodeStyleHostLanguageServices.cs.cs:line 61
   at Microsoft.CodeAnalysis.Host.Mef.MefLanguageServices..ctor(MefWorkspaceServices workspaceServices, String language) in /_/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Workspace/Mef/MefLanguageServices.cs:line 37
   at Microsoft.CodeAnalysis.Host.Mef.MefWorkspaceServices.<>c.<GetLanguageServices>b__21_0(String languageName, MefWorkspaceServices self) in /_/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Workspace/Mef/MefWorkspaceServices.cs:line 163
   at System.Collections.Immutable.ImmutableInterlocked.GetOrAdd[TKey,TValue,TArg](ImmutableDictionary`2& location, TKey key, Func`3 valueFactory, TArg factoryArgument)
   at Microsoft.CodeAnalysis.Host.Mef.MefWorkspaceServices.GetLanguageServices(String languageName) in /_/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Workspace/Mef/MefWorkspaceServices.cs:line 163
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices..ctor(HostLanguageServices hostLanguageServices) in /_/src/CodeStyle/Core/CodeFixes/Host/Mef/CodeStyleHostLanguageServices.MefHostExportProvider.cs:line 27
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.Create(HostLanguageServices hostLanguageServices) in /_/src/CodeStyle/Core/CodeFixes/Host/Mef/CodeStyleHostLanguageServices.MefHostExportProvider.cs:line 38
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey key, CreateValueCallback createValueCallback)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.GetRequiredMappedCodeStyleLanguageServices(HostLanguageServices hostLanguageServices) in /_/src/CodeStyle/Core/CodeFixes/Host/Mef/CodeStyleHostLanguageServices.MefHostExportProvider.cs:line 35
   at Microsoft.CodeAnalysis.Host.HostWorkspaceServicesExtensions.GetExtendedLanguageServices(HostWorkspaceServices hostWorkspaceServices, String languageName) in 
...
sharwell commented 2 years ago

@tmat MEF 2 does not support silent part rejection. Parts defined in the Workspaces or Features layer (where VS MEF might not be used) that need to silently exclude themselves must follow this pattern: https://github.com/dotnet/roslyn/pull/59047

sharwell commented 2 years ago

Specifically, you want this pattern:

https://github.com/dotnet/roslyn/blob/b38725d4a20c8bdf7a5683e56a0b44a32f760db1/src/Features/Core/Portable/ExternalAccess/VSTypeScript/VSTypeScriptDiagnosticAnalyzerLanguageService.cs#L17-L28