microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2.1k stars 90 forks source link

.NET SDK 6.0.302 breaks the build when using cswin32 #622

Closed drelyea3 closed 2 years ago

drelyea3 commented 2 years ago

Actual behavior

When a new project is created using .NET 6.0.302 (or after the .NET SDK is upgraded to 6.0.302), compilation errors complaining about duplicate definitions are generated.

C:\Test\cswin32new\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.SourceGenerator\Windows.Win32.MONITOR_FROM_FLAGS.g.cs(22,17)
: error CS0101: The namespace 'Windows.Win32.Graphics.Gdi' already contains a definition for 'MONITOR_FROM_FLAGS' [C:\Test\cswin32n
ew\cswin32new_5xwvzd4u_wpftmp.csproj]
C:\Test\cswin32new\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.SourceGenerator\Windows.Win32.PInvoke.User32.g.cs(38,58): er
ror CS0111: Type 'PInvoke' already defines a member called 'MonitorFromWindow' with the same parameter types [C:\Test\cswin32new\cs
win32new_5xwvzd4u_wpftmp.csproj]

...and so on.

Expected behavior

App should continue to build and run as before.

Repro steps

dotnet new wpf dotnet add package Microsoft.Windows.CsWin32 --prerelease

  1. NativeMethods.txt content:

    HWND
    MonitorFromWindow
  2. NativeMethods.json content (if present):

  3. Any of your own code that should be shared?

public MainWindow()
{
    InitializeComponent();

    var helper = new WindowInteropHelper(this);
    var hmonitor = Windows.Win32.PInvoke.MonitorFromWindow(
        (Windows.Win32.Foundation.HWND)helper.Handle, 
        Windows.Win32.Graphics.Gdi.MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONULL);
}

Context

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.10-beta">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

Before (working):

C:\Test\cswin32>dotnet sdk check
.NET SDKs:
Version      Status
----------------------------------------
6.0.201      Patch 6.0.203 is available.

Try out the newest .NET SDK features with .NET 7.0.100-preview.6.22352.1.

.NET Runtimes:
Name                              Version      Status
--------------------------------------------------------------------------
Microsoft.AspNetCore.App          5.0.15       .NET 5.0 is out of support.
Microsoft.NETCore.App             5.0.15       .NET 5.0 is out of support.
Microsoft.WindowsDesktop.App      5.0.15       .NET 5.0 is out of support.
Microsoft.AspNetCore.App          6.0.3        Patch 6.0.7 is available.
Microsoft.NETCore.App             6.0.3        Patch 6.0.7 is available.
Microsoft.WindowsDesktop.App      6.0.3        Patch 6.0.7 is available.

After (not working):

C:\Test\cswin32>dotnet sdk check
.NET SDKs:
Version      Status
----------------------------------------
6.0.201      Patch 6.0.203 is available.
6.0.302      Up to date.

Try out the newest .NET SDK features with .NET 7.0.100-preview.6.22352.1.

.NET Runtimes:
Name                              Version      Status
--------------------------------------------------------------------------
Microsoft.AspNetCore.App          5.0.15       .NET 5.0 is out of support.
Microsoft.NETCore.App             5.0.15       .NET 5.0 is out of support.
Microsoft.WindowsDesktop.App      5.0.15       .NET 5.0 is out of support.
Microsoft.AspNetCore.App          6.0.3        Patch 6.0.7 is available.
Microsoft.NETCore.App             6.0.3        Patch 6.0.7 is available.
Microsoft.WindowsDesktop.App      6.0.3        Patch 6.0.7 is available.
Microsoft.AspNetCore.App          6.0.7        Up to date.
Microsoft.NETCore.App             6.0.7        Up to date.
Microsoft.WindowsDesktop.App      6.0.7        Up to date.
AArnott commented 2 years ago

Duplicate of #601