[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving block code.#24936
Conversation
…marking when preserving block code. This makes it easier to move this code out of a custom linker step in the future. Contributes towards #17693.
There was a problem hiding this comment.
Pull request overview
This PR updates the dotnet-linker workflow to preserve block-related code using [DynamicDependency] attributes (via assembly rewriting) instead of manual marking, aligning with the effort to remove custom linker-step dependencies (issue #17693).
Changes:
- Introduces a new
PreserveBlockCodeStepthat injects[DynamicDependency]attributes to preserve theHandlerfield andInvokemethod for block trampolines. - Refactors assembly-rewriting steps by adding an
AssemblyModifierStepbase and anIsActiveForhook toConfigurationAwareStep. - Adds MSBuild toggles to choose between the new dynamic-dependency-based preservation and the legacy mark handler.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/dotnet-linker/Steps/PreserveBlockCodeStep.cs | New step that rewrites assemblies to preserve block code via dynamic dependencies. |
| tools/dotnet-linker/Steps/PreserveBlockCodeHandler.cs | Reuses the new member-detection helper; remains as fallback when not using dynamic dependencies. |
| tools/dotnet-linker/Steps/ConfigurationAwareStep.cs | Adds IsActiveFor(AssemblyDefinition) gating before processing assemblies. |
| tools/dotnet-linker/Steps/AssemblyModifierStep.cs | New base step to centralize AppBundleRewriter usage and nested-type traversal. |
| tools/dotnet-linker/PreserveSmartEnumConversionsStep.cs | Migrates to AssemblyModifierStep and uses the new activation hook. |
| tools/dotnet-linker/PreserveProtocolsStep.cs | Migrates to AssemblyModifierStep and uses the new activation hook. |
| tools/dotnet-linker/AppBundleRewriter.cs | Adds overload to create dynamic dependency attributes for methods + fixes param doc for fields. |
| dotnet/targets/Xamarin.Shared.Sdk.targets | Adds an MSBuild switch to enable dynamic-dependency-based block code preservation and conditionally disables the old handler. |
Comments suppressed due to low confidence (2)
tools/dotnet-linker/PreserveSmartEnumConversionsStep.cs:70
AssemblyModifierStepalready recurses into nested types, butProcessTypestill does its own nested-type recursion. This results in nested types being processed twice (and potentially adding the same dynamic dependencies multiple times, even if later deduped). Remove the recursion inProcessTypeand rely on the base class traversal.
protected override bool ProcessType (TypeDefinition type)
{
var modified = false;
if (type.HasNestedTypes) {
foreach (var nested in type.NestedTypes)
modified |= ProcessType (nested);
}
tools/dotnet-linker/PreserveProtocolsStep.cs:65
AssemblyModifierStepalready recursively walks nested types viaProcessTypeImpl, but this override also manually recurses intotype.NestedTypes. That will process nested types twice (extra Resolve()/attribute work). Remove the manual recursion here and letAssemblyModifierStephandle nested types.
protected override bool ProcessType (TypeDefinition type)
{
var modified = false;
if (type.HasNestedTypes) {
foreach (var nested in type.NestedTypes)
modified |= ProcessType (nested);
}
You can also share your feedback on Copilot code review. Take the survey.
| // The type is also nested inside ObjCRuntime.Trampolines class) | ||
| var nestingType = type.DeclaringType; | ||
| if (!nestingType.Is ("ObjCRuntime", "Trampolines")) | ||
| return false; | ||
|
|
||
| // The class has a readonly field named 'Handler' | ||
| field = type.Fields [0]; | ||
| if (!field.IsInitOnly) | ||
| return false; | ||
| if (field.Name != "Handler") | ||
| return false; | ||
|
|
||
| // The class has a parameterless 'Invoke' method with a 'MonoPInvokeCallback' attribute | ||
| if (!type.HasMethods) | ||
| return false; |
|
|
||
| public static bool GetMembersToPreserve (TypeDefinition type, [NotNullWhen (true)] out FieldDefinition? field, [NotNullWhen (true)] out MethodDefinition? method) | ||
| { |
| protected override string Name { get; } = "Preserve Block Code"; | ||
| protected override int ErrorCode { get; } = 2240; |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
🔥 [CI Build #efee7bd] Build failed (Build packages) 🔥Build failed for the job 'Build packages' (with job status 'Failed') Pipeline on Agent |
🔥 [PR Build #efee7bd] Build failed (Detect API changes) 🔥Build failed for the job 'Detect API changes' (with job status 'Failed') Pipeline on Agent |
|
🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire Pipeline on Agent |
This makes it easier to move this code out of a custom linker step in the future.
Contributes towards #17693.