runtime: DynamicallyAccessedMembersAnalyzer throws an internal exception due to source location outside compilation

Describe the bug

In enabling the AOT readiness analyzers, the MessagePack codebase produces a lot of warnings. Among them is an AD0001 warning about a failed analyzer. See Exceptions section for details.

To Reproduce

git clone https://github.com/MessagePack-CSharp/MessagePack-CSharp.git
cd MessagePack-CSharp
git checkout dotnet/sdk/bug44120repro
.\MessagePack.sln

Inspect the error list, filtering to AD0001 entries.

Exceptions (if any)

Analyzer ‘ILLink.RoslynAnalyzer.DynamicallyAccessedMembersAnalyzer’ threw an exception of type ‘System.ArgumentException’ with message ‘Reported diagnostic ‘IL2075’ has a source location in file ‘Q:\source\MessagePack-CSharp\src\MessagePack.Annotations\Attributes.cs’, which is not part of the compilation being analyzed. (Parameter ‘diagnostic’)’. Exception occurred with following context: Compilation: MessagePack ISymbol: BuildCustomFormatterField (Method)

System.ArgumentException: Reported diagnostic 'IL2075' has a source location in file 'Q:\source\MessagePack-CSharp\src\MessagePack.Annotations\Attributes.cs', which is not part of the compilation being analyzed. (Parameter 'diagnostic')
   at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyDiagnosticLocationInCompilation(String id, Location location, Compilation compilation)
   at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyDiagnosticLocationsInCompilation(Diagnostic diagnostic, Compilation compilation)
   at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyArguments(Diagnostic diagnostic, Compilation compilation, Func`3 isSupportedDiagnostic, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Diagnostics.OperationBlockAnalysisContext.ReportDiagnostic(Diagnostic diagnostic)
   at ILLink.RoslynAnalyzer.DynamicallyAccessedMembersAnalyzer.<>c.<Initialize>b__8_1(OperationBlockAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

Further technical details

  • Include the output of dotnet --info
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 19 (17 by maintainers)

Most upvoted comments

I wasn’t able to find the root cause. I haven’t checked recent builds, but the repro in the original message worked once I set VS to run analysis on unopened files.

Thank you, that did it, I can repro now. Now to investigate why…

Or do you have a setting that turns on analysis on for unopened files?

I definitely have this setting in my IDE.

I’ve already repro’d it on 2 distinct machines.

@jtschuster The bug still repros on 17.13 Preview 2 (35511.228.main):

Analyzer ‘ILLink.RoslynAnalyzer.DynamicallyAccessedMembersAnalyzer’ threw an exception of type ‘System.ArgumentException’ with message ‘Reported diagnostic ‘IL2075’ has a source location in file ‘D:\source\MessagePack-CSharp\src\MessagePack.Annotations\Attributes.cs’, which is not part of the compilation being analyzed. (Parameter ‘diagnostic’)’. Exception occurred with following context: Compilation: MessagePack ISymbol: BuildCustomFormatterField (Method)

System.ArgumentException: Reported diagnostic ‘IL2075’ has a source location in file ‘D:\source\MessagePack-CSharp\src\MessagePack.Annotations\Attributes.cs’, which is not part of the compilation being analyzed. (Parameter ‘diagnostic’) at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyDiagnosticLocationInCompilation(String id, Location location, Compilation compilation) at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyDiagnosticLocationsInCompilation(Diagnostic diagnostic, Compilation compilation) at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyArguments(Diagnostic diagnostic, Compilation compilation, Func3 isSupportedDiagnostic, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Diagnostics.OperationBlockAnalysisContext.ReportDiagnostic(Diagnostic diagnostic) at ILLink.RoslynAnalyzer.DynamicallyAccessedMembersAnalyzer.<>c.<Initialize>b__8_1(OperationBlockAnalysisContext context) at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

@jtschuster could you take a look?