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

Implement an AppContext compatibility switch re-enabling reflection fallback in STJ source generators. #75615

Merged

Conversation

eiriktsarpalis
Copy link
Member

@eiriktsarpalis eiriktsarpalis commented Sep 14, 2022

.NET 7 introduced an intentional breaking change which removes silent fallback to reflection-based serialization in System.Text.Json source generators. Based on early feedback we have been receiving from customers and partner teams, it appears that quite a few users have (mostly accidentally) taken a dependency on the fallback behavior (see dotnet/aspnetcore#43894, dotnet/aspnetcore#43236 for a few examples).

Even though a workaround for the breaking change has been documented, it still requires a code change which might not be possible in certain scenaria. This PR introduces an AppContext compatibility switch (named System.Text.Json.Serialization.EnableSourceGenReflectionFallback) that brings back the reflection fallback logic for source generators.

Should be backported to release/7.0. We should also update the documentation added in dotnet/docs#31132 to incorporate this switch.

cc @ericstj @davidfowl @brunolins16

@ghost
Copy link

ghost commented Sep 14, 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

.NET 7 introduced an intentional breaking change which removes silent fallback to reflection-based serialization in System.Text.Json source generators. Based on early feedback we have been receiving from customers, it appears that quite a few users have (mostly accidentally) taken a dependency on the fallback behavior (see dotnet/aspnetcore#43894, dotnet/aspnetcore#43236 for more details).

Even though a workaround for the breaking change has been documented, it still requires a code change which might not be possible in certain scenaria. This PR introduces an AppContext compatibility switch (named System.Text.Json.Serialization.EnableSourceGenReflectionFallback) that brings back the reflection fallback logic for source generators.

Author: eiriktsarpalis
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis eiriktsarpalis added this to the 7.0.x milestone Sep 14, 2022
@eiriktsarpalis
Copy link
Member Author

cc @terrajobst @bartonjs @GrabYourPitchforks who might be opinionated on the compatibility switch name.

@eiriktsarpalis eiriktsarpalis modified the milestones: 7.0.x, 8.0.0 Sep 14, 2022
Copy link
Member

@krwq krwq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, we should consider creating issue to kill the switch once 6.0 gets out of support (.NET 9 I believe)

@jkoritzinsky
Copy link
Member

Do we need to add a feature flag for the linker to ensure the reflection path can still be trimmed away, or was the reflection fallback still not trimmable after the breaking change?

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@eiriktsarpalis
Copy link
Member Author

Do we need to add a feature flag for the linker to ensure the reflection path can still be trimmed away, or was the reflection fallback still not trimmable after the breaking change?

The fallback logic is only exercised in serialization methods already marked RequiresUnreferencedCode, so the breaking change should have no impact on trimmability.

@eiriktsarpalis eiriktsarpalis merged commit 6b2349a into dotnet:main Sep 15, 2022
@eiriktsarpalis eiriktsarpalis deleted the appcontext-reflection-fallback branch September 15, 2022 15:39
@eiriktsarpalis
Copy link
Member Author

/backport to release/7.0

@github-actions
Copy link
Contributor

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/3061785141

@github-actions
Copy link
Contributor

@eiriktsarpalis backporting to release/7.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Implement an AppContext compatibility switch re-enabling reflection fallback in sourcegen.
Using index info to reconstruct a base tree...
M	src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
M	src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
Auto-merging src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
CONFLICT (content): Merge conflict in src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Implement an AppContext compatibility switch re-enabling reflection fallback in sourcegen.
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

eiriktsarpalis added a commit to eiriktsarpalis/runtime that referenced this pull request Sep 15, 2022
…allback in STJ source generators. (dotnet#75615)

* Implement an AppContext compatibility switch re-enabling reflection fallback in sourcegen.

* address feedback
carlossanlop pushed a commit that referenced this pull request Sep 16, 2022
…allback in STJ source generators. (#75615) (#75694)

* Implement an AppContext compatibility switch re-enabling reflection fallback in sourcegen.

* address feedback
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants