Skip to content

Commit

Permalink
fix analyzer exception 'Syntax node is not within syntax tree' (#307)
Browse files Browse the repository at this point in the history
During orchestration method analysis, since the syntax tree of the callee method might be different from the caller method, we need to get the correct semantic model when they differ.
  • Loading branch information
allantargino authored May 6, 2024
1 parent 8c544bc commit aa810cc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Analyzers/Orchestration/OrchestrationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,12 @@ void FindInvokedMethods(
IEnumerable<MethodDeclarationSyntax> calleeSyntaxes = calleeMethodSymbol.GetSyntaxNodes();
foreach (MethodDeclarationSyntax calleeSyntax in calleeSyntaxes)
{
this.FindInvokedMethods(semanticModel, calleeSyntax, calleeMethodSymbol, rootOrchestration, reportDiagnostic);
// 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 ?
semanticModel : semanticModel.Compilation.GetSemanticModel(calleeSyntax.SyntaxTree);

this.FindInvokedMethods(sm, calleeSyntax, calleeMethodSymbol, rootOrchestration, reportDiagnostic);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ public class MyOrchestrator : ITaskOrchestrator
await VerifyCS.VerifyDurableTaskAnalyzerAsync(code, expected);
}

[Fact]
public async Task TaskOrchestratorUsingDifferentSyntaxTreesHasDiag()
{
string mainCode = Wrapper.WrapTaskOrchestrator(@"
public class MyOrchestrator : TaskOrchestrator<string, DateTime>
{
public override Task<DateTime> RunAsync(TaskOrchestrationContext context, string input)
{
var dependency = new Dependency();
return Task.FromResult(dependency.Method());
}
}
");
string codeInAnotherSyntaxTree = Wrapper.WrapTaskOrchestrator(@"
public class Dependency{
public DateTime Method() => {|#0:DateTime.Now|};
}
");

void configureTest(VerifyCS.Test test) => test.TestState.Sources.Add(codeInAnotherSyntaxTree);

DiagnosticResult expected = BuildDiagnostic().WithLocation(0).WithArguments("Method", "System.DateTime.Now", "MyOrchestrator");

await VerifyCS.VerifyDurableTaskAnalyzerAsync(mainCode, configureTest, expected);
}


[Fact]
public async Task FuncOrchestratorWithLambdaHasDiag()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public static partial class CSharpAnalyzerVerifier<TAnalyzer>
{
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.VerifyAnalyzerAsync(string, DiagnosticResult[])"/>
public static async Task VerifyDurableTaskAnalyzerAsync(string source, params DiagnosticResult[] expected)
{
await VerifyDurableTaskAnalyzerAsync(source, null, expected);
}

public static async Task VerifyDurableTaskAnalyzerAsync(string source, Action<Test>? configureTest = null, params DiagnosticResult[] expected)
{
Test test = new()
{
Expand All @@ -24,6 +29,8 @@ public static async Task VerifyDurableTaskAnalyzerAsync(string source, params Di

test.ExpectedDiagnostics.AddRange(expected);

configureTest?.Invoke(test);

await test.RunAsync(CancellationToken.None);
}
}

0 comments on commit aa810cc

Please sign in to comment.