runtime icon indicating copy to clipboard operation
runtime copied to clipboard

DynamicallyAccessedMembersAnalyzer throws an internal exception due to source location outside compilation

Open AArnott opened this issue 1 year ago • 13 comments

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

AArnott avatar Oct 11 '24 20:10 AArnott

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.

ghost avatar Oct 11 '24 20:10 ghost

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.

ghost avatar Oct 11 '24 20:10 ghost

CC @amanasifkhalid.

JulieLeeMSFT avatar Nov 06 '24 20:11 JulieLeeMSFT

@agocke, please see if this is an ILLInk related issue.

JulieLeeMSFT avatar Nov 06 '24 20:11 JulieLeeMSFT

@jtschuster could you take a look?

agocke avatar Nov 08 '24 21:11 agocke

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 avatar Nov 11 '24 18:11 jtschuster

@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: Image

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

AArnott avatar Nov 11 '24 18:11 AArnott

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 avatar Nov 13 '24 15:11 jtschuster

@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)

AArnott avatar Nov 13 '24 17:11 AArnott

I'm not able to repro the issue either - @AArnott are you able to repro this on another machine?

sbomer avatar Nov 13 '24 21:11 sbomer

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

AArnott avatar Nov 13 '24 22:11 AArnott

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?

sbomer avatar Nov 13 '24 23:11 sbomer

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.

jtschuster avatar Nov 14 '24 13:11 jtschuster

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.

AArnott avatar Nov 14 '24 17:11 AArnott

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?

jtschuster avatar Nov 14 '24 17:11 jtschuster

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

I definitely have this setting in my IDE.

AArnott avatar Nov 14 '24 18:11 AArnott

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

jtschuster avatar Nov 14 '24 18:11 jtschuster

@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.

sbomer avatar Jun 10 '25 18:06 sbomer

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.

jtschuster avatar Jun 10 '25 19:06 jtschuster