Skip to content
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

Simplify and unify assembly loader provider #74895

Merged
merged 16 commits into from
Aug 26, 2024

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public LanguageServerWorkspaceFactory(
public async Task InitializeSolutionLevelAnalyzersAsync(ImmutableArray<string> analyzerPaths)
{
var references = new List<AnalyzerFileReference>();
var analyzerLoader = Workspace.Services.GetRequiredService<IAnalyzerAssemblyLoaderProvider>().GetShadowCopyLoader();
var analyzerLoader = Workspace.Services.GetRequiredService<IAnalyzerAssemblyLoaderProvider>().SharedShadowCopyLoader;

foreach (var analyzerPath in analyzerPaths)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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 System.Composition;
using System.Reflection;
using Microsoft.CodeAnalysis.Host;
Expand All @@ -15,20 +14,14 @@ namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace;
[ExportWorkspaceService(typeof(IAnalyzerAssemblyLoaderProvider), [WorkspaceKind.Host]), Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class VSCodeAnalyzerLoaderProvider(
internal sealed class VSCodeAnalyzerLoaderProviderFactory(
ExtensionAssemblyManager extensionAssemblyManager,
ILoggerFactory loggerFactory,
[ImportMany] IEnumerable<IAnalyzerAssemblyResolver> externalResolvers)
: AbstractAnalyzerAssemblyLoaderProvider(externalResolvers.ToImmutableArray())
: AbstractAnalyzerAssemblyLoaderProvider(externalResolvers)
{
private readonly ExtensionAssemblyManager _extensionAssemblyManager = extensionAssemblyManager;
private readonly ILoggerFactory _loggerFactory = loggerFactory;

protected override IAnalyzerAssemblyLoaderInternal CreateShadowCopyLoader(ImmutableArray<IAnalyzerAssemblyResolver> externalResolvers)
{
var baseLoader = base.CreateShadowCopyLoader(externalResolvers);
return new VSCodeExtensionAssemblyAnalyzerLoader(baseLoader, _extensionAssemblyManager, _loggerFactory.CreateLogger<VSCodeExtensionAssemblyAnalyzerLoader>());
}
protected override IAnalyzerAssemblyLoaderInternal WrapLoader(IAnalyzerAssemblyLoaderInternal baseLoader)
=> new VSCodeExtensionAssemblyAnalyzerLoader(baseLoader, extensionAssemblyManager, loggerFactory.CreateLogger<VSCodeExtensionAssemblyAnalyzerLoader>());

/// <summary>
/// Analyzer loader that will re-use already loaded assemblies from the extension load context.
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected virtual AnalyzerReference ReadAnalyzerReferenceFrom(ObjectReader reade
{
case nameof(AnalyzerFileReference):
var fullPath = reader.ReadRequiredString();
return new AnalyzerFileReference(fullPath, _analyzerLoaderProvider.GetShadowCopyLoader());
return new AnalyzerFileReference(fullPath, _analyzerLoaderProvider.SharedShadowCopyLoader);

case nameof(AnalyzerImageReference):
var guid = reader.ReadGuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,50 @@
// 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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.IO;
using Microsoft.CodeAnalysis.Host.Mef;

namespace Microsoft.CodeAnalysis.Host;

internal interface IAnalyzerAssemblyLoaderProvider : IWorkspaceService
{
IAnalyzerAssemblyLoaderInternal GetShadowCopyLoader();
IAnalyzerAssemblyLoaderInternal SharedShadowCopyLoader { get; }
}

/// <summary>
/// Abstract implementation of an analyzer assembly loader that can be used by VS/VSCode to provide a <see
/// cref="IAnalyzerAssemblyLoader"/> with an appropriate path.
/// </summary>
internal abstract class AbstractAnalyzerAssemblyLoaderProvider : IAnalyzerAssemblyLoaderProvider
{
private readonly ImmutableArray<IAnalyzerAssemblyResolver> _externalResolvers;
private readonly Lazy<IAnalyzerAssemblyLoaderInternal> _shadowCopyLoader;

public AbstractAnalyzerAssemblyLoaderProvider(IEnumerable<IAnalyzerAssemblyResolver> externalResolvers)
{
_externalResolvers = externalResolvers.ToImmutableArray();
_shadowCopyLoader = new(CreateShadowCopyLoader);
}

public IAnalyzerAssemblyLoaderInternal SharedShadowCopyLoader
=> _shadowCopyLoader.Value;

private IAnalyzerAssemblyLoaderInternal CreateShadowCopyLoader()
=> this.WrapLoader(DefaultAnalyzerAssemblyLoader.CreateNonLockingLoader(
Path.Combine(Path.GetTempPath(), nameof(Roslyn), "AnalyzerAssemblyLoader"),
Copy link
Member

Choose a reason for hiding this comment

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

It looks like we're now always using the same path now for VS/OOP - which I think is fine? I assume in VS we only make shadow copies in the remote workspace now? So devenv won't shadow copy analyzers and conflict with the remote workspace any more?

Copy link
Member Author

Choose a reason for hiding this comment

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

correct. every shadow copy loader already puts their work in a unique guid-named subdirectory of the base directory passed in. So there's no conflicts ever across instances (within the same process, or other process, etc.). THey are always independent.

_externalResolvers));

protected virtual IAnalyzerAssemblyLoaderInternal WrapLoader(IAnalyzerAssemblyLoaderInternal loader)
=> loader;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a little confused by this comment. _workspaceKind is still factoring into the path passed into WrapLoader, no?


[ExportWorkspaceService(typeof(IAnalyzerAssemblyLoaderProvider)), Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class DefaultAnalyzerAssemblyLoaderProvider(
[ImportMany] IEnumerable<IAnalyzerAssemblyResolver> externalResolvers)
: AbstractAnalyzerAssemblyLoaderProvider(externalResolvers);
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ internal ProjectSystemProject(
var provider = _projectSystemProjectFactory.SolutionServices.GetRequiredService<IAnalyzerAssemblyLoaderProvider>();
// NOTE: The provider will always return the same singleton, shadow copying, analyzer loader instance, which is
// important to ensure that analyzer dependencies are correctly loaded.
_analyzerAssemblyLoader = provider.GetShadowCopyLoader();
_analyzerAssemblyLoader = provider.SharedShadowCopyLoader;

_sourceFiles = new BatchingDocumentCollection(
this,
Expand Down Expand Up @@ -752,7 +752,7 @@ static ProjectUpdateState UpdateAnalyzerReferences(
projectUpdateState = projectUpdateState.WithIncrementalAnalyzerReferencesAdded(analyzersAddedInBatch);

var loaderProvider = solutionChanges.Solution.Services.GetRequiredService<IAnalyzerAssemblyLoaderProvider>();
var shadowCopyLoader = loaderProvider.GetShadowCopyLoader();
var shadowCopyLoader = loaderProvider.SharedShadowCopyLoader;

solutionChanges.UpdateSolutionForProjectAction(
projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ private Task StartRefreshingAnalyzerReferenceForFileAsync(string fullFilePath, C

var project = solution.GetRequiredProject(projectId);
var oldAnalyzerReference = project.AnalyzerReferences.First(r => r.FullPath == oldReferenceFullPath);
var newAnalyzerReference = new AnalyzerFileReference(oldReferenceFullPath, assemblyLoaderProvider.GetShadowCopyLoader());
var newAnalyzerReference = new AnalyzerFileReference(oldReferenceFullPath, assemblyLoaderProvider.SharedShadowCopyLoader);

var newSolution = solution
.RemoveAnalyzerReference(projectId, oldAnalyzerReference)
Expand Down

This file was deleted.

Loading