Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STJ source generation fails to compile without referencing the STJ package #68810

Closed
database64128 opened this issue May 3, 2022 · 12 comments
Closed
Assignees
Milestone

Comments

@database64128
Copy link

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 3, 2022
@ghost
Copy link

ghost commented May 3, 2022

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: -

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented May 4, 2022

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 eiriktsarpalis added the needs-author-action An issue or pull request that requires more info or actions from the author. label May 4, 2022
@ghost
Copy link

ghost commented May 4, 2022

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

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label May 4, 2022
@database64128
Copy link
Author

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).

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels May 4, 2022
@database64128
Copy link
Author

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@1cfa42a: Fresh WPF app with STJ source generation: no issues
  2. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@561c4c1: Add RxUI and use ReactiveObject: no issues
  3. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@743bea9: Switch from WPF's Window to RxUI's ReactiveWindow: build fails.
  4. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@b670587: Add package reference to System.Text.Json: build succeeds.

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

@ssrmm
Copy link

ssrmm commented May 5, 2022

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.

@layomia layomia self-assigned this May 17, 2022
@layomia layomia added this to the 7.0.0 milestone May 17, 2022
@ssrmm
Copy link

ssrmm commented Aug 5, 2022

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.

database64128 added a commit to database64128/youtube-dl-wpf that referenced this issue Aug 6, 2022
This commit basically reverts 24c516e, because .NET doesn't seem to like this anymore. dotnet/runtime#68810
@database64128
Copy link
Author

database64128 commented Aug 6, 2022

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.

@jeffhandley
Copy link
Member

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 jeffhandley added needs-author-action An issue or pull request that requires more info or actions from the author. and removed needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration labels Aug 15, 2022
@ghost
Copy link

ghost commented Aug 15, 2022

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

@jeffhandley jeffhandley modified the milestones: 7.0.0, Future Aug 15, 2022
@jeffschwMSFT
Copy link
Member

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

@database64128
Copy link
Author

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

@eiriktsarpalis eiriktsarpalis removed blocking-release needs-author-action An issue or pull request that requires more info or actions from the author. labels Aug 22, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants