Skip to content

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

Draft
rolfbjarne wants to merge 2 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 2 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.

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

Comment on lines +73 to +87
// The type is also nested inside ObjCRuntime.Trampolines class)
var nestingType = type.DeclaringType;
if (!nestingType.Is ("ObjCRuntime", "Trampolines"))
return false;

// The class has a readonly field named 'Handler'
field = type.Fields [0];
if (!field.IsInitOnly)
return false;
if (field.Name != "Handler")
return false;

// The class has a parameterless 'Invoke' method with a 'MonoPInvokeCallback' attribute
if (!type.HasMethods)
return false;
Comment on lines +44 to +46

public static bool GetMembersToPreserve (TypeDefinition type, [NotNullWhen (true)] out FieldDefinition? field, [NotNullWhen (true)] out MethodDefinition? method)
{
Comment on lines +34 to +35
protected override string Name { get; } = "Preserve Block Code";
protected override int ErrorCode { get; } = 2240;
@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 #efee7bd] Build failed (Build packages) 🔥

Build failed for the job 'Build packages' (with job status 'Failed')

Pipeline on Agent
Hash: efee7bdcad18768950e9e3d3520f25674d94d661 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [PR Build #efee7bd] Build failed (Detect API changes) 🔥

Build failed for the job 'Detect API changes' (with job status 'Failed')

Pipeline on Agent
Hash: efee7bdcad18768950e9e3d3520f25674d94d661 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: efee7bdcad18768950e9e3d3520f25674d94d661 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

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