Skip to content

Move CheckForObsoletePreserveAttribute into PostTrimmingPipeline#11009

Open
Copilot wants to merge 2 commits intomainfrom
copilot/move-check-for-obsolete-preserve-attribute
Open

Move CheckForObsoletePreserveAttribute into PostTrimmingPipeline#11009
Copilot wants to merge 2 commits intomainfrom
copilot/move-check-for-obsolete-preserve-attribute

Conversation

Copy link
Contributor

Copilot AI commented Mar 24, 2026

CheckForObsoletePreserveAttribute runs as a standalone MSBuild task on every incremental build, costing ~120ms to open and scan every user assembly with PEReader. This is unnecessary work since PostTrimmingPipeline already loads all assemblies via Mono.Cecil.

Changes

  • New CheckForObsoletePreserveAttributeStep — implements IAssemblyModifierPipelineStep, checks for Android.Runtime.PreserveAttribute type references using the already-loaded AssemblyDefinition (zero additional I/O)
  • Added step to PostTrimmingPipeline — runs before StripEmbeddedLibrariesStep, emits the same IL6001 warning
  • Removed standalone infrastructure — deleted CheckForObsoletePreserveAttribute.cs task, its UsingTask declaration, and the _CheckForObsoletePreserveAttribute target from Microsoft.Android.Sdk.AssemblyResolution.targets

Previously, CheckForObsoletePreserveAttribute ran as a separate MSBuild task
on every incremental build (AfterTargets="_PrepareAssemblies"), taking ~120ms
to open and read every user assembly with PEReader.

Now the check runs as a step in PostTrimmingPipeline, where assemblies are
already loaded by Mono.Cecil. This eliminates the overhead of separately
opening each assembly file, and the check only runs when trimming is enabled.

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/android/sessions/34cf49e8-922e-4ad8-b7ea-c598fcf71b63
}

var steps = new List<IAssemblyModifierPipelineStep> ();
steps.Add (new CheckForObsoletePreserveAttributeStep (Log));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot there is a build error:

       (CoreCompile target) -> 
         /Users/builder/azdo/_work/2/s/android/src/Xamarin.Android.Build.Tasks/Tasks/PostTrimmingPipeline.cs(50,18): error CS0246: The type or namespace name 'CheckForObsoletePreserveAttributeStep' could not be found (are you missing a using directive or an assembly reference?) [/Users/builder/azdo/_work/2/s/android/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj]

    177 Warning(s)
    1 Error(s)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3add14d. Two issues: (1) the Linker\** directory is excluded from compilation in the csproj, so the new file needed an explicit <Compile Include> entry, and (2) LogCodedWarning is an extension method from Microsoft.Android.Build.Tasks namespace which needed a using directive.

…d missing using

The Linker\** directory is excluded from compilation by default, so files
must be explicitly listed. Also add the missing Microsoft.Android.Build.Tasks
using directive for the LogCodedWarning extension method.

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/android/sessions/0d36b884-8972-4ef3-b842-8834afd6a7af
Copilot AI requested a review from jonathanpeppers March 24, 2026 21:08
@simonrozsival simonrozsival marked this pull request as ready for review March 26, 2026 16:43
@simonrozsival simonrozsival self-requested a review as a code owner March 26, 2026 16:43
Copilot AI review requested due to automatic review settings March 26, 2026 16:43
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 aims to remove the incremental-build cost of scanning assemblies for Android.Runtime.PreserveAttribute references by moving the check into the existing post-trimming pipeline, reusing already-loaded Mono.Cecil assemblies.

Changes:

  • Added a new CheckForObsoletePreserveAttributeStep implementing IAssemblyModifierPipelineStep to emit IL6001 without extra I/O.
  • Registered the new step in PostTrimmingPipeline so it runs alongside other post-trim assembly modifications.
  • Removed the standalone CheckForObsoletePreserveAttribute MSBuild task and its _CheckForObsoletePreserveAttribute target wiring.

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
src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj Includes the new linker step source file in the explicit Linker compile list.
src/Xamarin.Android.Build.Tasks/Tasks/PostTrimmingPipeline.cs Adds the new warning step into the post-trimming step list.
src/Xamarin.Android.Build.Tasks/Tasks/CheckForObsoletePreserveAttribute.cs Removes the standalone PEReader-based task implementation.
src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets Removes the UsingTask and target that ran the warning task after _PrepareAssemblies.
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/CheckForObsoletePreserveAttributeStep.cs New Cecil-based pipeline step that detects PreserveAttribute type references and logs IL6001.

Comment on lines 12 to 15

<UsingTask TaskName="Xamarin.Android.Tasks.CheckForObsoletePreserveAttribute" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.ProcessAssemblies" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.ProcessNativeLibraries" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.ProcessRuntimePackLibraryDirectories" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the standalone _CheckForObsoletePreserveAttribute target drops the IL6001 warning from the normal _PrepareAssemblies path, and the replacement implementation currently only runs via PostTrimmingPipeline (which is conditioned on $(PublishTrimmed)). This likely changes behavior for standard Release linked builds (AndroidLinkMode/TrimMode) that don’t set PublishTrimmed—LinkerTests.WarnWithReferenceToPreserveAttribute currently covers this and will probably fail. Please ensure the warning is still emitted for the configurations where ILLink/linking runs today, or update the targets/conditions accordingly.

Copilot uses AI. Check for mistakes.
}

var steps = new List<IAssemblyModifierPipelineStep> ();
steps.Add (new CheckForObsoletePreserveAttributeStep (Log));
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the warning check as a PostTrimmingPipeline step means it will only run when the PostTrimmingPipeline MSBuild target runs (currently gated on $(PublishTrimmed) in Microsoft.Android.Sdk.TypeMap.LlvmIr.targets). The existing build/test scenario LinkerTests.WarnWithReferenceToPreserveAttribute expects the IL6001 warning in a normal Release linked build without setting PublishTrimmed, so this change will likely stop emitting the warning (and break that test) for non-PublishTrimmed builds. Consider moving this check into the ILLink pipeline used for AndroidLinkMode/TrimMode release linking, or otherwise ensuring the step runs whenever linking/trimming is performed, not only for PublishTrimmed publishes.

Suggested change
steps.Add (new CheckForObsoletePreserveAttributeStep (Log));

Copilot uses AI. Check for mistakes.
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