-
Notifications
You must be signed in to change notification settings - Fork 53
Add Roslyn analyzer for non-contextual logger usage in orchestrations (DURABLE0010) #553
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
+412
−0
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
58fe068
Initial plan
Copilot cbff2ac
Add LoggerOrchestrationAnalyzer implementation and tests
Copilot 088cc67
Fix LoggerOrchestrationAnalyzer to handle ILogger<T> and avoid duplic…
Copilot df1db68
Simplify IsILoggerType method per code review feedback
Copilot 024efe3
Merge branch 'main' into copilot/alert-non-contextual-logger
YunchuWang 71d8fa7
Update src/Analyzers/Orchestration/LoggerOrchestrationAnalyzer.cs
YunchuWang f29b505
Combine nested if statements in LoggerOrchestrationAnalyzer
Copilot d87ce97
Potential fix for pull request finding 'Missed opportunity to use Where'
YunchuWang 7590aa6
Potential fix for pull request finding 'Missed opportunity to use Where'
YunchuWang 4d782fe
Merge branch 'main' into copilot/alert-non-contextual-logger
YunchuWang 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
146 changes: 146 additions & 0 deletions
146
src/Analyzers/Orchestration/LoggerOrchestrationAnalyzer.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,146 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
| using static Microsoft.DurableTask.Analyzers.Orchestration.LoggerOrchestrationAnalyzer; | ||
|
|
||
| namespace Microsoft.DurableTask.Analyzers.Orchestration; | ||
|
|
||
| /// <summary> | ||
| /// Analyzer that reports a warning when a non-contextual ILogger is used in an orchestration method. | ||
| /// </summary> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class LoggerOrchestrationAnalyzer : OrchestrationAnalyzer<LoggerOrchestrationVisitor> | ||
| { | ||
| /// <summary> | ||
| /// Diagnostic ID supported for the analyzer. | ||
| /// </summary> | ||
| public const string DiagnosticId = "DURABLE0009"; | ||
|
|
||
| static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.LoggerOrchestrationAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); | ||
| static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.LoggerOrchestrationAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); | ||
|
|
||
| static readonly DiagnosticDescriptor Rule = new( | ||
| DiagnosticId, | ||
| Title, | ||
| MessageFormat, | ||
| AnalyzersCategories.Orchestration, | ||
| DiagnosticSeverity.Warning, | ||
| isEnabledByDefault: true); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Rule]; | ||
|
|
||
| /// <summary> | ||
| /// Visitor that inspects the method body for ILogger usage. | ||
| /// </summary> | ||
| public sealed class LoggerOrchestrationVisitor : MethodProbeOrchestrationVisitor | ||
| { | ||
| INamedTypeSymbol? iLoggerSymbol; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override bool Initialize() | ||
| { | ||
| this.iLoggerSymbol = this.KnownTypeSymbols.ILogger; | ||
| if (this.iLoggerSymbol == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void VisitMethod(SemanticModel semanticModel, SyntaxNode methodSyntax, IMethodSymbol methodSymbol, string orchestrationName, Action<Diagnostic> reportDiagnostic) | ||
| { | ||
| IOperation? methodOperation = semanticModel.GetOperation(methodSyntax); | ||
| if (methodOperation is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Track which parameters we've already reported on to avoid duplicates | ||
| HashSet<IParameterSymbol> reportedParameters = new(SymbolEqualityComparer.Default); | ||
|
|
||
| // Check for ILogger parameters in the method signature | ||
| foreach (IParameterSymbol parameter in methodSymbol.Parameters) | ||
| { | ||
| if (this.IsILoggerType(parameter.Type)) | ||
| { | ||
| // Found an ILogger parameter - report diagnostic at the parameter location | ||
| if (parameter.DeclaringSyntaxReferences.Length > 0) | ||
| { | ||
| SyntaxNode parameterSyntax = parameter.DeclaringSyntaxReferences[0].GetSyntax(); | ||
| reportDiagnostic(RoslynExtensions.BuildDiagnostic(Rule, parameterSyntax, methodSymbol.Name, orchestrationName)); | ||
| reportedParameters.Add(parameter); | ||
| } | ||
YunchuWang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
github-code-quality[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||
| } | ||
github-code-quality[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
YunchuWang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Check for ILogger field or property references (but not parameter references, as those were already reported) | ||
| foreach (IOperation descendant in methodOperation.Descendants()) | ||
| { | ||
| ITypeSymbol? typeToCheck = null; | ||
| SyntaxNode? syntaxNode = null; | ||
|
|
||
| switch (descendant) | ||
| { | ||
| case IFieldReferenceOperation fieldRef: | ||
| typeToCheck = fieldRef.Field.Type; | ||
| syntaxNode = fieldRef.Syntax; | ||
| break; | ||
| case IPropertyReferenceOperation propRef: | ||
| typeToCheck = propRef.Property.Type; | ||
| syntaxNode = propRef.Syntax; | ||
| break; | ||
| case IParameterReferenceOperation paramRef: | ||
| // Skip parameter references that we already reported on in the parameter list | ||
| if (reportedParameters.Contains(paramRef.Parameter)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| typeToCheck = paramRef.Parameter.Type; | ||
| syntaxNode = paramRef.Syntax; | ||
| break; | ||
| } | ||
|
|
||
| if (typeToCheck != null && syntaxNode != null && this.IsILoggerType(typeToCheck)) | ||
| { | ||
| reportDiagnostic(RoslynExtensions.BuildDiagnostic(Rule, syntaxNode, methodSymbol.Name, orchestrationName)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool IsILoggerType(ITypeSymbol type) | ||
| { | ||
| if (this.iLoggerSymbol == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // First check for exact match with ILogger | ||
| if (SymbolEqualityComparer.Default.Equals(type, this.iLoggerSymbol)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Check if the type implements ILogger interface (covers ILogger<T> case) | ||
| if (type is INamedTypeSymbol namedType) | ||
| { | ||
| foreach (INamedTypeSymbol interfaceType in namedType.AllInterfaces) | ||
| { | ||
| if (SymbolEqualityComparer.Default.Equals(interfaceType, this.iLoggerSymbol)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
github-code-quality[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
github-code-quality[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
YunchuWang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
github-code-quality[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.