Open KalleOlaviNiemitalo opened 1 year ago
I get the same error if I add a global.json file that specifies .NET SDK 6.0.413. However, even with that global.json, the "lib\netstandard2.0\dotnet-Microsoft.XmlSerializer.Generator.dll" tool in the package appears to be run by the .NET 7 runtime rather than .NET 6, so it uses the C# code generator from .NET 7. This issue then seems related to https://github.com/dotnet/runtime/pull/66914, which shipped in .NET 7 and not in .NET 6.
More generally, if new versions of System.Private.Xml start generating code that requires a new version of C# or a new library, then there should be a way to keep old versions of Microsoft.XmlSerializer.Generator using old versions of System.Private.Xml, so that just installing a new version of .NET SDK side by side won't break people's builds.
Too bad you cannot simply change this dotnet Microsoft.XmlSerializer.Generator
invocation here
to run dotnet --fx-version 6.0 Microsoft.XmlSerializer.Generator
instead, ensuring that it uses a runtime that generates C# code for a compatible language version; that syntax apparently requires a path-to-application argument that is a path rather than just a tool name.
Description
If .NET 7 Runtime has been installed, then the MSBuild integration in Microsoft.XmlSerializer.Generator generates code that uses
ref struct
types like this:It then runs the
Csc
task to compile the generated code, but it does not specify the C# language version. If the project is being built in Visual Studio 2017, then the C# language version defaults to 7.0, which does not allowref struct
types, and the serializer assembly fails to build. This happens even if the project that references Microsoft.XmlSerializer.Generator sets<LangVersion>7.2</LangVersion>
for itself.Reproduction Steps
XmlSer.csproj:
Root.cs:
Build in Visual Studio 2017; either in the IDE, or with MSBuild.exe.
Expected behavior
The XML serializer assembly should be built without warnings.
Actual behavior
Regression?
Yes! Uninstalling .NET 7 Runtime and keeping .NET 6 SDK makes the build work again. (The bug is reproducible even without .NET 7 SDK, if .NET 7 Runtime is installed.)
Known Workarounds
Add to the project file:
But this seems risky because the
CscRspFile
name might be also used in packages other than Microsoft.XmlSerializer.Generator.Configuration
Microsoft.XmlSerializer.Generator 6.0.0. The
dotnet Microsoft.XmlSerializer.Generator
command is run using .NET SDK 7.0.400. Visual Studio 2017 version 15.9.56, in whichCsc
is Microsoft (R) Visual C# Compiler version 2.10.0.0 (b9fb1610). Windows 10 version 22H2 on amd64.dotnet --info
``` C:\Projects>dotnet --info .NET SDK: Version: 7.0.400 Commit: 73bf45718d Runtime Environment: OS Name: Windows OS Version: 10.0.19045 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.400\ Host: Version: 7.0.10 Architecture: x64 Commit: a6dbb800a4 .NET SDKs installed: 2.1.526 [C:\Program Files\dotnet\sdk] 6.0.413 [C:\Program Files\dotnet\sdk] 7.0.110 [C:\Program Files\dotnet\sdk] 7.0.400 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Other architectures found: None Environment variables: Not set global.json file: Not found Learn more: https://aka.ms/dotnet/info Download .NET: https://aka.ms/dotnet/download ```Other information
The specific incompatibility due to
ref struct
types (introduced in https://github.com/dotnet/runtime/pull/66914) could be addressed by making Microsoft.XmlSerializer.Generator set the C# language version for theCsc
task invocations, here:https://github.com/dotnet/runtime/blob/3bda6e0013ddb5b48a7b2a89fd84bf4fbbed0e37/src/libraries/Microsoft.XmlSerializer.Generator/src/build/Microsoft.XmlSerializer.Generator.targets#L53-L54
Based on Csc Task documentation,
LangVersion="7.2"
should do the job. I think this should not depend on the$(LangVersion)
of the referencing project, because the generated C# code doesn't depend on that either.However, there may be further incompatibilities coming up, if even higher versions of .NET Runtime are installed.