Skip to content

Preserve HybridCacheEntryOptions.ToDistributedCacheEntryOptions from trimming#130403

Open
svick wants to merge 1 commit into
dotnet:mainfrom
svick:preserve-hybridcacheentryoptions-todistributed
Open

Preserve HybridCacheEntryOptions.ToDistributedCacheEntryOptions from trimming#130403
svick wants to merge 1 commit into
dotnet:mainfrom
svick:preserve-hybridcacheentryoptions-todistributed

Conversation

@svick

@svick svick commented Jul 9, 2026

Copy link
Copy Markdown
Member

Fixes #130376

Summary

In .NET 11, Microsoft.Extensions.Caching.Abstractions was added to the base shared framework (dotnet/dotnet#5206, decision in #67487). As a result, its assembly is now ILLink-trimmed as part of the framework, whereas previously it shipped only as an untrimmed NuGet package.

HybridCacheEntryOptions.ToDistributedCacheEntryOptions() is internal and has no caller inside its own assembly. Its only consumer is the Microsoft.Extensions.Caching.Hybrid implementation package, which reaches it via [UnsafeAccessor]. The trimmer cannot follow cross-assembly UnsafeAccessor references (they resolve by name at runtime, with no metadata edge), so the method was trimmed out of the shared-framework build. This surfaces as a MissingMethodException at runtime for the Hybrid cache.

Fix

  • Add ILLink.Descriptors.xml to preserve the method (auto-embedded via eng/illink.targets).
  • Add a trimming test that mirrors the Hybrid UnsafeAccessor access pattern and returns 100 only when the method resolves.

Validation

Verified with a full self-contained TrimMode=full publish:

  • With the descriptor: the published app exits 100; the method is present in the trimmed assembly.
  • Without the descriptor: the trimming-test harness reports [Failed Test] with exit code -532462766 (0xE0434352, the MissingMethodException), confirming the test guards the fix.

Note

This pull request was authored by Copilot (AI).

…trimming

In .NET 11, Microsoft.Extensions.Caching.Abstractions was added to the base
shared framework (dotnet/dotnet#5206, decision in dotnet#67487), so its
assembly is now ILLink-trimmed in the framework. Previously it shipped only as
an untrimmed NuGet package.

HybridCacheEntryOptions.ToDistributedCacheEntryOptions is internal and has no
in-assembly caller; its only consumer is Microsoft.Extensions.Caching.Hybrid,
which reaches it via [UnsafeAccessor]. The trimmer cannot follow cross-assembly
UnsafeAccessor references, so the method was removed from the shared-framework
build, causing a MissingMethodException at runtime.

Add an ILLink.Descriptors.xml entry to preserve the method, plus a trimming
test that exercises the UnsafeAccessor access pattern and fails if the method
is trimmed away.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 9, 2026 11:35
@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jul 9, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@svick svick marked this pull request as ready for review July 9, 2026 11:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR ensures an internal helper on HybridCacheEntryOptions remains present after framework trimming, since it’s accessed cross-assembly via [UnsafeAccessor] and therefore has no static metadata edge for the trimmer to follow.

Changes:

  • Add an ILLink descriptor to preserve HybridCacheEntryOptions.ToDistributedCacheEntryOptions().
  • Add a trimming test console app that uses [UnsafeAccessor] to validate the method survives trimming and behaves as expected.
  • Add a new trimming test project entry for Microsoft.Extensions.Caching.Abstractions.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/libraries/Microsoft.Extensions.Caching.Abstractions/tests/TrimmingTests/Microsoft.Extensions.Caching.Abstractions.TrimmingTests.proj Registers the new trimming console-app test source file for the trimming test harness.
src/libraries/Microsoft.Extensions.Caching.Abstractions/tests/TrimmingTests/HybridCacheEntryOptionsAccessorTest.cs Exercises [UnsafeAccessor] access to ToDistributedCacheEntryOptions and validates expected option propagation.
src/libraries/Microsoft.Extensions.Caching.Abstractions/src/ILLink/ILLink.Descriptors.xml Preserves the internal HybridCacheEntryOptions.ToDistributedCacheEntryOptions method during trimming.

svick added a commit to svick/runtime that referenced this pull request Jul 9, 2026
The LibraryBuild descriptor is only applied during the library's own build-time
trim pass and is not embedded into the shipping assembly, so it does not protect
ToDistributedCacheEntryOptions against downstream app trimming (the shared-framework
scenario in dotnet#130376). That is fixed separately and correctly via an
embedded ILLink.Descriptors.xml in dotnet#130403, so this file is removed
to avoid redundancy and a merge collision.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Extensions-Caching linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

2 participants