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
1.99k stars 84 forks source link

CS8785 when adding a locally built copy of Windows.Win32.winmd #1176

Closed riverar closed 1 month ago

riverar commented 1 month ago

Actual behavior

After adding a reference to a custom projection metadata winmd, the source generator fails with the following warning:

CSC : warning CS8785: Generator 'SourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'An element with the same key but a different value already exists. Key: 'Windows.Win32'
<ItemGroup>
    <PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.49-beta">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <ProjectionMetadataWinmd Include="C:\Sources\win32metadata\bin\Windows.Win32.winmd" />
</ItemGroup>

Expected behavior

No warning / source generation to function

Repro steps

  1. Create a new baseline CsWin32-enabled C# project
  2. Attempt to refer to a custom .winmd on disk
  3. Observe warning/error

Context

riverar commented 1 month ago

Looks like I can cheat and use <CsWin32InputMetadataPaths> instead https://github.com/microsoft/CsWin32/blob/main/src/Microsoft.Windows.CsWin32/build/Microsoft.Windows.CsWin32.props#L22, but figured I'd run this past you.

AArnott commented 1 month ago

Updating the win32metadata projection is typically done with a PackageReference, which will then override the one brought in by the cswin32 dependency. But if you really want to bring in your locally built one directly, you can add a PackageReference to the public win32metadata package with ExcludeAssets="build" to turn off its self-advertising, so you only have one win32metadata projection being proffered into cswin32.

riverar commented 1 month ago

Thanks @aarnott. Feel free to close if this is out of scope.

AArnott commented 1 month ago

I think I'll improve the error reporting for this case.