Skip to content

Make PrepareTrimConfiguration a public extension point#127253

Open
sbomer wants to merge 2 commits intodotnet:mainfrom
sbomer:il3050
Open

Make PrepareTrimConfiguration a public extension point#127253
sbomer wants to merge 2 commits intodotnet:mainfrom
sbomer:il3050

Conversation

@sbomer
Copy link
Copy Markdown
Member

@sbomer sbomer commented Apr 21, 2026

Note

This PR was created with assistance from AI.

Summary

Rename _PrepareTrimConfiguration to PrepareTrimConfiguration so that external SDKs can hook BeforeTargets="PrepareTrimConfiguration" to add RuntimeHostConfigurationOption items before they are snapshotted into _TrimmerFeatureSettings.

Problem

PR #124801 extracted the RuntimeHostConfigurationOption_TrimmerFeatureSettings conversion from PrepareForILLink into a new internal target _PrepareTrimConfiguration. This target runs as a transitive dependency of PrepareForILLink (via _ComputeManagedAssemblyToLink), so it executes before BeforeTargets="PrepareForILLink" hooks fire.

MAUI adds its feature switches (e.g. IsHybridWebViewSupported) via BeforeTargets="PrepareForILLink". After #124801, these items arrive after _PrepareTrimConfiguration has already snapshotted RuntimeHostConfigurationOption into _TrimmerFeatureSettings. As a result, ILC never receives --feature flags for MAUI's switches on Android NativeAOT (where RunILLink=true), causing spurious IL3050 warnings.

Fix

Make PrepareTrimConfiguration a public target so SDKs have a stable, intentional hook point for adding feature switch configuration that flows to both ILLink and ILC. MAUI (and other SDKs) can then use BeforeTargets="PrepareTrimConfiguration" to add their RuntimeHostConfigurationOption items before the snapshot.

Verified

Tested with a MAUI Android NativeAOT app:

  • Before: --feature:Microsoft.Maui.RuntimeFeature.IsHybridWebViewSupported=false missing from ILC rsp → IL3050 emitted
  • After: --feature flag present → IL3050 suppressed by FeatureGuard

Fixes #127017

cc @sbomer @simonrozsival

Copilot AI review requested due to automatic review settings April 21, 2026 23:16
@github-actions github-actions Bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Apr 21, 2026
@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Apr 21, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

Rename _PrepareTrimConfiguration to PrepareTrimConfiguration so that
external SDKs (e.g. MAUI) can hook BeforeTargets="PrepareTrimConfiguration"
to add RuntimeHostConfigurationOption items before they are snapshotted
into _TrimmerFeatureSettings.

PR dotnet#124801 extracted the RHCO → _TrimmerFeatureSettings conversion from
PrepareForILLink into _PrepareTrimConfiguration, which runs as a
dependency of PrepareForILLink. This caused MAUI's feature switches
(added via BeforeTargets="PrepareForILLink") to arrive after the
snapshot, so ILC never received --feature flags for switches like
IsHybridWebViewSupported. The result was spurious IL3050 warnings on
Android NativeAOT where RunILLink=true.

Making the target public gives SDKs a stable, intentional hook point
for adding feature switch configuration that flows to both ILLink and
ILC.

Fixes dotnet#127017

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Assisted-by: Claude:claude-opus-4.6-1m
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 makes the trim configuration target a public MSBuild extension point so external SDKs can reliably add RuntimeHostConfigurationOption items before they’re snapshotted into _TrimmerFeatureSettings (and thus flow into both ILLink and ILC/NativeAOT). It also includes a linker marking behavior change around type-forwarders, with new tests covering the scenarios.

Changes:

  • Rename _PrepareTrimConfiguration to PrepareTrimConfiguration and retarget dependencies to the new public name.
  • Update NativeAOT publish targets to depend on / order around PrepareTrimConfiguration.
  • Adjust ILLink MarkStep behavior for type-forwarding scenarios and add new test cases (plus ILTrim expected-failure entries).

Reviewed changes

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

Show a summary per file
File Description
src/tools/illink/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets Renames target to PrepareTrimConfiguration and documents it as an extension point; updates dependent target to use new name.
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets Switches dependencies and ordering from _PrepareTrimConfiguration to PrepareTrimConfiguration.
src/tools/illink/src/linker/Linker.Steps/MarkStep.cs Changes marking behavior for exported types/type references in rooted/copy scenarios.
src/tools/illink/test/Mono.Linker.Tests.Cases/TypeForwarding/RootedForwarderWithExportedTypesIsHandled.cs New linker test for rooted forwarder assemblies with exported types.
src/tools/illink/test/Mono.Linker.Tests.Cases/TypeForwarding/UsedForwarderIsRemovedWhenReferencedByRootedAssembly.cs New linker test ensuring forwarder removal even when the referencing assembly is rooted.
src/coreclr/tools/ILTrim.Tests/ILTrimExpectedFailures.txt Adds the two new TypeForwarding tests to ILTrim’s expected-failure list.

Comment thread src/tools/illink/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets Outdated
Comment thread src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 21, 2026 23:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 makes PrepareTrimConfiguration a public MSBuild extension point so external SDKs (e.g., MAUI) can reliably inject RuntimeHostConfigurationOption items before those settings are snapshotted into _TrimmerFeatureSettings and used by both ILLink and ILC (NativeAOT).

Changes:

  • Renamed the internal target _PrepareTrimConfiguration to PrepareTrimConfiguration in ILLink targets and documented it as a public hook point.
  • Updated NativeAOT publish target dependencies/orderings to reference PrepareTrimConfiguration instead of _PrepareTrimConfiguration.

Reviewed changes

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

File Description
src/tools/illink/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets Exposes PrepareTrimConfiguration as a public target and wires _ComputeManagedAssemblyToLink to depend on it.
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets Updates NativeAOT pipeline targets to depend on / schedule around the newly public PrepareTrimConfiguration.

@MichalStrehovsky
Copy link
Copy Markdown
Member

Why is MAUI adding this in a target? This sounds like just asking for trouble. Is it guaranteed that this will end up in runtimeconfig.json in non-trimmed scenarios?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Tools-ILLink .NET linker development as well as trimming analyzers linkable-framework Issues associated with delivering a linker friendly framework

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[ILC/Android NativeAOT] FeatureGuard does not suppress IL3050 after PR #124801 decoupled ILC from ManagedAssemblyToLink

3 participants