[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when applying the [Preserve] attribute.#25001
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors how [Preserve] is handled in the dotnet linker pipeline by generating [DynamicDependency] attributes (so preservation intent is expressed in IL rather than via manual marking), aligning with the longer-term goal of reducing/removing custom linker steps (Issue #17693).
Changes:
- Replaces the older
ApplyPreserveAttributeimplementation with a shared implementation (ApplyPreserveAttributeImpl) and introduces a newApplyPreserveAttributeStepthat emits[DynamicDependency]attributes before marking. - Refactors
AssemblyModifierStepto support assembly-level modification hooks (ModifyAssembly) and ensures assemblies are only saved when modified. - Wires the new step into the build via MSBuild properties/step registration, and adds a link-context flag to prevent running both the old marking-based logic and the new dynamic-dependency logic.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/linker/ApplyPreserveAttribute.cs | Removes the legacy linker-step implementation. |
| tools/dotnet-linker/dotnet-linker.csproj | Drops compilation/linking of the removed legacy file. |
| tools/dotnet-linker/Steps/AssemblyModifierStep.cs | Introduces ModifyAssembly and seals TryProcessAssembly to standardize assembly-save behavior. |
| tools/dotnet-linker/ApplyPreserveAttributeStep.cs | New step that translates [Preserve] into [DynamicDependency] attributes pre-mark. |
| tools/dotnet-linker/ApplyPreserveAttributeBase.cs | Refactors into ApplyPreserveAttribute substep + shared ApplyPreserveAttributeImpl used by both approaches. |
| tools/dotnet-linker/AppBundleRewriter.cs | Exposes helpers used by the new step (GetOrCreateStaticConstructor, AddAttributeOnlyOnce). |
| tools/common/DerivedLinkContext.cs | Adds a flag to coordinate whether the new step ran. |
| dotnet/targets/Xamarin.Shared.Sdk.targets | Adds MSBuild property + registers ApplyPreserveAttributeStep before MarkStep. |
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.
d09d3b4 to
81457b4
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.
b6ae4b2 to
a2d1483
Compare
4036efb to
9b7d008
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.
a2d1483 to
723f6c9
Compare
cade00a to
d8b772e
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.
…marking when applying the [Preserve] attribute. This makes it easier to move this code out of a custom linker step in the future. This was simplified a bit because we already had code to do this, but it was only in effect when using NativeAOT. Generalize it, move it out of the marking phase of the linker, and run it before marking. Also move the removal of any [Preserve] attributes to the RemoveAttributes step. Contributes towards #17693.
d8b772e to
d7fde5c
Compare
✅ [CI Build #d7fde5c] Build passed (Build packages) ✅Pipeline on Agent |
✅ [PR Build #d7fde5c] 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 #d7fde5c] Build passed (Build macOS tests) ✅Pipeline on Agent |
🔥 [CI Build #d7fde5c] Test results 🔥Test results❌ Tests failed on VSTS: test results 4 tests crashed, 1 tests failed, 139 tests passed. Failures❌ dotnettests tests (iOS)🔥 Failed catastrophically on VSTS: test results - dotnettests_ios (no summary found). Html Report (VSDrops) Download ❌ dotnettests tests (tvOS)1 tests failed, 0 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (macOS)🔥 Failed catastrophically on VSTS: test results - monotouch_macos (no summary found). Html Report (VSDrops) Download ❌ msbuild tests🔥 Failed catastrophically on VSTS: test results - msbuild (no summary found). Html Report (VSDrops) Download ❌ sharpie tests🔥 Failed catastrophically on VSTS: test results - sharpie (no summary found). 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.
This was simplified a bit because we already had code to do this, but it was only
in effect when using NativeAOT. Generalize it, move it out of the marking phase of
the linker, and run it before marking.
Also move the removal of any [Preserve] attributes to the RemoveAttributes step.
Contributes towards #17693.