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

NullReferenceException during build #641

Closed
impworks opened this issue May 21, 2024 · 16 comments · Fixed by #655
Closed

NullReferenceException during build #641

impworks opened this issue May 21, 2024 · 16 comments · Fixed by #655
Assignees
Labels

Comments

@impworks
Copy link

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
Copy link
Author

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 bhagyshricompany self-assigned this May 28, 2024
@bhagyshricompany
Copy link

Thanks for updating will check and update the same.

@boylec
Copy link

boylec commented Jun 10, 2024

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
Copy link

bhagyshricompany commented Jun 11, 2024

I am checking.Will update.

@mhoumann
Copy link

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
Copy link

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

@mhoumann
Copy link

mhoumann commented Jun 18, 2024

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
Copy link

mhoumann commented Jun 18, 2024

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
Copy link

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
Copy link

Moerup commented Jun 27, 2024

Still a problem

@Skovvart
Copy link

Skovvart commented Jul 3, 2024

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

@jonnyguo1988
Copy link

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.

@impworks
Copy link
Author

Still a problem. The exact source line and the trigger have been identified and confirmed by several people, I have no idea which "additional feedback" MS bot says this issue requires

@bhagyshricompany
Copy link

please share the all sdk lib used with version.Thanks

@kshyju
Copy link
Member

kshyju commented Jul 16, 2024

Thanks @impworks I was able to repro with the below function code.

[FunctionName("Function1")]
public  IActionResult Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "foo/{id}")] HttpRequest req, string? id)
{
    return new OkObjectResult("hello world");
}

We will update the SDK with a fix. I will share an update on this thread when it is ready. Thanks for your patience.

@kshyju
Copy link
Member

kshyju commented Aug 1, 2024

Version 4.4.1 has been released, which includes a fix for this issue. Please upgrade to this version. Thank you for your patience.

https://www.nuget.org/packages/Microsoft.NET.Sdk.Functions/4.4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants