-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Cache MEF catalog in servicehub process #78122
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
Merged
ToddGrun
merged 25 commits into
dotnet:main
from
ToddGrun:dev/toddgrun/4-11-MEF-Caching-In-ServiceHub-Process
Apr 22, 2025
Merged
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8ae3b7b
Cache MEF catalog in servicehub process
ToddGrun f1dcf0a
1) Get rid of MS.Extensions.Logging dependency
ToddGrun 9ff80c0
Check cancellation token after doing expensive operation
ToddGrun 17d73d7
Include the Env version in the hash, not in the path
ToddGrun dbfb724
Remove unused variable
ToddGrun 7103a4f
fix a unit test issue
ToddGrun 97edeb6
Merge branch 'dotnet:main' into dev/toddgrun/4-11-MEF-Caching-In-Serv…
ToddGrun 87a1d96
merge main
ToddGrun 0014f71
1) Properly fix merge conflict
ToddGrun 36df091
1) Use ArrayBuilder instead of Enumerable.Concat
ToddGrun e5b9416
Merge branch 'dotnet:main' into dev/toddgrun/4-11-MEF-Caching-In-Serv…
ToddGrun 38d0567
Move some methods from static to instance based and make the Language…
ToddGrun b4e5b8e
remove accidental debug aid
ToddGrun b542f43
1) Return an error message (if encountered) during initialization and…
ToddGrun acbb93d
Fix build, cosmetic suggestions in PR
ToddGrun 9dc7404
Merge branch 'dotnet:main' into dev/toddgrun/4-11-MEF-Caching-In-Serv…
ToddGrun fdaa909
Temporary workaround until the razor EA issue is addressed
ToddGrun a6e5fbb
fix linting issue when temporarily commenting out code
ToddGrun bbaff4e
Handlre razor external access assembly mef loading issue better
ToddGrun e9176c9
1) Fix more unit tests due to razor EA
ToddGrun 584f74b
Merge branch 'dotnet:main' into dev/toddgrun/4-11-MEF-Caching-In-Serv…
ToddGrun 6886404
Cleanup after razor EA cleanup
ToddGrun 3020cb9
cleanup the cleanup
ToddGrun 57da43b
1) Rename variable
ToddGrun 012c2bd
1) Remove unuseful doc comment
ToddGrun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
229 changes: 0 additions & 229 deletions
229
src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/ExportProviderBuilder.cs
This file was deleted.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
...nguageServer/Microsoft.CodeAnalysis.LanguageServer/LanguageServerExportProviderBuilder.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis.LanguageServer.Logging; | ||
| using Microsoft.CodeAnalysis.LanguageServer.Services; | ||
| using Microsoft.CodeAnalysis.PooledObjects; | ||
| using Microsoft.CodeAnalysis.Remote; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.VisualStudio.Composition; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.LanguageServer; | ||
|
|
||
| internal sealed class LanguageServerExportProviderBuilder : ExportProviderBuilder | ||
| { | ||
| private readonly ILogger<ExportProviderBuilder> _logger; | ||
|
|
||
| // For testing purposes, track the last cache write task. | ||
| private static Task? s_cacheWriteTask; | ||
|
|
||
| private LanguageServerExportProviderBuilder( | ||
| ImmutableArray<string> assemblyPaths, | ||
| Resolver resolver, | ||
| string cacheDirectory, | ||
| string catalogPrefix, | ||
| ILoggerFactory loggerFactory) | ||
| : base(assemblyPaths, resolver, cacheDirectory, catalogPrefix) | ||
| { | ||
| _logger = loggerFactory.CreateLogger<ExportProviderBuilder>(); | ||
| } | ||
|
|
||
| public static async Task<ExportProvider> CreateExportProviderAsync( | ||
| ExtensionAssemblyManager extensionManager, | ||
| IAssemblyLoader assemblyLoader, | ||
| string? devKitDependencyPath, | ||
| string cacheDirectory, | ||
| ILoggerFactory loggerFactory, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var baseDirectory = AppContext.BaseDirectory; | ||
|
|
||
| // Load any Roslyn assemblies from the extension directory | ||
| using var _ = ArrayBuilder<string>.GetInstance(out var assemblyPathsBuilder); | ||
| assemblyPathsBuilder.AddRange(Directory.EnumerateFiles(baseDirectory, "Microsoft.CodeAnalysis*.dll")); | ||
ToddGrun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assemblyPathsBuilder.AddRange(Directory.EnumerateFiles(baseDirectory, "Microsoft.ServiceHub*.dll")); | ||
|
|
||
| // DevKit assemblies are not shipped in the main language server folder | ||
| // and not included in ExtensionAssemblyPaths (they get loaded into the default ALC). | ||
| // So manually add them to the MEF catalog here. | ||
| if (devKitDependencyPath != null) | ||
| assemblyPathsBuilder.AddRange(devKitDependencyPath); | ||
ToddGrun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Add the extension assemblies to the MEF catalog. | ||
| assemblyPathsBuilder.AddRange(extensionManager.ExtensionAssemblyPaths); | ||
|
|
||
| // Create a MEF resolver that can resolve assemblies in the extension contexts. | ||
| var builder = new LanguageServerExportProviderBuilder( | ||
| assemblyPathsBuilder.ToImmutableAndClear(), | ||
| new Resolver(assemblyLoader), | ||
| cacheDirectory, | ||
| catalogPrefix: "c#-languageserver", | ||
| loggerFactory); | ||
| var exportProvider = await builder.CreateExportProviderAsync(cancellationToken); | ||
|
|
||
| // Also add the ExtensionAssemblyManager so it will be available for the rest of the composition. | ||
| exportProvider.GetExportedValue<ExtensionAssemblyManagerMefProvider>().SetMefExtensionAssemblyManager(extensionManager); | ||
|
|
||
| // Immediately set the logger factory, so that way it'll be available for the rest of the composition | ||
| exportProvider.GetExportedValue<ServerLoggerFactory>().SetFactory(loggerFactory); | ||
|
|
||
| return exportProvider; | ||
| } | ||
|
|
||
| protected override void LogError(string message) | ||
| => _logger.LogError(message); | ||
|
|
||
| protected override void LogTrace(string message) | ||
| => _logger.LogTrace(message); | ||
|
|
||
| protected override Task<ExportProvider> CreateExportProviderAsync(CancellationToken cancellationToken) | ||
| { | ||
| // Clear any previous cache write task, so that it is easy to discern whether | ||
| // a cache write was attempted. | ||
| s_cacheWriteTask = null; | ||
|
|
||
| return base.CreateExportProviderAsync(cancellationToken); | ||
| } | ||
|
|
||
| protected override bool ContainsUnexpectedErrors(IEnumerable<string> erroredParts, ImmutableList<PartDiscoveryException> partDiscoveryExceptions) | ||
| { | ||
| // Verify that we have exactly the MEF errors that we expect. If we have less or more this needs to be updated to assert the expected behavior. | ||
| var expectedErrorPartsSet = new HashSet<string>(["CSharpMapCodeService", "PythiaSignatureHelpProvider", "CopilotSemanticSearchQueryExecutor"]); | ||
| var hasUnexpectedErroredParts = erroredParts.Any(part => !expectedErrorPartsSet.Contains(part)); | ||
ToddGrun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return hasUnexpectedErroredParts || !partDiscoveryExceptions.IsEmpty; | ||
| } | ||
|
|
||
| protected override Task WriteCompositionCacheAsync(string compositionCacheFile, CompositionConfiguration config, CancellationToken cancellationToken) | ||
| { | ||
| s_cacheWriteTask = base.WriteCompositionCacheAsync(compositionCacheFile, config, cancellationToken); | ||
|
|
||
| return s_cacheWriteTask; | ||
| } | ||
|
|
||
| protected override void PerformCacheDirectoryCleanup(DirectoryInfo directoryInfo, CancellationToken cancellationToken) | ||
| { | ||
| // No cache directory cleanup is needed for the language server. | ||
| } | ||
|
|
||
| internal static class TestAccessor | ||
| { | ||
| #pragma warning disable VSTHRD200 // Use "Async" suffix for async methods | ||
| public static Task? GetCacheWriteTask() => s_cacheWriteTask; | ||
| #pragma warning restore VSTHRD200 // Use "Async" suffix for async methods | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.