Skip to content

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when applying the [Preserve] attribute.#25001

Open
rolfbjarne wants to merge 1 commit intomainfrom
dev/rolf/use-dynamic-dependency-attributes-applypreserveattribute
Open

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when applying the [Preserve] attribute.#25001
rolfbjarne wants to merge 1 commit intomainfrom
dev/rolf/use-dynamic-dependency-attributes-applypreserveattribute

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

@rolfbjarne rolfbjarne commented Mar 24, 2026

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.

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 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 ApplyPreserveAttribute implementation with a shared implementation (ApplyPreserveAttributeImpl) and introduces a new ApplyPreserveAttributeStep that emits [DynamicDependency] attributes before marking.
  • Refactors AssemblyModifierStep to 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.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-applypreserveattribute branch from d09d3b4 to 81457b4 Compare March 24, 2026 18:56
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-optimizegeneratedcode branch from b6ae4b2 to a2d1483 Compare March 30, 2026 07:25
@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-applypreserveattribute branch from 4036efb to 9b7d008 Compare March 30, 2026 07:30
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-optimizegeneratedcode branch from a2d1483 to 723f6c9 Compare March 31, 2026 07:37
@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-applypreserveattribute branch from cade00a to d8b772e Compare March 31, 2026 10:55
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

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.
@rolfbjarne rolfbjarne marked this pull request as ready for review April 1, 2026 10:02
@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-applypreserveattribute branch from d8b772e to d7fde5c Compare April 1, 2026 10:02
@rolfbjarne rolfbjarne requested a review from dalexsoto as a code owner April 1, 2026 10:02
@rolfbjarne rolfbjarne changed the base branch from dev/rolf/use-dynamic-dependency-attributes-optimizegeneratedcode to main April 1, 2026 10:02
@rolfbjarne rolfbjarne requested a review from Copilot April 1, 2026 10:02
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

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #d7fde5c] Build passed (Build packages) ✅

Pipeline on Agent
Hash: d7fde5cb5006ae8399dbac4170e47a4d15cd01a4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #d7fde5c] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: d7fde5cb5006ae8399dbac4170e47a4d15cd01a4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: d7fde5cb5006ae8399dbac4170e47a4d15cd01a4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #d7fde5c] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: d7fde5cb5006ae8399dbac4170e47a4d15cd01a4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🔥 [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

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.DotNetProjectTest.LinkedWithNativeLibraries(TVOS,"...: Frameworks
      Expected: equivalent to < "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", "/System/Library/...

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
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 11 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: d7fde5cb5006ae8399dbac4170e47a4d15cd01a4 [PR build]

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants