[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving block code.#24936
Conversation
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.
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.
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.
2d2685d to
d4e05fb
Compare
…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.
efee7bd to
68519d1
Compare
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.
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…tenumconversion' into dev/rolf/use-dynamic-dependency-attributes-preserveblockcode
✅ [PR Build #695e793] Build passed (Detect API changes) ✅Pipeline on Agent |
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
✅ [CI Build #695e793] Build passed (Build packages) ✅Pipeline on Agent |
✅ [CI Build #695e793] Build passed (Build macOS tests) ✅Pipeline on Agent |
🔥 [CI Build #695e793] Test results 🔥Test results❌ Tests failed on VSTS: test results 1 tests crashed, 31 tests failed, 120 tests passed. Failures❌ monotouch tests (iOS)9 tests failed, 2 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (MacCatalyst)12 tests failed, 3 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (macOS)9 tests failed, 3 tests passed.Failed tests
Html Report (VSDrops) Download ❌ Tests on macOS Sonoma (14) tests🔥 Failed catastrophically on VSTS: test results - mac_sonoma (no summary found). Html Report (VSDrops) Download ❌ Tests on macOS Tahoe (26) tests1 tests failed, 4 tests passed.Failed tests
Html Report (VSDrops) Download Successes✅ cecil: All 1 tests passed. Html Report (VSDrops) Download macOS tests✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
This makes it easier to move this code out of a custom linker step in the future.
Contributes towards #17693.