runtime icon indicating copy to clipboard operation
runtime copied to clipboard

STJ source generation fails to compile without referencing the STJ package

Open database64128 opened this issue 3 years ago • 8 comments

Description

Use STJ source generation with this Settings class causes build to fail with:

 D:\a\youtube-dl-wpf\youtube-dl-wpf\YoutubeDl.Wpf\Models\SettingsJsonSerializerContext.cs(9,22): error CS0534: 'SettingsJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GeneratedSerializerOptions.get' [D:\a\youtube-dl-wpf\youtube-dl-wpf\YoutubeDl.Wpf\YoutubeDl.Wpf_hlx5rijb_wpftmp.csproj]
 D:\a\youtube-dl-wpf\youtube-dl-wpf\YoutubeDl.Wpf\Models\SettingsJsonSerializerContext.cs(9,22): error CS0534: 'SettingsJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)' [D:\a\youtube-dl-wpf\youtube-dl-wpf\YoutubeDl.Wpf\YoutubeDl.Wpf_hlx5rijb_wpftmp.csproj]

However, this can be worked around by referencing the STJ package in the project, even though the project already targets net6.0-windows10.0.22000.0.

Reproduction Steps

I attempted to make a minimal reproduction repo by copying the whole Settings class, target net6.0-windows10.0.22000.0 and enable UseWPF. It just won't reproduce. This bug can only be reproduced in this specific repo.

Expected behavior

Build succeeds.

Actual behavior

Build fails.

Regression?

No response

Known Workarounds

No response

Configuration

❯ dotnet --version
6.0.202

Other information

No response

database64128 avatar May 03 '22 12:05 database64128

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Use STJ source generation with this Settings class causes build to fail with:

 D:\a\youtube-dl-wpf\youtube-dl-wpf\YoutubeDl.Wpf\Models\SettingsJsonSerializerContext.cs(9,22): error CS0534: 'SettingsJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GeneratedSerializerOptions.get' [D:\a\youtube-dl-wpf\youtube-dl-wpf\YoutubeDl.Wpf\YoutubeDl.Wpf_hlx5rijb_wpftmp.csproj]
 D:\a\youtube-dl-wpf\youtube-dl-wpf\YoutubeDl.Wpf\Models\SettingsJsonSerializerContext.cs(9,22): error CS0534: 'SettingsJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)' [D:\a\youtube-dl-wpf\youtube-dl-wpf\YoutubeDl.Wpf\YoutubeDl.Wpf_hlx5rijb_wpftmp.csproj]

However, this can be worked around by referencing the STJ package in the project, even though the project already targets net6.0-windows10.0.22000.0.

Reproduction Steps

I attempted to make a minimal reproduction repo by copying the whole Settings class, target net6.0-windows10.0.22000.0 and enable UseWPF. It just won't reproduce. This bug can only be reproduced in this specific repo.

Expected behavior

Build succeeds.

Actual behavior

Build fails.

Regression?

No response

Known Workarounds

No response

Configuration

❯ dotnet --version
6.0.202

Other information

No response

Author: database64128
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

msftbot[bot] avatar May 03 '22 12:05 msftbot[bot]

Does the source generator produce any warning or error messages when you build? Does it fail to compile in the IDE, the command line, or both? Unfortunately, we would really need a minimal reproduction in order to take a closer look. I would recommend creating a separate branch in your repo, slowly removing components until the issue no longer reproduces.

eiriktsarpalis avatar May 04 '22 08:05 eiriktsarpalis

This issue has been marked needs-author-action and may be missing some important information.

msftbot[bot] avatar May 04 '22 08:05 msftbot[bot]

Does the source generator produce any warning or error messages when you build?

No, the source generator does not emit any error messages. The 2 CS0534 errors are the only error messages.

Does it fail to compile in the IDE, the command line, or both?

It fails on both (my local VS2022 IDE and GitHub Actions).

database64128 avatar May 04 '22 09:05 database64128

I would recommend creating a separate branch in your repo, slowly removing components until the issue no longer reproduces.

Thank you for the tip! Took me an hour to figure out which part of my app was causing this. I made a minimal reproduction repo: https://github.com/database64128/WPFRxUISTJSourceGenerationMinimalReproduction. There are 4 commits:

  1. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@1cfa42a81dc3a17de085f10bafccf9f24dbdb8b4: Fresh WPF app with STJ source generation: no issues
  2. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@561c4c16469558c1d32aa55ed3ae23f4c210ca34: Add RxUI and use ReactiveObject: no issues
  3. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@743bea978e39695e12d07bb08a5f3875ec38ff75: Switch from WPF's Window to RxUI's ReactiveWindow: build fails.
  4. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@b6705875106c04a5231243ac99da6fd05fa5d161: Add package reference to System.Text.Json: build succeeds.

I also opened an issue at RxUI: reactiveui/ReactiveUI#3255

database64128 avatar May 04 '22 12:05 database64128

I also ran into what seems to be a very similar or even the same issue, I am seeing the exact same error messages in the following scenario: I have a WPF project with a XAML file that references a string from a ResX file. The same project contains a System.Text.Json source generator serializer context. And equally, the error messages go away when adding a PackageReference to System.Text.Json.

The minimal working example for my error case is here: https://github.com/ssrmm/mwe-system.text.json-wpftmp-error

The readme file inside the repository contains some more details as well.

To me it seems that the source generator from the runtime/SDK does not work together correctly with he *_wpftmp.csproj projects in certain cases.

ssrmm avatar May 05 '22 16:05 ssrmm

