Skip to content

NullReferenceException in ConvertComImportToGeneratedComInterfaceAnalyzer #94968

Closed

Description

@meziantou

Description

Visual Studio reports AD0001 Analyzer 'Microsoft.Interop.Analyzers.ConvertComImportToGeneratedComInterfaceAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.

image

Note that dotnet build doesn't report any warning.

Reproduction Steps

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
</Project>
using System.Runtime.InteropServices;

namespace ClassLibrary1;

[ComImport()]
[Guid("42f85136-db7e-439c-85f1-e4075d135fc8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IFileDialog
{
    [PreserveSig]
    int Show([In] nint parent);
}

[ComImport()]
[Guid("d57c7288-d4ad-4768-be02-9d969532d960")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IFileOpenDialog : IFileDialog
{
    [PreserveSig]
    new int Show([In] nint parent);
}

[ComImport]
[Guid("d57c7288-d4ad-4768-be02-9d969532d960")]
[CoClass(typeof(FileOpenDialogRCW))]
internal interface NativeFileOpenDialog : IFileOpenDialog
{
}

[ComImport]
[ClassInterface(ClassInterfaceType.None)]
[Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
internal class FileOpenDialogRCW
{
}

Expected behavior

No warning in Visual Studio

Actual behavior

Warning	AD0001	Analyzer 'Microsoft.Interop.Analyzers.ConvertComImportToGeneratedComInterfaceAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
Exception occurred with following context:
Compilation: ClassLibrary1
ISymbol: NativeFileOpenDialog (NamedType)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Interop.Analyzers.ConvertComImportToGeneratedComInterfaceAnalyzer.<>c__DisplayClass3_0.<Initialize>b__2(SymbolAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

Regression?

No response

Known Workarounds

No response

Configuration

Visual Studio version: 17.9.0 Preview 1.0

dotnet --info

.NET SDK:
 Version:           8.0.100
 Commit:            57efcf1350
 Workload version:  8.0.100-manifests.71b9f198

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22635
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100\

.NET workloads installed:
 Workload version: 8.0.100-manifests.71b9f198
There are no installed workloads to display.

Host:
  Version:      8.0.0
  Architecture: x64
  Commit:       5535e31a71

.NET SDKs installed:
  8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.0 [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]

Other information

No response

Activity

  1. ghost added
    needs-area-label
    on Nov 19, 2023
  2. ghost added
    untriaged
    on Nov 19, 2023
  3. added and removed
    needs-area-label
    on Nov 19, 2023
  4. jtschuster commented on Nov 21, 2023

    @jtschuster
    Member

    I can't reproduce this error with the same setup on VS Community 17.9.0 Preview 1. Did you hit this while editing code, or when running a build in VS?

  5. removed
    untriaged
    on Nov 21, 2023
  6. meziantou commented on Nov 21, 2023

    @meziantou
    ContributorAuthor
    • It only repro in VS. The warning appears a few seconds after pasting the code from the initial message (no need to run a build). You need to select "IntelliSense" or "Build + IntelliSense" in the Error window.
    • The CLI doesn't report any error

    Full VS version: Microsoft Visual Studio Enterprise 2022 (64-bit) - Preview, Version 17.9.0 Preview 1.0

  7. added
    code-analyzer
    and removed on Nov 21, 2023
  8. added this to the 9.0.0 milestone on Nov 21, 2023
  9. powercode commented on Dec 13, 2023

    @powercode

    Same in Preview 2.0 Professional.

  10. ghost added
    in-pr
    on Feb 19, 2024
  11. ghost removed
    in-pr
    on Feb 21, 2024
  12. locked and limited conversation to collaborators on Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtime.InteropServicescode-analyzer

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions