Azure / azure-functions-vs-build-sdk

MSBuild task for Azure Functions
MIT License
96 stars 62 forks source link

NullReferenceException during build #641

Open impworks opened 1 month ago

impworks commented 1 month ago

After updating the project to .NET 8 I started getting build errors:

System.NullReferenceException: Object reference not set to an instance of an object.
   at MakeFunctionJson.AttributeExtensions.IsWebJobsAttribute(CustomAttribute attribute) in /_/src/Microsoft.NET.Sdk.Functions.Generator/AttributeExtensions.cs:line 52
   at MakeFunctionJson.ParameterInfoExtensions.<>c.<ToFunctionJsonBindings>b__1_0(CustomAttribute a) in /_/src/Microsoft.NET.Sdk.Functions.Generator/ParameterInfoExtensions.cs:line 31
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MakeFunctionJson.ParameterInfoExtensions.ToFunctionJsonBindings(ParameterDefinition parameterInfo) in /_/src/Microsoft.NET.Sdk.Functions.Generator/ParameterInfoExtensions.cs:line 29
   at MakeFunctionJson.MethodInfoExtensions.<>c.<ToFunctionJson>b__6_0(ParameterDefinition p) in /_/src/Microsoft.NET.Sdk.Functions.Generator/MethodInfoExtensions.cs:line 66
   at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.SparseArrayBuilder`1.ReserveOrAdd(IEnumerable`1 items)
   at System.Linq.Enumerable.Concat2Iterator`1.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at MakeFunctionJson.MethodInfoExtensions.ToFunctionJson(MethodDefinition method, String assemblyPath) in /_/src/Microsoft.NET.Sdk.Functions.Generator/MethodInfoExtensions.cs:line 65
   at MakeFunctionJson.FunctionJsonConverter.GenerateFunctions(IEnumerable`1 types)+MoveNext() in /_/src/Microsoft.NET.Sdk.Functions.Generator/FunctionJsonConverter.cs:line 140
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MakeFunctionJson.FunctionJsonConverter.TryGenerateFunctionJsons() in /_/src/Microsoft.NET.Sdk.Functions.Generator/FunctionJsonConverter.cs:line 194
   at MakeFunctionJson.FunctionJsonConverter.TryRun() in /_/src/Microsoft.NET.Sdk.Functions.Generator/FunctionJsonConverter.cs:line 87

The NuGet package version is the latest currently available (4.4.0).

impworks commented 1 month ago

The root of the problem seems to be a nullable argument from a HttpTrigger, like string? arg in the example below:

[FunctionName("DoStuff")]
public Task DoStuff([HttpTrigger("get", Route = "v1/DoStuff/{arg?}")] HttpRequest req, string? arg, ILogger log)
{
    return Task.CompletedTask;
}

When changing the type from string? to just string, the compilation succeeds.

bhagyshricompany commented 1 month ago

Thanks for updating will check and update the same.

boylec commented 3 weeks ago

I'm getting this issue but I don't have any HttpTrigger decorated functions with a nullable route segment in fact all of my HttpTrigger functions are parameterless except an "HttpRequest req" parameter

bhagyshricompany commented 3 weeks ago

I checked and found working fine .can you send your api url with arg

mhoumann commented 3 weeks ago

I'm getting the same going from v4.0.0 to v4.4.0. Haven't found out what triggers it yet. No [HttpTrigger] and no nullable argument types.

3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error : System.NullReferenceException: Object reference not set to an instance of an object.
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at MakeFunctionJson.AttributeExtensions.IsWebJobsAttribute(CustomAttribute attribute) in /_/src/Microsoft.NET.Sdk.Functions.Generator/AttributeExtensions.cs:line 52
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at MakeFunctionJson.MethodInfoExtensions.<>c.<GetOutputBindingsFromReturnAttribute>b__7_0(CustomAttribute a) in /_/src/Microsoft.NET.Sdk.Functions.Generator/MethodInfoExtensions.cs:line 101
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at MakeFunctionJson.MethodInfoExtensions.GetOutputBindingsFromReturnAttribute(MethodDefinition method) in /_/src/Microsoft.NET.Sdk.Functions.Generator/MethodInfoExtensions.cs:line 101
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at MakeFunctionJson.MethodInfoExtensions.ToFunctionJson(MethodDefinition method, String assemblyPath) in /_/src/Microsoft.NET.Sdk.Functions.Generator/MethodInfoExtensions.cs:line 65
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at MakeFunctionJson.FunctionJsonConverter.GenerateFunctions(IEnumerable`1 types)+MoveNext() in /_/src/Microsoft.NET.Sdk.Functions.Generator/FunctionJsonConverter.cs:line 140
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at MakeFunctionJson.FunctionJsonConverter.TryGenerateFunctionJsons() in /_/src/Microsoft.NET.Sdk.Functions.Generator/FunctionJsonConverter.cs:line 194
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :    at MakeFunctionJson.FunctionJsonConverter.TryRun() in /_/src/Microsoft.NET.Sdk.Functions.Generator/FunctionJsonConverter.cs:line 87
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error : Error generating functions metadata
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error :
3>C:\Users\mh\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Build.targets(32,5): error : Metadata generation failed.
bhagyshricompany commented 3 weeks ago

if possible can you share the code snippet or still you getting the same issue.

mhoumann commented 2 weeks ago

I'll try to find at repro, and post it if/when I do. But with no indication of what is actually going wrong, there's not much to go on.

I can understand wanting to find a root cause. But still it seems like preventing the NullReferenceException in AttributeExtensions.IsWebJobsAttribute should be a straight forward fix.

Just tested with 4.1.0 -> 4.3.0 and all have the same error.

mhoumann commented 2 weeks ago

Cause of error seems to be using a tuple return value in my case:

[FunctionName("SomeActivity")]
public async Task<(int Id, MyState State, string Identifier)> ExecuteAsync([ActivityTrigger] MyArguments args, CancellationToken cancellationToken)

Fixed by returning class type instead.

tskardal commented 2 weeks ago

I had a similar error. I commented out all the [FunctionName] attributes in my project and added them back in until I found the one causing the error.

I my case the problem was a function that returned a Task<List<Something>?> (Don't ask me why the list is nullable😅. It is legacy code). After removing the nullable (which also caused a warning) it built successfully.

Now the return type is Task<List<Something>>

Moerup commented 1 week ago

Still a problem

Skovvart commented 5 days ago

I also experienced the issue with a return value of IEnumerable<KeyValuePair<string, string>> (assuming the KeyValuePair was the issue)

jonnyguo1988 commented 17 hours ago

I also encountered this issue because I enabled enable, when I use any nullable returns of a durable, like Task<Resource?>, this build error happens.