phmonte / Buildalyzer

A utility to perform design-time builds of .NET projects without having to think too hard about it.
MIT License
593 stars 94 forks source link

Exception when analyzing a project with an empty Import element #167

Closed marknfawaz closed 4 months ago

marknfawaz commented 3 years ago

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.

phmonte commented 4 months ago

Hello @marknfawaz , sorry for the delay. It seems interesting to me, if you can make a PR I would love it, if not, I'll add it to the backlog.

phmonte commented 4 months ago

Closing the issue due to lack of feedback, if the problem persists, please open a new one.