diff --git a/src/Analyzers/Orchestration/OrchestrationAnalyzer.cs b/src/Analyzers/Orchestration/OrchestrationAnalyzer.cs index a299d5777..342b1e6c0 100644 --- a/src/Analyzers/Orchestration/OrchestrationAnalyzer.cs +++ b/src/Analyzers/Orchestration/OrchestrationAnalyzer.cs @@ -141,7 +141,14 @@ public override void Initialize(AnalysisContext context) case IMethodReferenceOperation methodReferenceOperation: // use the method reference as the method symbol methodSymbol = methodReferenceOperation.Method; - methodSyntax = methodReferenceOperation.Method.DeclaringSyntaxReferences.First().GetSyntax(); + + // Only get syntax for methods in the current project (skip external assemblies) + // If IsEmpty (external method), methodSyntax stays null and we skip analysis below + if (!methodReferenceOperation.Method.DeclaringSyntaxReferences.IsEmpty) + { + methodSyntax = methodReferenceOperation.Method.DeclaringSyntaxReferences.First().GetSyntax(); + } + break; default: break; @@ -316,6 +323,13 @@ void FindInvokedMethods( IEnumerable calleeSyntaxes = calleeMethodSymbol.GetSyntaxNodes(); foreach (MethodDeclarationSyntax calleeSyntax in calleeSyntaxes) { + // Check if the syntax tree is part of the current compilation before trying to get its semantic model. + // Extension methods from external assemblies won't be in the compilation, so skip them. + if (!semanticModel.Compilation.ContainsSyntaxTree(calleeSyntax.SyntaxTree)) + { + continue; + } + // Since the syntax tree of the callee method might be different from the caller method, we need to get the correct semantic model, // avoiding the exception 'Syntax node is not within syntax tree'. SemanticModel sm = semanticModel.SyntaxTree == calleeSyntax.SyntaxTree ? diff --git a/src/Analyzers/RoslynExtensions.cs b/src/Analyzers/RoslynExtensions.cs index 10339f7a4..597013334 100644 --- a/src/Analyzers/RoslynExtensions.cs +++ b/src/Analyzers/RoslynExtensions.cs @@ -115,6 +115,13 @@ public static bool BaseTypeIsConstructedFrom(this INamedTypeSymbol symbol, IType /// The collection of syntax nodes of a given method symbol. public static IEnumerable GetSyntaxNodes(this IMethodSymbol methodSymbol) { + // If the method has no syntax references (e.g., extension methods from external assemblies), + // return empty to skip analysis rather than throwing ArgumentException. + if (methodSymbol.DeclaringSyntaxReferences.IsEmpty) + { + return Enumerable.Empty(); + } + return methodSymbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax()).OfType(); }