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
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
CC @amanasifkhalid.
@agocke, please see if this is an ILLInk related issue.
@jtschuster could you take a look?
I'm unable to reproduce this. Does this occur in VS only, or command line builds too? What version for the .NET SDK and VS are you using?
@jtschuster This repros for me just now with Visual Studio Version 17.13.0 Preview 1.0 [35429.394.main].
It can be helpful to filter your Error List to just AD0001 warnings to find it:
dotnet --info
dotnet --info
.NET SDK:
Version: 8.0.403
Commit: c64aa40a71
Workload version: 8.0.400-manifests.42a6cf86
MSBuild version: 17.11.9+a69bbaaf5
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.403\
.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.
Host:
Version: 9.0.0-rc.2.24473.5
Architecture: x64
Commit: 990ebf52fc
.NET SDKs installed:
8.0.403 [C:\Program Files\dotnet\sdk]
9.0.100-rc.2.24474.11 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0-rc.2.24474.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0-rc.2.24473.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.0-rc.2.24474.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
D:\source\MessagePack-CSharp\global.json
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
I'm trying to figure out how to install 17.13.0 Preview 1.0, but on 17.13.0 Preview 2.0, the IL2075 warning location is MessagePack-CSharp\src\MessagePack\Resolvers\DynamicObjectResolver.cs:401, which is where it should be and is in the compilation. Maybe this is a Roslyn issue that got resolved between preview versions? If you upgrade to Preview 2.0, does it persist?
@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, 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, Action1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)
I'm not able to repro the issue either - @AArnott are you able to repro this on another machine?
I've already repro'd it on 2 distinct machines.
Still can't repro on a fresh dev box. However, I do notice that the IL2075 warning shows up in a command-line build, but not in the error list. Maybe the AD0001 is getting swallowed somehow in our setup. @jtschuster do you see the warning in the error list or on the command-line?
I see the IL2075 in DynamicObjectResolver.cs in the VS error list, but only (usually) on a rebuild or (consistently) after a git clean -xdf.
FWIW I see the AD0001 error without any build at all. Just open the sln on a clean repo and wait a couple minutes. A flood of entries appear in the Error List, so I then apply the column filter to show only the (1) AD0001 error and it shows up.
Warnings don't show up for me until I open source files. When I open DynamicObjectResolver.cs and MessagePack.Annotations\Attributes.cs, with no build I still can't get the AD0001. Which files do you have open when you get the AD0001 error? Or do you have a setting that turns on analysis on for unopened files?
Or do you have a setting that turns on analysis on for unopened files?
I definitely have this setting in my IDE.
Thank you, that did it, I can repro now. Now to investigate why..
@jtschuster did you end up getting any more insight into this? If you still have a repro handy it would be great if you could share it for future reference.
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.