Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Nullable Reference Types in Test Projects #44862

Merged
merged 8 commits into from
Dec 16, 2024

Conversation

v-wuzhai
Copy link
Member

This PR addresses enabling nullable reference types for the following test projects:

  • Microsoft.Win32.Msi.Manual.Tests.csproj
  • HelixTasks.csproj
  • Microsoft.NET.TestFramework.csproj

@v-wuzhai v-wuzhai requested review from a team and tmat as code owners November 14, 2024 05:56
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Infrastructure untriaged Request triage from a team member labels Nov 14, 2024
@@ -22,7 +22,7 @@ public class SDKCustomCreateXUnitWorkItemsWithTestExclusion : Build.Utilities.Ta
/// The two required parameters will be automatically created if XUnitProject.Identity is set to the path of the XUnit csproj file
/// </summary>
[Required]
public ITaskItem[] XUnitProjects { get; set; }
public ITaskItem[]? XUnitProjects { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rainersigwald Have ya'll enabled nullable for any msbuild tasks? Is it possible for these to be null or does MSBuild force them to have a value always? Do they have to be marked Required to not be null?

@@ -66,7 +66,7 @@ private static int ShowSdkInfo()
{
var log = new StringTestLogger();
var command = new DotnetCommand(log, "--info");
var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current.TestExecutionDirectory, "sdkinfo"));
var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "sdkinfo"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if rather than having the ! here we should have the property getter for TestExecutionDirectory throw if it's null (as it really should never be null). That would potentially remove a lot of these !s that you've added by doing it in one place.

@v-wuzhai v-wuzhai force-pushed the dev/v-wuzhai/enable-nullable branch 2 times, most recently from 49f596d to 1676070 Compare November 20, 2024 07:48
@v-wuzhai v-wuzhai requested review from a team, AntonLapounov and vijayrkn as code owners November 20, 2024 07:48
@v-wuzhai v-wuzhai force-pushed the dev/v-wuzhai/enable-nullable branch from 1676070 to 1a475e9 Compare November 20, 2024 07:54
@MiYanni
Copy link
Member

MiYanni commented Dec 6, 2024

@v-wuzhai I do plan on reviewing this PR at some point. However, we're having internal discussions about nullability as part of my PR. Take a look at it if you're interested.

@v-wuzhai v-wuzhai force-pushed the dev/v-wuzhai/enable-nullable branch from 9f34fd2 to 2709f62 Compare December 13, 2024 06:25
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 13, 2024
@v-wuzhai
Copy link
Member Author

@MiYanni @marcpopMSFT would you be able to review these changes? Thanks!

Copy link
Member

@MiYanni MiYanni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks good. Just a few adjustments to be made based on the comments I left. Otherwise, good to go.


