Skip to content

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving block code.#24936

Draft
rolfbjarne wants to merge 3 commits intodev/rolf/use-dynamic-dependency-attributes-preservesmartenumconversionfrom
dev/rolf/use-dynamic-dependency-attributes-preserveblockcode
Draft

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving block code.#24936
rolfbjarne wants to merge 3 commits intodev/rolf/use-dynamic-dependency-attributes-preservesmartenumconversionfrom
dev/rolf/use-dynamic-dependency-attributes-preserveblockcode

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 14:17
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 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 PreserveBlockCodeStep that injects [DynamicDependency] attributes to preserve the Handler field and Invoke method for block trampolines.
  • Refactors assembly-rewriting steps by adding an AssemblyModifierStep base and an IsActiveFor hook to ConfigurationAwareStep.
  • 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

  • AssemblyModifierStep already recurses into nested types, but ProcessType still 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 in ProcessType and 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

  • AssemblyModifierStep already recursively walks nested types via ProcessTypeImpl, but this override also manually recurses into type.NestedTypes. That will process nested types twice (extra Resolve()/attribute work). Remove the manual recursion here and let AssemblyModifierStep handle 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.

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

@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-preservesmartenumconversion branch from 2d2685d to d4e05fb Compare March 23, 2026 18:39
…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.
@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-preserveblockcode branch from efee7bd to 68519d1 Compare March 23, 2026 18:51
@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.

…tenumconversion' into dev/rolf/use-dynamic-dependency-attributes-preserveblockcode
@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

Pipeline on Agent
Hash: 695e793a451bc087f785e6279043a754363d45a4 [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: 695e793a451bc087f785e6279043a754363d45a4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #695e793] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 695e793a451bc087f785e6279043a754363d45a4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #695e793] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 695e793a451bc087f785e6279043a754363d45a4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

  • 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: 3577 Inconclusive: 11 Failed: 1 Ignored: 161)

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: 3567 Inconclusive: 4 Failed: 1 Ignored: 103)
  • 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: 3566 Inconclusive: 4 Failed: 1 Ignored: 101)
  • monotouch-test/macOS/Debug (static registrar): Failed (Test run failed.
    Tests run: 3668 Passed: 3566 Inconclusive: 4 Failed: 1 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 Sonoma (14) tests

🔥 Failed catastrophically on VSTS: test results - mac_sonoma (no summary found).

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
✅ linker: All 44 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 Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 695e793a451bc087f785e6279043a754363d45a4 [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.

3 participants