-
Notifications
You must be signed in to change notification settings - Fork 4k
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
When replacing CodeStyle analyzers do not provide Features analyzers fallback options #75534
Merged
JoeRobich
merged 7 commits into
release/dev17.12
from
dev/jorobich/redirect-feature-analyzers
Oct 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a838b9
Add a HasSdkCodeStyleAnalyzers property to ProjectAttributes
JoeRobich 180455c
Track unmapped AnalyzerReferences added by the project system.
JoeRobich 03eccdd
Selectively treat some Host analyzers as Project analyzers.
JoeRobich e9f7c04
Allow the DiagnosticComputer to treat some Host analyzers as Project …
JoeRobich 1198405
Added integration tests
JoeRobich 7344695
PR feedback
JoeRobich dff04fe
Backport test skip from #75612
JoeRobich 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.Linq; | ||
using Roslyn.Utilities; | ||
|
||
|
@@ -32,18 +33,84 @@ public IEnumerable<StateSet> GetAllHostStateSets() | |
private HostAnalyzerStateSets GetOrCreateHostStateSets(Project project, ProjectAnalyzerStateSets projectStateSets) | ||
{ | ||
var key = new HostAnalyzerStateSetKey(project.Language, project.Solution.SolutionState.Analyzers.HostAnalyzerReferences); | ||
var hostStateSets = ImmutableInterlocked.GetOrAdd(ref _hostAnalyzerStateMap, key, CreateLanguageSpecificAnalyzerMap, project.Solution.SolutionState.Analyzers); | ||
// Some Host Analyzers may need to be treated as Project Analyzers so that they do not have access to the | ||
// Host fallback options. These ids will be used when building up the Host and Project analyzer collections. | ||
var referenceIdsToRedirect = GetReferenceIdsToRedirectAsProjectAnalyzers(project); | ||
var hostStateSets = ImmutableInterlocked.GetOrAdd(ref _hostAnalyzerStateMap, key, CreateLanguageSpecificAnalyzerMap, (project.Solution.SolutionState.Analyzers, referenceIdsToRedirect)); | ||
return hostStateSets.WithExcludedAnalyzers(projectStateSets.SkippedAnalyzersInfo.SkippedAnalyzers); | ||
|
||
static HostAnalyzerStateSets CreateLanguageSpecificAnalyzerMap(HostAnalyzerStateSetKey arg, HostDiagnosticAnalyzers hostAnalyzers) | ||
static HostAnalyzerStateSets CreateLanguageSpecificAnalyzerMap(HostAnalyzerStateSetKey arg, (HostDiagnosticAnalyzers HostAnalyzers, ImmutableHashSet<object> ReferenceIdsToRedirect) state) | ||
{ | ||
var language = arg.Language; | ||
var analyzersPerReference = hostAnalyzers.GetOrCreateHostDiagnosticAnalyzersPerReference(language); | ||
var analyzersPerReference = state.HostAnalyzers.GetOrCreateHostDiagnosticAnalyzersPerReference(language); | ||
|
||
var analyzerMap = CreateStateSetMap(language, [], analyzersPerReference.Values, includeWorkspacePlaceholderAnalyzers: true); | ||
var (hostAnalyzerCollection, projectAnalyzerCollection) = GetAnalyzerCollections(analyzersPerReference, state.ReferenceIdsToRedirect); | ||
var analyzerMap = CreateStateSetMap(language, projectAnalyzerCollection, hostAnalyzerCollection, includeWorkspacePlaceholderAnalyzers: true); | ||
|
||
return new HostAnalyzerStateSets(analyzerMap); | ||
} | ||
|
||
static (IEnumerable<ImmutableArray<DiagnosticAnalyzer>> HostAnalyzerCollection, IEnumerable<ImmutableArray<DiagnosticAnalyzer>> ProjectAnalyzerCollection) GetAnalyzerCollections( | ||
ImmutableDictionary<object, ImmutableArray<DiagnosticAnalyzer>> analyzersPerReference, | ||
ImmutableHashSet<object> referenceIdsToRedirectAsProjectAnalyzers) | ||
{ | ||
if (referenceIdsToRedirectAsProjectAnalyzers.IsEmpty) | ||
{ | ||
return (analyzersPerReference.Values, []); | ||
} | ||
|
||
var hostAnalyzerCollection = new List<ImmutableArray<DiagnosticAnalyzer>>(); | ||
var projectAnalyzerCollection = new List<ImmutableArray<DiagnosticAnalyzer>>(); | ||
|
||
foreach (var kvp in analyzersPerReference) | ||
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. Deconstruct: |
||
{ | ||
if (referenceIdsToRedirectAsProjectAnalyzers.Contains(kvp.Key)) | ||
{ | ||
projectAnalyzerCollection.Add(kvp.Value); | ||
} | ||
else | ||
{ | ||
hostAnalyzerCollection.Add(kvp.Value); | ||
} | ||
} | ||
|
||
return (hostAnalyzerCollection, projectAnalyzerCollection); | ||
} | ||
} | ||
|
||
private static readonly ImmutableHashSet<string> FeaturesAnalyzerFileNames = [ | ||
"Microsoft.CodeAnalysis.Features.dll", | ||
"Microsoft.CodeAnalysis.CSharp.Features.dll", | ||
"Microsoft.CodeAnalysis.VisualBasic.Features.dll", | ||
]; | ||
|
||
private static ImmutableHashSet<object> GetReferenceIdsToRedirectAsProjectAnalyzers(Project project) | ||
{ | ||
if (project.State.HasSdkCodeStyleAnalyzers) | ||
{ | ||
// When a project uses CodeStyle analyzers added by the SDK, we remove them in favor of the | ||
// Features analyzers. We need to then treat the Features analyzers as Project analyzers so | ||
// they do not get access to the Host fallback options. | ||
return GetFeaturesAnalyzerReferenceIds(project.Solution.SolutionState.Analyzers); | ||
} | ||
|
||
return []; | ||
|
||
static ImmutableHashSet<object> GetFeaturesAnalyzerReferenceIds(HostDiagnosticAnalyzers hostAnalyzers) | ||
{ | ||
var builder = ImmutableHashSet.CreateBuilder<object>(); | ||
|
||
foreach (var analyzerReference in hostAnalyzers.HostAnalyzerReferences) | ||
{ | ||
var fileName = Path.GetFileName(analyzerReference.FullPath)!; | ||
if (FeaturesAnalyzerFileNames.Contains(fileName)) | ||
{ | ||
builder.Add(analyzerReference.Id); | ||
} | ||
} | ||
|
||
return builder.ToImmutable(); | ||
} | ||
} | ||
|
||
private sealed class HostAnalyzerStateSets | ||
|
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.
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.
def don't like returning these as lazy enumerbles. can you make both an
ImmutableArray<ImmutableArray<...
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.
I could but then we have to do something about the common case where we aren't redirecting and are returning
analyzersPerReference.Values
which is anIEnumerable<ImmutableArray<DiagnosticAnalyzer>>
. The use of these IEnumerable's is also piped intoStateManager.CreateStateSetMap
. This might be cleanup for a follow up PR.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.
Ick :) Can that one change?
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.
at the very least, don't return mutable lists. return an immutable array here. that way we don't have to worry about it changing.
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.
Updated to use ArrayBuilder instead of List.