-
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
Fix IDE remote analyzer loader on .Net Core #57407
Merged
Merged
Changes from all commits
Commits
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 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 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 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
50 changes: 50 additions & 0 deletions
50
src/Workspaces/Remote/ServiceHub/Host/RemoteAnalyzerAssemblyLoader.cs
This file contains 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,50 @@ | ||
// 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 System.IO; | ||
|
||
namespace Microsoft.CodeAnalysis.Remote.Diagnostics | ||
{ | ||
/// <summary> | ||
/// For analyzers shipped in Roslyn, different set of assemblies might be used when running | ||
/// in-proc and OOP e.g. in-proc (VS) running on desktop clr and OOP running on ServiceHub .Net6 | ||
/// host. We need to make sure to use the ones from the same location as the remote. | ||
/// </summary> | ||
internal sealed class RemoteAnalyzerAssemblyLoader : DefaultAnalyzerAssemblyLoader | ||
{ | ||
private readonly string _baseDirectory; | ||
|
||
public RemoteAnalyzerAssemblyLoader(string baseDirectory) | ||
{ | ||
_baseDirectory = baseDirectory; | ||
} | ||
|
||
protected override string GetPathToLoad(string fullPath) | ||
{ | ||
var fixedPath = Path.GetFullPath(Path.Combine(_baseDirectory, Path.GetFileName(fullPath))); | ||
return File.Exists(fixedPath) ? fixedPath : fullPath; | ||
} | ||
|
||
#if NETCOREAPP | ||
|
||
// The following are special assemblies since they contain IDE analyzers and/or their dependencies, | ||
// but in the meantime, they also contain the host of compiler in remote process. Therefore on coreclr, | ||
// we must ensure they are only loaded once and in the same ALC compiler asemblies are loaded into. | ||
// Otherwise these analyzers will fail to interoperate with the host due to mismatch in assembly identity. | ||
private static readonly ImmutableHashSet<string> s_ideAssemblySimpleNames = | ||
CompilerAssemblySimpleNames.Union(new[] | ||
{ | ||
"Microsoft.CodeAnalysis.Features", | ||
"Microsoft.CodeAnalysis.CSharp.Features", | ||
"Microsoft.CodeAnalysis.VisualBasic.Features", | ||
"Microsoft.CodeAnalysis.Workspaces", | ||
"Microsoft.CodeAnalysis.CSharp.Workspaces", | ||
"Microsoft.CodeAnalysis.VisualBasic.Workspaces", | ||
}); | ||
|
||
internal override ImmutableHashSet<string> AssemblySimpleNamesToBeLoadedInCompilerContext => s_ideAssemblySimpleNames; | ||
#endif | ||
} | ||
} |
This file contains 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 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
16 changes: 16 additions & 0 deletions
16
...orkspaces/Remote/ServiceHubTest/Microsoft.CodeAnalysis.Remote.ServiceHub.UnitTests.csproj
This file contains 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,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- 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. --> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to add a new test project because we currently don't have any IDE tests runs on .Net Core above workspaces layer |
||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
<TargetFramework>net6.0-windows</TargetFramework> | ||
<RootNamespace>Microsoft.CodeAnalysis.UnitTests</RootNamespace> | ||
</PropertyGroup> | ||
<ItemGroup Label="Project References"> | ||
<ProjectReference Include="..\ServiceHub\Microsoft.CodeAnalysis.Remote.ServiceHub.csproj" /> | ||
<ProjectReference Include="..\..\..\Compilers\Test\Core\Microsoft.CodeAnalysis.Test.Utilities.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> | ||
</ItemGroup> | ||
</Project> |
70 changes: 70 additions & 0 deletions
70
src/Workspaces/Remote/ServiceHubTest/RemoteAnalyzerAssemblyLoaderTests.cs
This file contains 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,70 @@ | ||
// 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.IO; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Remote.Diagnostics; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Remote.UnitTests | ||
{ | ||
public class RemoteAnalyzerAssemblyLoaderTests | ||
{ | ||
[Fact] | ||
public void NonIdeAnalyzerAssemblyShouldBeLoadedInSeparateALC() | ||
{ | ||
var remoteAssemblyInCurrentAlc = typeof(RemoteAnalyzerAssemblyLoader).GetTypeInfo().Assembly; | ||
var remoteAssemblyLocation = remoteAssemblyInCurrentAlc.Location; | ||
|
||
var loader = new RemoteAnalyzerAssemblyLoader(Path.GetDirectoryName(remoteAssemblyLocation)!); | ||
|
||
// Try to load MS.CA.Remote.ServiceHub.dll as an analyzer assembly via RemoteAnalyzerAssemblyLoader | ||
// since it's not one of the special assemblies listed in RemoteAnalyzerAssemblyLoader, | ||
// RemoteAnalyzerAssemblyLoader should loaded in a spearate DirectoryLoadContext. | ||
loader.AddDependencyLocation(remoteAssemblyLocation); | ||
var remoteAssemblyLoadedViaRemoteLoader = loader.LoadFromPath(remoteAssemblyLocation); | ||
|
||
var alc1 = AssemblyLoadContext.GetLoadContext(remoteAssemblyInCurrentAlc); | ||
var alc2 = AssemblyLoadContext.GetLoadContext(remoteAssemblyLoadedViaRemoteLoader); | ||
Assert.NotEqual(alc1, alc2); | ||
} | ||
|
||
[Fact] | ||
public void IdeAnalyzerAssemblyShouldBeLoadedInLoaderALC() | ||
{ | ||
var featuresAssemblyInCurrentAlc = typeof(Microsoft.CodeAnalysis.Completion.CompletionProvider).GetTypeInfo().Assembly; | ||
var featuresAssemblyLocation = featuresAssemblyInCurrentAlc.Location; | ||
|
||
// Try to load MS.CA.Features.dll as an analyzer assembly via RemoteAnalyzerAssemblyLoader | ||
// since it's listed as one of the special assemblies in RemoteAnalyzerAssemblyLoader, | ||
// RemoteAnalyzerAssemblyLoader should loaded in its own ALC. | ||
var loader = new RemoteAnalyzerAssemblyLoader(Path.GetDirectoryName(featuresAssemblyLocation)!); | ||
loader.AddDependencyLocation(featuresAssemblyLocation); | ||
|
||
var featuresAssemblyLoadedViaRemoteLoader = loader.LoadFromPath(featuresAssemblyLocation); | ||
|
||
var alc1 = AssemblyLoadContext.GetLoadContext(featuresAssemblyInCurrentAlc); | ||
var alc2 = AssemblyLoadContext.GetLoadContext(featuresAssemblyLoadedViaRemoteLoader); | ||
Assert.Equal(alc1, alc2); | ||
} | ||
|
||
[Fact] | ||
public void CompilerAssemblyShouldBeLoadedInLoaderALC() | ||
{ | ||
var compilerAssemblyInCurrentAlc = typeof(SyntaxNode).GetTypeInfo().Assembly; | ||
var compilerAssemblyLocation = compilerAssemblyInCurrentAlc.Location; | ||
|
||
var loader = new RemoteAnalyzerAssemblyLoader(Path.GetDirectoryName(compilerAssemblyLocation)!); | ||
loader.AddDependencyLocation(compilerAssemblyLocation); | ||
|
||
var compilerAssemblyLoadedViaRemoteLoader = loader.LoadFromPath(compilerAssemblyLocation); | ||
|
||
var alc1 = AssemblyLoadContext.GetLoadContext(compilerAssemblyInCurrentAlc); | ||
var alc2 = AssemblyLoadContext.GetLoadContext(compilerAssemblyLoadedViaRemoteLoader); | ||
Assert.Equal(alc1, alc2); | ||
} | ||
} | ||
} |
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.
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.