Add HasMetadata checks to all PEReader code paths to handle native Windows DLLs#10938
Add HasMetadata checks to all PEReader code paths to handle native Windows DLLs#10938
Conversation
…dows DLLs Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR hardens MonoAndroidHelper.HasMonoAndroidReference(ITaskItem) to safely handle native (non-.NET) PE DLLs that can appear in publish item lists (e.g., from NuGet packages), preventing exceptions when probing for CLI metadata.
Changes:
- Added a
PEReader.HasMetadataguard to early-returnfalsebefore callingGetMetadataReader()on non-managed binaries.
…in MSBuild tasks Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
…reader on failure Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
jonathanpeppers
left a comment
There was a problem hiding this comment.
@copilot ## 🤖 AI Review Summary
Verdict:
Found 2 issues: 1 warning, 1 suggestion
⚠️ Testing: No regression test for this bug fix. A test that includes a native (non-.NET) PE DLL in the build inputs would verify these guards work and prevent regressions.- 💡 Documentation: Inline comment in
HasMonoAndroidReferenceis slightly narrow —HasMetadatacan be false for reasons beyond native Windows DLLs (MonoAndroidHelper.cs:357)
👍 Positives:
- Comprehensive coverage — all
PEReader.GetMetadataReader()call sites are guarded (independently verified). The two pre-existing guards (CheckForObsoletePreserveAttribute,GenerateProguardConfiguration) and the deliberately-skippedGenerateNativeApplicationConfigSources(only processesMono.Android.dll) are correctly accounted for. MetadataResolver.csproperly disposes thePEReaderbefore throwing when metadata is missing — good resource management.- Consistent debug logging pattern (
"Skipping non-.NET assembly: {path}") across all task-context call sites. - PR description is thorough and accurate.
⏳ CI: Linux build passed ✅. Mac and Windows builds are still pending.
Review generated by android-reviewer from review guidelines.
Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Added a Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
/azp run Xamarin.Android-PR |
|
No pipelines are associated with this pull request. |
Summary
Extracted from PR #10862: Add
PEReader.HasMetadatachecks before callingGetMetadataReader()across all code paths that usePEReaderto prevent exceptions when processing native (non-.NET) PE DLLs.Problem
When NuGet packages (like MSTest) include native Windows
.dllfiles that flow intoResolvedFileToPublish, callingPEReader.GetMetadataReader()on these files throws an exception because they are not .NET assemblies and have no CLI metadata.Fix
Added
PEReader.HasMetadataguards beforeGetMetadataReader()calls across all relevant code paths. In MSBuild tasks with log access, aLogDebugMessage()call is included when skipping non-.NET assemblies.Changes
src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs: AddedHasMetadataguard inHasMonoAndroidReference(ITaskItem)andIsReferenceAssembly(with debug logging)src/Xamarin.Android.Build.Tasks/Tasks/FilterAssemblies.cs: AddedHasMetadataguard withLogDebugMessageinProcessAssemblysrc/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs: AddedHasMetadataguard withLogDebugMessageinExtractsrc/Xamarin.Android.Build.Tasks/Tasks/CheckForInvalidDesignerConfig.cs: AddedHasMetadataguard inHasResourceDesignerAssemblyReferencesrc/Xamarin.Android.Build.Tasks/Utilities/ResourceDesignerImportGenerator.cs: AddedHasMetadataguard withLogDebugMessageinCreateImportMethodssrc/Xamarin.Android.Build.Tasks/Utilities/MetadataResolver.cs: AddedHasMetadataguard at reader creation time with proper disposal on failuresrc/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/FilterAssembliesTests.cs: AddedNativeDll_Skippedregression test that creates a minimal PE file without CLI metadata and verifiesFilterAssembliesgracefully skips itAlready protected (no changes needed):
GenerateProguardConfiguration,CheckForObsoletePreserveAttribute. Skipped:GenerateNativeApplicationConfigSources.GetRequiredTokens(only processesMono.Android.dll).✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.