From 2b5392a47a7623d0d88c3f99292d14d7161186c9 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Mon, 8 Jul 2024 15:18:39 -0500 Subject: [PATCH] [Xamarin.Android.Build.Tasks] fix NRE in XAJavaTypeScanner (#9087) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Context: https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=9842772&view=logs&j=9a1c0345-5071-55ee-c0f3-a2911782c698&t=1e33272b-48f7-5132-3788-3179300312b4 We saw a `NullReferenceException` in `XAJavaTypeScanner` in the build linked above: Microsoft.Android.Sdk.Darwin/34.0.95/tools/Xamarin.Android.Common.targets(1536,3): error XAGJS7001: System.NullReferenceException: Object reference not set to an instance of an object. Microsoft.Android.Sdk.Darwin/34.0.95/tools/Xamarin.Android.Common.targets(1536,3): error XAGJS7001: at Xamarin.Android.Tasks.XAJavaTypeScanner.GetJavaTypes(ICollection`1 inputAssemblies, XAAssemblyResolver resolver) Microsoft.Android.Sdk.Darwin/34.0.95/tools/Xamarin.Android.Common.targets(1536,3): error XAGJS7001: at Xamarin.Android.Tasks.GenerateJavaStubs.Run(XAAssemblyResolver res, Boolean useMarshalMethods) Microsoft.Android.Sdk.Darwin/34.0.95/tools/Xamarin.Android.Common.targets(1536,3): error XAGJS7001: at Xamarin.Android.Tasks.GenerateJavaStubs.RunTask() Microsoft.Android.Sdk.Darwin/34.0.95/tools/Xamarin.Android.Common.targets(1536,3): error XAGJS7001: at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25 I added `#nullable enable` to `XAJavaTypeScanner.cs`, and fixed the one warning that existed around `resolver.Load()`. Now, when `resolver.Load()` returns `null`, we'll log the Debug message: [{targetArch}] Unable to load assembly '…' --- .../Utilities/XAJavaTypeScanner.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/XAJavaTypeScanner.cs b/src/Xamarin.Android.Build.Tasks/Utilities/XAJavaTypeScanner.cs index 59cd57ebb53..1980427ef0d 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/XAJavaTypeScanner.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/XAJavaTypeScanner.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Collections.Generic; using System.IO; @@ -48,7 +49,11 @@ public List GetJavaTypes (ICollection inputAssemblies throw new InvalidOperationException ($"Internal error: assembly '{asmItem.ItemSpec}' should be in the '{targetArch}' architecture, but is in '{arch}' instead."); } - AssemblyDefinition asmdef = resolver.Load (asmItem.ItemSpec); + AssemblyDefinition? asmdef = resolver.Load (asmItem.ItemSpec); + if (asmdef == null) { + log.LogDebugMessage ($"[{targetArch}] Unable to load assembly '{asmItem.ItemSpec}'"); + continue; + } foreach (ModuleDefinition md in asmdef.Modules) { foreach (TypeDefinition td in md.Types) {