dotnet / linker

389 stars 127 forks source link

COMAnalyzer NullReferenceException in Maui Essentials #2686

Closed eerhardt closed 2 years ago

eerhardt commented 2 years ago

I'm getting the following error when setting IsTrimmable=true for the Maui Essentials project and building with msbuild Essentials.csproj -p:Configuration=Release -t:Rebuild /bl:

    Errors
        CSC error AD0001: Analyzer 'ILLink.RoslynAnalyzer.COMAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. [C:\git\maui\src\Essentials\src\Essentials.csproj]
    Exception occurred with following context:
    Compilation: Microsoft.Maui.Essentials
    IOperation: Invocation
    SyntaxTree: C:\git\maui\src\Essentials\src\DeviceDisplay\DeviceDisplay.uwp.cs
    SyntaxNode: EnumDisplaySettings(mi.Value.DeviceNameToLPTStr ... [InvocationExpressionSyntax]@[2465..2533) (101,3)-(101,71)
    System.NullReferenceException: Object reference not set to an instance of an object.
    at ILLink.RoslynAnalyzer.COMAnalyzer.<Initialize>g__IsComInterop|6_1(ISymbol symbol)
    at ILLink.RoslynAnalyzer.COMAnalyzer.<>c.<Initialize>b__6_2(OperationAnalysisContext operationContext)
    at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteOperationAction>b__63_0(ValueTuple`2 data)
    at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
    -----
    Suppress the following diagnostics to disable this analyzer: IL2050
    CompilerServer: server - server processed compilation - 1477aa4a-10c1-4109-b699-b092d24b4231

This looks like https://github.com/dotnet/linker/issues/2622, but I believe it is a different issue because this method isn't using a pointer, and because the version of the ILLink.RoslynAnalyzer assembly I'm using is from https://github.com/dotnet/linker/commits/e9cfb5413a6a7a7b5bfc3b9a73671be2b18642cf, which has the fix for #2622.

To Repro

  1. git clone https://github.com/dotnet/maui
  2. check out this commit: https://github.com/dotnet/maui/pull/5028/commits/7a6d62fce07c0b086f6c8671c422f48f38523a07
  3. C:\git\maui\src\Essentials\src>msbuild Essentials.csproj -p:Configuration=Release -t:Rebuild /bl

cc @tlakollo

tannergooding commented 2 years ago

Also encountering this in some of my personal projects: https://github.com/terrafx/terrafx.interop.windows/runs/5545589775?check_suite_focus=true

jtschuster commented 2 years ago

Thanks for filing the issue! It looks like the root of these issues is the Roslyn API for symbols doesn't use nullable annotations, but the linker assumes that fields without the ? will not be null. Usually this isn't a problem, but when we deal with COM interop, there are a lot of types without e.g. a namespace, and we run into issues there.

In particular, the MAUI issue seemed to be caused by an array type being used with COM interop. There is a fix here that should address the issue.