Something seems to have changed since recently. The behavior is now different for certain VS/.NET SDK versions. In those versions the minimal example I posted above does compile without errors. However now there is a different problem: Referencing the NuGet package causes errors instead. The _wpftmp.csproj file now has other errors and complains about duplicate definitions of the source generated types.

Full error logs

  Determining projects to restore...
  All projects are up-to-date for restore.
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(10,6): error CS0579: Duplicate 'global::System.CodeDom.Compiler.GeneratedCodeAttribute' attribute [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Int32.g.cs(11,101): error CS0102: The type 'FooSerializerContext' already contains a definition for '_Int32' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Int32.g.cs(12,99): error CS0102: The type 'FooSerializerContext' already contains a definition for 'Int32' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Foo.g.cs(11,99): error CS0102: The type 'FooSerializerContext' already contains a definition for '_Foo' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Foo.g.cs(12,97): error CS0102: The type 'FooSerializerContext' already contains a definition for 'Foo' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Foo.g.cs(43,91): error CS0111: Type 'FooSerializerContext' already defines a member called 'FooPropInit' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Foo.g.cs(73,29): error CS0111: Type 'FooSerializerContext' already defines a member called 'FooSerializeHandler' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(14,71): error CS0102: The type 'FooSerializerContext' already contains a definition for 's_defaultOptions' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(23,61): error CS0102: The type 'FooSerializerContext' already contains a definition for 's_defaultContext' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(24,59): error CS0102: The type 'FooSerializerContext' already contains a definition for 'Default' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(26,76): error CS0102: The type 'FooSerializerContext' already contains a definition for 'GeneratedSerializerOptions' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(28,16): error CS0111: Type 'FooSerializerContext' already defines a member called 'FooSerializerContext' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(32,16): error CS0111: Type 'FooSerializerContext' already defines a member called 'FooSerializerContext' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(36,71): error CS0111: Type 'FooSerializerContext' already defines a member called 'GetRuntimeProvidedCustomConverter' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.GetJsonTypeInfo.g.cs(11,86): error CS0111: Type 'FooSerializerContext' already defines a member called 'GetTypeInfo' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]

Build FAILED.

<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(10,6): error CS0579: Duplicate 'global::System.CodeDom.Compiler.GeneratedCodeAttribute' attribute [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Int32.g.cs(11,101): error CS0102: The type 'FooSerializerContext' already contains a definition for '_Int32' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Int32.g.cs(12,99): error CS0102: The type 'FooSerializerContext' already contains a definition for 'Int32' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Foo.g.cs(11,99): error CS0102: The type 'FooSerializerContext' already contains a definition for '_Foo' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Foo.g.cs(12,97): error CS0102: The type 'FooSerializerContext' already contains a definition for 'Foo' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Foo.g.cs(43,91): error CS0111: Type 'FooSerializerContext' already defines a member called 'FooPropInit' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.Foo.g.cs(73,29): error CS0111: Type 'FooSerializerContext' already defines a member called 'FooSerializeHandler' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(14,71): error CS0102: The type 'FooSerializerContext' already contains a definition for 's_defaultOptions' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(23,61): error CS0102: The type 'FooSerializerContext' already contains a definition for 's_defaultContext' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(24,59): error CS0102: The type 'FooSerializerContext' already contains a definition for 'Default' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(26,76): error CS0102: The type 'FooSerializerContext' already contains a definition for 'GeneratedSerializerOptions' [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(28,16): error CS0111: Type 'FooSerializerContext' already defines a member called 'FooSerializerContext' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(32,16): error CS0111: Type 'FooSerializerContext' already defines a member called 'FooSerializerContext' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.g.cs(36,71): error CS0111: Type 'FooSerializerContext' already defines a member called 'GetRuntimeProvidedCustomConverter' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
<Path>\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\FooSerializerContext.GetJsonTypeInfo.g.cs(11,86): error CS0111: Type 'FooSerializerContext' already defines a member called 'GetTypeInfo' with the same parameter types [<Path>\WpfApp_3hcrzqv1_wpftmp.csproj]
    0 Warning(s)
    15 Error(s)
Compiles... ...with PackageReference ...without PackageReference
VS 17.0.12
SDK 6.0.107
X
VS 17.1.1
SDK 6.0.201
X
VS 17.2.6
SDK 6.0.302
X

This is especially annoying when you are in an environment with multiple different VS/SDK version in use. Some machines will have compile errors when including the package and some will have erros when not including it.

ssrmm avatar Aug 05 '22 09:08 ssrmm

~~For me it's completely broken on all versions, with or without referencing STJ.~~

Update: It was unrelated to this issue. reactivemarbles/ObservableEvents#130, dotnet/wpf#6792.

database64128 avatar Aug 06 '22 14:08 database64128

~For me it's completely broken on all versions, with or without referencing STJ.~

Update: It was unrelated to this issue. reactivemarbles/ObservableEvents#130, dotnet/wpf#6792.

@database64128 Can you clarify if you still have an unsolved issue here? Thanks!

jeffhandley avatar Aug 15 '22 05:08 jeffhandley

This issue has been marked needs-author-action and may be missing some important information.

msftbot[bot] avatar Aug 15 '22 05:08 msftbot[bot]

@jeffhandley I noticed that this future item is still marked as blocking-release. Is that still the case?

jeffschwMSFT avatar Aug 22 '22 17:08 jeffschwMSFT

Sorry for the delay. I can confirm this particular issue has been fixed in the latest SDK version.

database64128 avatar Aug 22 '22 17:08 database64128