Skip to content

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving smart enum methods.#24934

Open
rolfbjarne wants to merge 6 commits intomainfrom
dev/rolf/use-dynamic-dependency-attributes-preservesmartenumconversion
Open

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving smart enum methods.#24934
rolfbjarne wants to merge 6 commits intomainfrom
dev/rolf/use-dynamic-dependency-attributes-preservesmartenumconversion

Conversation

@rolfbjarne
Copy link
Member

This makes it easier to move this code out of a custom linker step in the future.

Contributes towards #17693.

@rolfbjarne rolfbjarne requested a review from Copilot March 19, 2026 13:32
Copy link
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 updates the dotnet-linker integration to preserve smart-enum conversion methods by injecting [DynamicDependency] attributes instead of manually marking methods in a custom mark handler, supporting the longer-term goal of removing custom linker steps (issue #17693).

Changes:

  • Added a new PreserveSmartEnumConversionsStep that scans assemblies for BindAsAttribute usage and adds DynamicDependency attributes to keep GetConstant/GetValue.
  • Refactored the existing PreserveSmartEnumConversionsHandler to reuse shared preservation logic.
  • Extended AppBundleRewriter with a helper to add DynamicDependency attributes, and wired the new step into Xamarin.Shared.Sdk.targets behind a property toggle.

Reviewed changes

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

File Description
tools/linker/MonoTouch.Tuner/PreserveSmartEnumConversions.cs Refactors mark handler to delegate smart-enum detection/preservation to shared helper logic.
tools/dotnet-linker/PreserveSmartEnumConversionsStep.cs Introduces a new pre-mark linker step that injects DynamicDependency attributes for smart-enum conversions.
tools/dotnet-linker/AppBundleRewriter.cs Adds a DynamicDependencyAttribute ctor reference and a convenience method to attach the attribute based on type/module location.
dotnet/targets/Xamarin.Shared.Sdk.targets Adds a property toggle and registers the new step conditionally, keeping the old handler as fallback.

You can also share your feedback on Copilot code review. Take the survey.

@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.

@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 preserving smart enum methods. This makes it easier to move this code out of a custom linker step in the future. Contributes towards #17693.
@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-preservesmartenumconversion branch from 2d2685d to d4e05fb Compare March 23, 2026 18:39
@rolfbjarne rolfbjarne changed the base branch from dev/rolf/use-dynamic-dependency-attributes-markiprotocolhandler to main March 23, 2026 18:40
@rolfbjarne rolfbjarne marked this pull request as ready for review March 23, 2026 18:41
@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.

Copy link
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 updates the dotnet-linker pipeline to preserve smart enum conversion methods by injecting DynamicDependency attributes (when enabled) instead of relying solely on manual marking in a custom mark handler, helping reduce reliance on custom linker steps (issue #17693).

Changes:

  • Introduces a new PreserveSmartEnumConversionsStep (an AssemblyModifierStep) that adds DynamicDependency attributes to root smart enum conversion methods.
  • Refactors the existing PreserveSmartEnumConversionsHandler to reuse shared preservation logic.
  • Extends AppBundleRewriter with helpers to add DynamicDependency attributes (including a guard to only add dependencies targeting trimmed assemblies) and wires the new step into Xamarin.Shared.Sdk.targets.

Reviewed changes

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

Show a summary per file
File Description
tools/linker/MonoTouch.Tuner/PreserveSmartEnumConversions.cs Refactors the mark handler to delegate logic to a shared preserver helper.
tools/dotnet-linker/PreserveSmartEnumConversionsStep.cs Adds a new assembly-modifier step that preserves smart enum conversions via DynamicDependency attributes.
tools/dotnet-linker/PreserveProtocolsStep.cs Adds clarifying comment about only processing linked assemblies.
tools/dotnet-linker/AppBundleRewriter.cs Adds DynamicDependency ctor lookup + helper to apply dependency attributes (and trims-only guard).
dotnet/targets/Xamarin.Shared.Sdk.targets Adds MSBuild switches and wires in the new step, keeping the legacy handler as fallback.
@rolfbjarne rolfbjarne enabled auto-merge (squash) March 23, 2026 19:39
@vs-mobiletools-engineering-service2

This comment has been minimized.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@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.

@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
Copy link
Collaborator

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

Pipeline on Agent
Hash: a92fbe4b82814678d90847303735fc1df41f1863 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

Pipeline on Agent
Hash: a92fbe4b82814678d90847303735fc1df41f1863 [PR build]

@vs-mobiletools-engineering-service2
Copy link
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: a92fbe4b82814678d90847303735fc1df41f1863 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

Pipeline on Agent
Hash: a92fbe4b82814678d90847303735fc1df41f1863 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [CI Build #a92fbe4] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 32 tests failed, 124 tests passed.

Failures

❌ linker tests

1 tests failed, 43 tests passed.

Failed tests

  • link all/iOS - simulator/Debug (don't bundle original resources): LaunchFailure

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

9 tests failed, 2 tests passed.

Failed tests

  • monotouch-test/iOS - simulator/Debug: Failed
  • monotouch-test/iOS - simulator/Debug (LinkSdk): Failed
  • monotouch-test/iOS - simulator/Debug (static registrar): Failed
  • monotouch-test/iOS - simulator/Release (all optimizations): Failed
  • monotouch-test/iOS - simulator/Debug (ARM64): Failed
  • monotouch-test/iOS - simulator/Debug (managed static registrar): Failed
  • monotouch-test/iOS - simulator/Release (managed static registrar, all optimizations): Failed
  • monotouch-test/iOS - simulator/Debug (interpreter): Failed
  • monotouch-test/iOS - simulator/Release (interpreter): Failed

Html Report (VSDrops) Download

❌ monotouch tests (MacCatalyst)

12 tests failed, 3 tests passed.

Failed tests

  • monotouch-test/Mac Catalyst/Debug: Failed (Test run failed.
    Tests run: 3742 Passed: 3587 Inconclusive: 10 Failed: 1 Ignored: 154)
  • monotouch-test/Mac Catalyst/Debug (ARM64): Failed (Test run failed.
    Tests run: 3742 Passed: 3587 Inconclusive: 10 Failed: 1 Ignored: 154)
  • monotouch-test/Mac Catalyst/Debug (managed static registrar): Failed (Test run failed.
    Tests run: 3739 Passed: 3586 Inconclusive: 10 Failed: 1 Ignored: 152)
  • monotouch-test/Mac Catalyst/Debug (static registrar): Failed (Test run failed.
    Tests run: 3739 Passed: 3586 Inconclusive: 10 Failed: 1 Ignored: 152)
  • monotouch-test/Mac Catalyst/Debug (static registrar, ARM64): Failed (Test run failed.
    Tests run: 3739 Passed: 3586 Inconclusive: 10 Failed: 1 Ignored: 152)
  • monotouch-test/Mac Catalyst/Release (managed static registrar): Failed (Test run failed.
    Tests run: 3739 Passed: 3580 Inconclusive: 10 Failed: 1 Ignored: 158)
  • monotouch-test/Mac Catalyst/Release (managed static registrar, all optimizations): Failed (Test run failed.
    Tests run: 3739 Passed: 3574 Inconclusive: 10 Failed: 1 Ignored: 164)
  • monotouch-test/Mac Catalyst/Release (static registrar): Failed (Test run failed.
    Tests run: 3739 Passed: 3580 Inconclusive: 10 Failed: 1 Ignored: 158)
  • monotouch-test/Mac Catalyst/Release (static registrar, all optimizations): Failed (Test run failed.
    Tests run: 3739 Passed: 3574 Inconclusive: 10 Failed: 1 Ignored: 164)
  • monotouch-test/Mac Catalyst/Release (ARM64, LLVM): Failed (Test run failed.
    Tests run: 3739 Passed: 3580 Inconclusive: 10 Failed: 1 Ignored: 158)
  • monotouch-test/Mac Catalyst/Debug (interpreter): Failed (Test run failed.
    Tests run: 3742 Passed: 3585 Inconclusive: 10 Failed: 1 Ignored: 156)
  • monotouch-test/Mac Catalyst/Release (interpreter): Failed (Test run failed.
    Tests run: 3739 Passed: 3578 Inconclusive: 10 Failed: 1 Ignored: 160)

Html Report (VSDrops) Download

❌ monotouch tests (macOS)

9 tests failed, 3 tests passed.

Failed tests

  • monotouch-test/macOS/Debug: Failed (Test run failed.
    Tests run: 3671 Passed: 3568 Inconclusive: 4 Failed: 1 Ignored: 102)
  • monotouch-test/macOS/Debug (ARM64): Failed (Test run failed.
    Tests run: 3671 Passed: 3568 Inconclusive: 4 Failed: 1 Ignored: 102)
  • monotouch-test/macOS/Debug (managed static registrar): Failed (Test run failed.
    Tests run: 3668 Passed: 3564 Inconclusive: 4 Failed: 2 Ignored: 102)
  • monotouch-test/macOS/Debug (static registrar): Failed (Test run failed.
    Tests run: 3668 Passed: 3565 Inconclusive: 4 Failed: 2 Ignored: 101)
  • monotouch-test/macOS/Debug (static registrar, ARM64): Failed (Test run failed.
    Tests run: 3668 Passed: 3566 Inconclusive: 4 Failed: 1 Ignored: 101)
  • monotouch-test/macOS/Release (managed static registrar): Failed (Test run failed.
    Tests run: 3668 Passed: 3564 Inconclusive: 4 Failed: 1 Ignored: 103)
  • monotouch-test/macOS/Release (managed static registrar, all optimizations): Failed (Test run failed.
    Tests run: 3668 Passed: 3562 Inconclusive: 4 Failed: 1 Ignored: 105)
  • monotouch-test/macOS/Release (static registrar): Failed (Test run failed.
    Tests run: 3668 Passed: 3564 Inconclusive: 4 Failed: 1 Ignored: 103)
  • monotouch-test/macOS/Release (static registrar, all optimizations): Failed (Test run failed.
    Tests run: 3668 Passed: 3561 Inconclusive: 4 Failed: 1 Ignored: 106)

Html Report (VSDrops) Download

❌ Tests on macOS Tahoe (26) tests

1 tests failed, 4 tests passed.

Failed tests

  • monotouch-test: Failed (exit code 2)
    • GetConstant : System.InvalidCastException : Unable to cast object of type 'System.Int64' to type 'System.Int32'.
    • GetConstant : System.InvalidCastException : Unable to cast object of type 'System.Int64' to type 'System.Int32'.
    • GetConstant : System.InvalidCastException : Specified cast is not valid.
    • ... and 1 more failures

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): 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
✅ dotnettests (tvOS): 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
✅ monotouch (tvOS): All 11 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 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

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: a92fbe4b82814678d90847303735fc1df41f1863 [PR build]

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

Labels

None yet

4 participants