-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Implement an AppContext compatibility switch re-enabling reflection fallback in STJ source generators. #75615
Conversation
…allback in sourcegen.
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis 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
|
cc @terrajobst @bartonjs @GrabYourPitchforks who might be opinionated on the compatibility switch name. |
src/libraries/System.Text.Json/src/System/Text/Json/AppContextSwitchHelper.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Show resolved
Hide resolved
There was a problem hiding this 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)
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? |
src/libraries/System.Text.Json/src/System/Text/Json/AppContextSwitchHelper.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/AppContextSwitchHelper.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
The fallback logic is only exercised in serialization methods already marked RequiresUnreferencedCode, so the breaking change should have no impact on trimmability. |
/backport to release/7.0 |
Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/3061785141 |
@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! |
…allback in STJ source generators. (dotnet#75615) * Implement an AppContext compatibility switch re-enabling reflection fallback in sourcegen. * address feedback
.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 (namedSystem.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