Buildalyzer icon indicating copy to clipboard operation
Buildalyzer copied to clipboard

Exception when analyzing a project with an empty Import element

Open marknfawaz opened this issue 5 years ago • 0 comments

Hi Dave,

Thank you again for this great project! This issue is related to a very specific use case that's causing a NPE when calling the RequiresNetFramework property in the ProjectFile class.

Basically the issue I saw is that someone was attempting to analyze a csproj file with an empty Import tag in their csproj file. While this is not a valid use case, since Project is a required attribute in Import, it is causing the property to fail.

The fix for this is pretty simple, adding a null check to the Project attribute before attempting to check it. It'll basically be changing this (Buildalyzer.Construction.ProjectFile, RequiresNetFramework Property)

_projectElement.GetDescendants(ProjectFileNames.Import).Any(x => ImportsThatRequireNetFramework.Any(i => x.GetAttributeValue(ProjectFileNames.Project).EndsWith(i, StringComparison.OrdinalIgnoreCase)))

to this:

_projectElement.GetDescendants(ProjectFileNames.Import).Any(x => ImportsThatRequireNetFramework.Any(i => x.GetAttributeValue(ProjectFileNames.Project)?.EndsWith(i, StringComparison.OrdinalIgnoreCase)==true))

This way, we can still check the other conditions in this property if this one fails.

Let me know if you're open to this change or you have questions/clarifications.

marknfawaz avatar Jan 27 '21 04:01 marknfawaz