dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.26k stars 4.73k forks source link

Microsoft.XmlSerializer.Generator added *.XmlSerialization.all to nuget package which causes CS0436 #107288

Open TiltonJH opened 2 months ago

TiltonJH commented 2 months ago

Description

Beginning with the Microsoft.XmlSerializer.Generator 7.* or greater (#2335; #73550), the generated *.XmlSerialization.all are added to the nuget package in the lib\*\ folder. Thereby, those become part of the compilation process down the line.

This will cause an CS0436 on another project also using Microsoft.XmlSerializer.Generator.

Reproduction Steps

If one creates two libraries both using the Microsoft.XmlSerializer.Generator and lib.B depends on lib.A via NuGet (package reference), than lib.B will have an CS0436.

Expected behavior

No warning. (Obviously 😉)

Add the generated *.XmlSerialization.dll not to the lib folder, but rather to the build folder including a .targets file which will ensure that the *.XmlSerialization.all is copied to the output directory (for example).

Actual behavior

Warning (active) CS0436 The type 'XmlSerializationReader1' in [...]\Lib.B\obj\Debug\net8.0\Lib.B.XmlSerializers.cs' conflicts with the imported type 'XmlSerializationReader1' in 'Lib.A, Version=[...] Culture=neutral, PublicKeyToken=null'.
Using the type defined in '[...]\Lib.B\obj\Debug\net8.0\Lib.B.XmlSerializers.cs'.

Regression?

Microsoft.XmlSerializer.Generator 6.0.0 work fine, because it did not add the generated *.XmlSerialization.dll to the nuget package.

Known Workarounds

Use the Microsoft.XmlSerializer.Generator 6.0.0.

Configuration

net6.0 / net8.0; VS 2022

Other information

No response

TiltonJH commented 2 months ago

Example *.targets file for the nuget build dir as mentioned above:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <None Include="$(MSBuildThisFileDirectory)*.XmlSerializers.dll"
              Link="%(Filename)%(Extension)"
              Visible="false">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>
</Project>