Skip to content

Fix STJ unsafe accessor detection with polyfilled attributes - #136463

Merged
eiriktsarpalis merged 1 commit into
dotnet:mainfrom
eiriktsarpalis:eiriktsarpalis-stj-polyfill-detection
Oct 26, 2026
Merged

eiriktsarpalis merged 1 commit into
dotnet:mainfrom
eiriktsarpalis:eiriktsarpalis-stj-polyfill-detection

Conversation

@eiriktsarpalis

Copy link
Copy Markdown
Member

The System.Text.Json source generator treats polyfilled UnsafeAccessorAttribute definitions as evidence of runtime support. Referencing an assembly such as BenchmarkDotNet can therefore cause a .NET Framework project to emit unsupported unsafe accessors, even without using any APIs from that assembly. A polyfilled OverloadResolutionPriorityAttribute can similarly cause generic unsafe accessors to be emitted on .NET 8.

Require both capability markers to come from the target compilation's core library, identified by the assembly defining System.Object. Other type lookups are unchanged. Adds one generator unit-test theory covering source and referenced polyfills with public and internal visibility.

Validation: .\dotnet.cmd build src\libraries\System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn4.4.Unit.Tests.csproj -t:Test -f net481 -p:RuntimeConfiguration=Release '-p:XunitMethodName=*UnsafeAccessorPolyfill*' passed all four cases. Standalone public API checks also passed for net472 and .NET 8 with source/referenced and public/internal marker polyfills.

Resolves #136457

Note

This pull request was generated with GitHub Copilot.

Require core-library definitions for unsafe accessor capability markers in the System.Text.Json source generator.

Co-authored-by: Copilot App <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@eiriktsarpalis
eiriktsarpalis merged commit ffed239 into dotnet:main Oct 26, 2026
87 checks passed
@eiriktsarpalis

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 27, 2026
ViveliDuCh pushed a commit that referenced this pull request Nov 14, 2026
…ibutes (#136473)

Backport of #136463 to release/11.0

/cc @eiriktsarpalis

## Customer Impact

- [x] Customer reported
- [ ] Found internally

In .NET 11, the STJ source generator added support for inaccessible
members (supported historically in the reflection-based serializer via
the `JsonIncludeAttribute` opt in) by way of unsafe accessors. The
generator checks for the availability of unsafe accessors in a target
framework by looking up the unsafe accessor attribute itself. It appears
however that the popular polyfill library PolySharp (used among others
by BenchmarkDotNet) is creating polyfills for targets that do not
natively support unsafe accessors, resulting in code that that fails at
runtime.

This PR strengthens the unsafe accessor availability heuristic against
such polyfills.

## Regression

- [x] Yes
- [ ] No

## Testing

Added testing validating the scenario.

## Risk

Low. Makes a straightforward update to the platform detection heuristic.

Co-authored-by: Eirik Tsarpalis <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Text.Json 11 rc.3 breaks source generation into net472 projects

2 participants