Skip to content

Enable #nullable enable in SolutionBuilder.cs#11458

Merged
jonathanpeppers merged 2 commits into
mainfrom
copilot/fix-solution-builder-nullable
May 22, 2026
Merged

Enable #nullable enable in SolutionBuilder.cs#11458
jonathanpeppers merged 2 commits into
mainfrom
copilot/fix-solution-builder-nullable

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 22, 2026

SolutionBuilder.cs lacks nullable annotations, hiding potential null-reference issues at compile time.

  • Add #nullable enable as the first line
  • SolutionPathstring? (never set in constructor, always via object initializer)
  • SolutionName → default = "" (constructor assigns it, default satisfies the compiler)
  • Add ArgumentNullException.ThrowIfNull(SolutionPath) in Save() and BuildProject()
  • Guard Directory.Delete in Dispose with string.IsNullOrEmpty(SolutionPath)
  • No ! (null-forgiving operator) used

Note: used string.IsNullOrEmpty() rather than the .IsNullOrEmpty() extension method because Xamarin.ProjectTools doesn't reference the NullableExtensions class — consistent with all other usages in this project.

Agent-Logs-Url: https://github.com/dotnet/android/sessions/cab59e12-2042-496b-a394-a730244a44f2

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot AI changed the title [WIP] Enable nullable enable in SolutionBuilder.cs Enable #nullable enable in SolutionBuilder.cs May 22, 2026
Copilot AI requested a review from jonathanpeppers May 22, 2026 17:06
@jonathanpeppers jonathanpeppers marked this pull request as ready for review May 22, 2026 19:35
Copilot AI review requested due to automatic review settings May 22, 2026 19:35
@jonathanpeppers jonathanpeppers merged commit ded820a into main May 22, 2026
4 of 5 checks passed
@jonathanpeppers jonathanpeppers deleted the copilot/fix-solution-builder-nullable branch May 22, 2026 19:35
Copy link
Copy Markdown
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

Enables nullable reference type analysis in the SolutionBuilder test helper to surface potential null-reference issues at compile time, and adds runtime guards where SolutionPath is required.

Changes:

  • Added #nullable enable to opt the file into full nullable analysis.
  • Annotated SolutionPath as nullable and provided a non-null default for SolutionName.
  • Added null guards for SolutionPath in Save()/BuildProject() and tightened cleanup conditions in Dispose().
Comments suppressed due to low confidence (3)

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/SolutionBuilder.cs:27

  • ArgumentNullException.ThrowIfNull (SolutionPath) doesn’t make subsequent uses of the property non-nullable for the compiler (and the property could change between reads). Consider capturing to a local (var solutionPath = SolutionPath; ThrowIfNull (solutionPath);) and using the local for all Path.Combine/WriteAllText calls in this method to avoid nullable warnings and double-evaluation.
		public void Save ()
		{
			ArgumentNullException.ThrowIfNull (SolutionPath);
			foreach (var p in Projects) {
				using (var pb = new ProjectBuilder (Path.Combine (SolutionPath, p.ProjectName))) {

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/SolutionBuilder.cs:72

  • Same pattern here: after ThrowIfNull (SolutionPath), subsequent references to the SolutionPath property are still string? from the compiler’s perspective. Capture to a local once, validate it, and use the local for Path.Combine/BuildInternal to avoid nullable warnings and ensure a consistent value is used.
		public bool BuildProject(XamarinProject project, string target = "Build")
		{
			ArgumentNullException.ThrowIfNull (SolutionPath);
			BuildSucceeded = BuildInternal(Path.Combine (SolutionPath, project.ProjectName, project.ProjectFilePath), target, restore: project.ShouldRestorePackageReferences);
			return BuildSucceeded;

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/SolutionBuilder.cs:102

  • !string.IsNullOrEmpty (SolutionPath) followed by Directory.Delete (SolutionPath, ...) re-reads the nullable property; this can still produce nullable warnings and allows the value to change between the check and the delete. Capture SolutionPath to a local variable, check the local, and pass the local into Directory.Delete.
		protected override void Dispose (bool disposing)
		{
			if (disposing)
				if (BuildSucceeded && !string.IsNullOrEmpty (SolutionPath))
					try {
						Directory.Delete (SolutionPath, recursive: true);
					} catch (Exception) {

Comment on lines 12 to 15
public IList<XamarinProject> Projects { get; }
public string SolutionPath { get; set; }
public string SolutionName { get; set; }
public string? SolutionPath { get; set; }
public string SolutionName { get; set; } = "";
public bool BuildSucceeded { get; set; }
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.

[fix-finder] Enable #nullable enable in SolutionBuilder.cs

3 participants