Skip to content

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

Merged
rolfbjarne merged 1 commit intomainfrom
dev/rolf/use-dynamic-dependency-attributes-preserveblockcode
Mar 30, 2026
Merged

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving block code.#24936
rolfbjarne merged 1 commit intomainfrom
dev/rolf/use-dynamic-dependency-attributes-preserveblockcode

Conversation

@rolfbjarne
Copy link
Copy Markdown
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
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 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
@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.

@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 marked this pull request as ready for review March 30, 2026 07:08
…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 changed the base branch from dev/rolf/use-dynamic-dependency-attributes-preservesmartenumconversion to main March 30, 2026 07:13
@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-preserveblockcode branch from 695e793 to e6cfae9 Compare March 30, 2026 07:13
@rolfbjarne rolfbjarne enabled auto-merge (squash) March 30, 2026 07:14
@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

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

Pipeline on Agent
Hash: e6cfae9cd85450152b14100bc1f57abdce44b56f [PR build]

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

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

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

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

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

Pipeline on Agent
Hash: e6cfae9cd85450152b14100bc1f57abdce44b56f [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

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

🚀 [CI Build #e6cfae9] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 156 tests passed 🎉

Tests counts

✅ 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. [attempt 2] 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. [attempt 2] 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 (macOS): All 12 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
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: e6cfae9cd85450152b14100bc1f57abdce44b56f [PR build]

@rolfbjarne rolfbjarne merged commit c41bd80 into main Mar 30, 2026
48 checks passed
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