Skip to content

[fix-finder] Enable nullable reference types in WaitForAppDetection.cs #11712

Description

@github-actions

Problem

src/Xamarin.Android.Build.Debugging.Tasks/Tasks/WaitForAppDetection.cs is shipped product code that has not been opted into nullable reference types. It is a small, self-contained AsyncTask whose only field can legitimately be null — it is populated from GetRegisteredTaskObjectAssemblyLocal<T>, which returns null when no task object is registered. The code already guards against null, so opting in to NRT simply makes the existing contract explicit. Its sibling in the same folder, DetectIfAppWasUninstalled.cs, was already converted in #11598, which explicitly left this file for later.

Location

  • File(s): src/Xamarin.Android.Build.Debugging.Tasks/Tasks/WaitForAppDetection.cs
  • Line(s): line 1 (add directive) and line 16 (field declaration)

Current Code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mono.AndroidTools;
using Microsoft.Android.Build.Tasks;

namespace Xamarin.Android.Tasks {
	public class WaitForAppDetection : AsyncTask
	{
		public override string TaskPrefix => "WFAD";

		System.Threading.Tasks.Task<List<AndroidInstalledPackage>> getPackagesAsync;

		public override bool Execute ()
		{
			var key =  ProjectSpecificTaskObjectKey (DetectIfAppWasUninstalled.GetPackagesAsyncKey);
			getPackagesAsync = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<System.Threading.Tasks.Task<List<AndroidInstalledPackage>>> (key, RegisteredTaskObjectLifetime.Build);
			return base.Execute ();
		}

		public async override System.Threading.Tasks.Task RunTaskAsync ()
		{
			LogDebugMessage ("Waiting for DetectIfAppWasUninstalled...");
			if (getPackagesAsync == null)
				return;
			await getPackagesAsync;
			LogDebugMessage ("DetectIfAppWasUninstalled Completed.");
			return;
		}
	}
}

Suggested Fix

The owning project (Xamarin.Android.Build.Debugging.Tasks.csproj) targets netstandard2.0 (LangVersion 12), so do NOT use ArgumentNullException.ThrowIfNull (it requires net6.0+). No null-throwing is needed here at all — only annotations.

  1. Add #nullable enable as the very first line of the file (no preceding blank lines):
#nullable enable
using System;
  1. Make the getPackagesAsync field nullable. It is left unassigned until Execute () runs, and GetRegisteredTaskObjectAssemblyLocal<T> returns null when nothing is registered:
		System.Threading.Tasks.Task<List<AndroidInstalledPackage>>? getPackagesAsync;

No other changes are required: the existing if (getPackagesAsync == null) return; guard in RunTaskAsync () already handles the null case, so there are no remaining nullable warnings.

Guidelines

  • Follow Mono formatting style: tabs, space before ( and [.
  • Never use the ! (null-forgiving) operator — check for null explicitly (already done here).
  • This is a netstandard2.0 project — do NOT use ArgumentNullException.ThrowIfNull.
  • Match the conventions already used in the sibling file DetectIfAppWasUninstalled.cs in the same directory (see [fix-finder] Enable nullable reference types in DetectIfAppWasUninstalled.cs #11598).

Acceptance Criteria

  • #nullable enable is the first line of the file (no preceding blank lines).
  • getPackagesAsync is declared as System.Threading.Tasks.Task<List<AndroidInstalledPackage>>? (nullable).
  • No use of the ! (null-forgiving) operator.
  • No other files are modified.
  • All tests pass.
  • No new warnings introduced.

Fix-finder metadata

  • Script: 01-nullable-reference-types
  • Score: 28/30 (actionability: 9, safety: 9, scope: 10)

Generated by Nightly Fix Finder · 372.6 AIC · ⌖ 73.2 AIC · ⊞ 40.7K ·

  • expires on Jun 30, 2026, 2:32 AM UTC

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions