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

Isolate each Analyzer assembly into its own LoadContext. #959

Merged
merged 2 commits into from Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Analyzers/AnalyzerReferenceInformationProvider.cs
Expand Up @@ -23,10 +23,8 @@ internal class AnalyzerReferenceInformationProvider : IAnalyzerInformationProvid

private AnalyzersAndFixers GetAnalyzersAndFixers(Project project)
{
var context = new AnalyzerLoadContext();

var analyzerAssemblies = project.AnalyzerReferences
.Select(reference => TryLoadAssemblyFrom(reference.FullPath, context))
.Select(reference => TryLoadAssemblyFrom(reference.FullPath, new AnalyzerLoadContext()))
.OfType<Assembly>()
.ToImmutableArray();

Expand Down
5 changes: 3 additions & 2 deletions src/Analyzers/SolutionCodeFixApplier.cs
Expand Up @@ -109,9 +109,10 @@ public override Task<IEnumerable<Diagnostic>> GetAllDiagnosticsAsync(Project pro
return GetProjectDiagnosticsAsync(project, cancellationToken);
}

public override Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken)
public override async Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken)
{
throw new NotImplementedException();
var projectDiagnostics = await GetProjectDiagnosticsAsync(document.Project, cancellationToken);
return projectDiagnostics.Where(diagnostic => diagnostic.Location.SourceTree?.FilePath == document.FilePath).ToImmutableArray();
}

public override Task<IEnumerable<Diagnostic>> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken)
Expand Down
93 changes: 93 additions & 0 deletions tests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs
Expand Up @@ -118,5 +118,98 @@ void M()

await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences);
}

[Fact]
public async Task TestIDisposableAnalyzer_AddsUsing()
{
var analyzerReferences = GetAnalyzerReferences("IDisposable");

var testCode = @"
using System.IO;

class C
{
void M()
{
var stream = File.OpenRead(string.Empty);
var b = stream.ReadByte();
stream.Dispose();
}
}
";

var expectedCode = @"
using System.IO;

class C
{
void M()
{
using (var stream = File.OpenRead(string.Empty))
{
var b = stream.ReadByte();
}
}
}
";

var editorConfig = new Dictionary<string, string>()
{
// Turn off all diagnostics analyzers
["dotnet_analyzer_diagnostic.severity"] = "none",

// Prefer using. IDISP017
["dotnet_diagnostic.IDISP017.severity"] = "error",
};

await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences);
}

[Fact]
public async Task TestLoadingAllAnalyzers_LoadsDependenciesFromAllSearchPaths()
{
// Loads all analyzer references.
var analyzerReferences = _analyzerReferencesProject.AnalyzerReferences;

var testCode = @"
using System.IO;

class C
{
void M()
{
var stream = File.OpenRead(string.Empty);
var b = stream.ReadByte();
stream.Dispose();
}
}
";

var expectedCode = @"
using System.IO;

class C
{
void M()
{
using (var stream = File.OpenRead(string.Empty))
{
var b = stream.ReadByte();
}
}
}
";

var editorConfig = new Dictionary<string, string>()
{
// Turn off all diagnostics analyzers
["dotnet_analyzer_diagnostic.severity"] = "none",

// Prefer using. IDISP017
["dotnet_diagnostic.IDISP017.severity"] = "error",
};

await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences);
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<disabledPackageSources />
</configuration>
Expand Up @@ -9,6 +9,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="AspNetCoreAnalyzers" Version="0.2.0.1-dev" PrivateAssets="all" />
<PackageReference Include="IDisposableAnalyzers" Version="3.4.8" PrivateAssets="all" />
</ItemGroup>

</Project>