Skip to content

[fix-finder] Enable nullable reference types in GenerateNativeAotEnvironmentAssemblerSources.cs #11545

@github-actions

Description

@github-actions

Problem

GenerateNativeAotEnvironmentAssemblerSources.cs lacks #nullable enable and has potential null-dereference bugs: outputFile is declared as ITaskItem? but dereferenced without a null check on line 32, and the ! null-forgiving operator is used on lines 40 and 42 (which is prohibited by repo conventions).

Location

  • File: src/Xamarin.Android.Build.Tasks/Tasks/GenerateNativeAotEnvironmentAssemblerSources.cs
  • Lines: 31–42

Current Code

ITaskItem? outputFile = GenerateNativeAotLibraryLoadAssemblerSources.FindOutputFile (OutputSources, abi: abi, rid: RID);
Log.LogDebugMessage ($"Environment variables file to generate: {outputFile.ItemSpec}");

string environmentLlFilePath = outputFile.ItemSpec;
var generator = new NativeAotEnvironmentNativeAssemblyGenerator (Log, envBuilder);
LLVMIR.LlvmIrModule environmentModule = generator.Construct ();
using var environmentWriter = MemoryStreamPool.Shared.CreateStreamWriter ();
bool fileFullyWritten = false;
try {
	generator.Generate (environmentModule, targetArch, environmentWriter, environmentLlFilePath!);
	environmentWriter.Flush ();
	Files.CopyIfStreamChanged (environmentWriter.BaseStream, environmentLlFilePath!);

Suggested Fix

  1. Add #nullable enable at the very top of the file (no preceding blank lines).

  2. Add a null check after FindOutputFile returns, and throw if null — this replaces the implicit dereference on line 32:

ITaskItem? outputFile = GenerateNativeAotLibraryLoadAssemblerSources.FindOutputFile (OutputSources, abi: abi, rid: RID);
if (outputFile == null) {
	Log.LogCodedError ("XA0000", Properties.Resources.XA0000, $"Could not find output file for ABI '{abi}' and RID '{RID}'");
	return false;
}
Log.LogDebugMessage ($"Environment variables file to generate: {outputFile.ItemSpec}");

Alternatively, if no specific error code exists for this scenario, a simpler approach is fine:

if (outputFile == null) {
	Log.LogError ($"Could not find output file for ABI '{abi}' and RID '{RID}'");
	return false;
}
  1. Remove the ! null-forgiving operators on lines 40 and 42. After the null check above, environmentLlFilePath is guaranteed non-null, so the ! is unnecessary:
generator.Generate (environmentModule, targetArch, environmentWriter, environmentLlFilePath);
environmentWriter.Flush ();
Files.CopyIfStreamChanged (environmentWriter.BaseStream, environmentLlFilePath);
  1. TFM note: This project targets netstandard2.0 — do NOT use ArgumentNullException.ThrowIfNull. Use explicit if + throw or Log.LogError + return false patterns instead.

Guidelines

  • Follow repo Mono formatting style (tabs, space before parentheses)
  • Never use ! (null-forgiving operator)
  • For netstandard2.0 projects, use explicit null checks, not ArgumentNullException.ThrowIfNull
  • [Required] properties should be non-nullable with defaults (already correct in this file)
  • Non-required properties should be nullable (already correct for Environments)

Acceptance Criteria

  • #nullable enable added at the top of the file
  • Null check added for outputFile with appropriate error logging
  • ! null-forgiving operators removed from lines 40 and 42
  • No new warnings introduced
  • All tests pass

Fix-finder metadata

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

Generated by Nightly Fix Finder · ● 7.3M ·

  • expires on Jun 7, 2026, 2:29 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