STJ source generation in a WPF project broken
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
Referencing certain NuGets in a Wpf project and using STJ source generation the build breaks.
### Reproduction Steps
1. create new WPF project
2. add reference to `Microsoft.Extensions.Hosting` or anything that indirectly depends on `System.Text.Json`
3. add the following source:
```c#
using System.Text.Json.Serialization;
[JsonSerializable(typeof(string[]))]
public partial class JsonInfo : JsonSerializerContext
{
}
```
4. build with `dotnet build`, VS, whatever
Minimal repro code: https://gist.github.com/bachratyg/09df487726946b05728d5129044ca574
### Expected behavior
Should build just fine
### Actual behavior
Output from dotnet build
```
MSBuild version 17.5.0+6f08c67f3 for .NET
Determining projects to restore...
Restored \WpfApp1.csproj (in 352 ms).
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(9,6): error CS0579: Duplicate 'global::System.CodeDom.Compiler.GeneratedCodeAttribute' attribute [\WpfApp1_oet233uf_wpftmp.csproj]
\JsonStuff.cs(4,22): error CS8646: 'IJsonTypeInfoResolver.GetTypeInfo(Type, JsonSerializerOptions)' is explicitly implemented more than once. [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.String.g.cs(10,102): error CS0102: The type 'JsonInfo' already contains a definition for '_String' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.String.g.cs(14,100): error CS0102: The type 'JsonInfo' already contains a definition for 'String' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.String.g.cs(19,101): error CS0111: Type 'JsonInfo' already defines a member called 'Create_String' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.StringArray.g.cs(10,104): error CS0102: The type 'JsonInfo' already contains a definition for '_StringArray' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.StringArray.g.cs(14,102): error CS0102: The type 'JsonInfo' already contains a definition for 'StringArray' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.StringArray.g.cs(19,103): error CS0111: Type 'JsonInfo' already defines a member called 'Create_StringArray' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.StringArray.g.cs(51,22): error CS0111: Type 'JsonInfo' already defines a member called 'StringArraySerializeHandler' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(13,71): error CS0102: The type 'JsonInfo' already contains a definition for 's_defaultOptions' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(22,42): error CS0102: The type 'JsonInfo' already contains a definition for 's_defaultContext' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(27,40): error CS0102: The type 'JsonInfo' already contains a definition for 'Default' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(32,76): error CS0102: The type 'JsonInfo' already contains a definition for 'GeneratedSerializerOptions' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(35,16): error CS0111: Type 'JsonInfo' already defines a member called 'JsonInfo' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(40,16): error CS0111: Type 'JsonInfo' already defines a member called 'JsonInfo' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(44,78): error CS0111: Type 'JsonInfo' already defines a member called 'GetRuntimeProvidedCustomConverter' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.GetJsonTypeInfo.g.cs(11,86): error CS0111: Type 'JsonInfo' already defines a member called 'GetTypeInfo' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.GetJsonTypeInfo.g.cs(26,141): error CS0111: Type 'JsonInfo' already defines a member called 'global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver.GetTypeInfo' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
Build FAILED.
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(9,6): error CS0579: Duplicate 'global::System.CodeDom.Compiler.GeneratedCodeAttribute' attribute [\WpfApp1_oet233uf_wpftmp.csproj]
\JsonStuff.cs(4,22): error CS8646: 'IJsonTypeInfoResolver.GetTypeInfo(Type, JsonSerializerOptions)' is explicitly implemented more than once. [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.String.g.cs(10,102): error CS0102: The type 'JsonInfo' already contains a definition for '_String' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.String.g.cs(14,100): error CS0102: The type 'JsonInfo' already contains a definition for 'String' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.String.g.cs(19,101): error CS0111: Type 'JsonInfo' already defines a member called 'Create_String' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.StringArray.g.cs(10,104): error CS0102: The type 'JsonInfo' already contains a definition for '_StringArray' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.StringArray.g.cs(14,102): error CS0102: The type 'JsonInfo' already contains a definition for 'StringArray' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.StringArray.g.cs(19,103): error CS0111: Type 'JsonInfo' already defines a member called 'Create_StringArray' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.StringArray.g.cs(51,22): error CS0111: Type 'JsonInfo' already defines a member called 'StringArraySerializeHandler' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(13,71): error CS0102: The type 'JsonInfo' already contains a definition for 's_defaultOptions' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(22,42): error CS0102: The type 'JsonInfo' already contains a definition for 's_defaultContext' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(27,40): error CS0102: The type 'JsonInfo' already contains a definition for 'Default' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(32,76): error CS0102: The type 'JsonInfo' already contains a definition for 'GeneratedSerializerOptions' [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(35,16): error CS0111: Type 'JsonInfo' already defines a member called 'JsonInfo' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(40,16): error CS0111: Type 'JsonInfo' already defines a member called 'JsonInfo' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.g.cs(44,78): error CS0111: Type 'JsonInfo' already defines a member called 'GetRuntimeProvidedCustomConverter' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.GetJsonTypeInfo.g.cs(11,86): error CS0111: Type 'JsonInfo' already defines a member called 'GetTypeInfo' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonInfo.GetJsonTypeInfo.g.cs(26,141): error CS0111: Type 'JsonInfo' already defines a member called 'global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver.GetTypeInfo' with the same parameter types [\WpfApp1_oet233uf_wpftmp.csproj]
0 Warning(s)
18 Error(s)
```
### Regression?
Might be related to #6792 (pull #6793, #6799)
### Known Workarounds
None that I found. Tried https://github.com/dotnet/wpf/pull/6680#issuecomment-1183552170 as suggested in #6792, didn't work
### Impact
Can't use STJ source generator together with libraries that also depend on STJ. E.g. `BackgroundService` from `Microsoft.Extensions.Hosting` or stuff from `Microsoft.Extensions.Configuration.Json`
### Configuration
Does not seem to be specific to configuration
dotnet --info
```
.NET SDK:
Version: 7.0.202
Commit: 6c74320bc3
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.202\
Host:
Version: 7.0.4
Architecture: x64
Commit: 0a396acafe
.NET SDKs installed:
7.0.202 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
arm64 [C:\Program Files\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\arm64\InstallLocation]
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
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
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.