if (!Directory.Exists(SourceDirectory))
{
Log.LogError($"SourceDirectory '{SourceDirectory} does not exist.");
Log.LogError($"SourceDirectory '{SourceDirectory}' does not exist.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find.

@@ -33,7 +33,7 @@ public sealed class TarGzFileCreateFromDirectory : ToolTask
/// <summary>
/// An item group of regular expressions for content to exclude from the archive.
/// </summary>
public ITaskItem[] ExcludePatterns { get; set; }
public ITaskItem[]? ExcludePatterns { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: trailing space

@@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable<string> args)
{
List<string> newArgs = new();
newArgs.Add("add");
if (!string.IsNullOrEmpty(_projectName))
if (_projectName is not null && !string.IsNullOrEmpty(_projectName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've updated the build to no longer warn for the .NET Framework compilation for string.IsNullOrEmpty so this added check is no longer necessary. See: #45299

@@ -16,7 +16,7 @@ public DotnetPublishCommand(ITestOutputHelper log, params string[] args) : base(
protected override SdkCommandSpec CreateCommand(IEnumerable<string> args)
{
List<string> newArgs = new(args);
if (!string.IsNullOrEmpty(_runtime))
if (_runtime is not null && !string.IsNullOrEmpty(_runtime))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer necessary.

@@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable<string> args)
{
List<string> newArgs = new();
newArgs.Add("list");
if (!string.IsNullOrEmpty(_projectName))
if (_projectName is not null && !string.IsNullOrEmpty(_projectName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer necessary.

@@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable<string> args)
{
List<string> newArgs = new();
newArgs.Add("remove");
if (!string.IsNullOrEmpty(_projectName))
if (_projectName is not null && !string.IsNullOrEmpty(_projectName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer necessary.

Comment on lines 159 to 166
if (ProjectSdk != null)
if (projectXml.Root.Attribute("Sdk") is not null && ProjectSdk != null)
{
projectXml.Root.Attribute("Sdk").Value = ProjectSdk;
projectXml.Root.Attribute("Sdk")!.Value = ProjectSdk;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to do something like this. Not sure it will work exactly like this since I'm not sure of the types used here. Might need to play around with it.

Suggested change
if (ProjectSdk != null)
if (projectXml.Root.Attribute("Sdk") is not null && ProjectSdk != null)
{
projectXml.Root.Attribute("Sdk").Value = ProjectSdk;
projectXml.Root.Attribute("Sdk")!.Value = ProjectSdk;
}
var sdkAttribute = projectXml.Root.Attribute("Sdk");
if (sdkAttribute is not null && ProjectSdk != null)
{
sdkAttribute.Value = ProjectSdk;
}

Comment on lines 139 to 148
var packageReferencesToUpdate =
project.Root.Descendants(ns + PropertyName)
.Where(p => p.Attribute("Version") != null && p.Attribute("Version")!.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase));
foreach (var packageReference in packageReferencesToUpdate)
{
if(packageReference.Attribute("Version") is not null)
{
packageReference.Attribute("Version")!.Value = targetValue;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the comment above. You might need to set the type on versionAttributesToUpdate to be explicitly not null for it to resolve in the foreach properly. This avoids checking it is not null twice, since you already filter out the nulls in the Where.

Suggested change
var packageReferencesToUpdate =
project.Root.Descendants(ns + PropertyName)
.Where(p => p.Attribute("Version") != null && p.Attribute("Version")!.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase));
foreach (var packageReference in packageReferencesToUpdate)
{
if(packageReference.Attribute("Version") is not null)
{
packageReference.Attribute("Version")!.Value = targetValue;
}
}
var versionAttributesToUpdate =
project.Root.Descendants(ns + PropertyName)
.Select(p => p.Attribute("Version"))
.Where(va => va is not null && va.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase));
foreach (var versionAttribute in versionAttributesToUpdate)
{
versionAttribute.Value = targetValue;
}

@@ -110,7 +110,7 @@ public TestAsset CreateTestProjects(

foreach (var testProject in testProjects)
{
new DotnetCommand(Log, "sln", "add", testProject.Name)
new DotnetCommand(Log, "sln", "add", testProject.Name?? string.Empty)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing space

@@ -185,7 +195,7 @@ public void AddTestEnvironmentVariables(IDictionary<string, string> environment)
environment.Add("DOTNET_ROOT(x86)", DotNetRoot);
}

if (!string.IsNullOrEmpty(CliHomePath))
if (CliHomePath is not null && !string.IsNullOrEmpty(CliHomePath))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer necessary.

@v-wuzhai
Copy link
Member Author

/azp run dotnet-sdk-public-ci,sdk-source-build,sdk-unified-build

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@v-wuzhai v-wuzhai enabled auto-merge December 16, 2024 10:25
@v-wuzhai v-wuzhai merged commit 14bb58e into main Dec 16, 2024
38 checks passed
@v-wuzhai v-wuzhai deleted the dev/v-wuzhai/enable-nullable branch December 16, 2024 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Infrastructure untriaged Request triage from a team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants