Skip to content

Application using Request Delegate Generator fails to start with KeyNotFoundException #48379

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

Closed
1 task done
martincostello opened this issue May 23, 2023 · 5 comments
Closed
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-rdg NativeAOT Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Theme: cloud native

Comments

@martincostello
Copy link
Member

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Enabling the Request Delegate Generator on an existing application of mine is causing the application to fail to start due to a KeyNotFoundException being thrown.

There appears to be an issue with something in the generated code which I think is for this endpoint.

Expected Behavior

The application runs the same as when the Request Delegate Generator is not enabled.

Steps To Reproduce

  1. Clone martincostello/adventofcode@daf9193
  2. Run build.ps1

Exceptions (if any)

2023-05-23T10:56:13.4242935Z �[39;49m�[31m  Error Message:
2023-05-23T10:56:13.4245412Z �[39;49m�[39;49m�[31m   System.Collections.Generic.KeyNotFoundException : The given key '(/_/src/AdventOfCode.Site/Program.cs, 125)' was not present in the dictionary.
2023-05-23T10:56:13.4247192Z �[39;49m�[39;49m�[31m  Stack Trace:
2023-05-23T10:56:13.4249132Z �[39;49m�[39;49m�[31m     at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
2023-05-23T10:56:13.4251480Z    at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>FB49A76E00958A78ED8A3DC031D4149080B37ED6EB129C512F9112A3DB80F087D__GeneratedRouteBuilderExtensionsCore.MapCore(IEndpointRouteBuilder routes, String pattern, Delegate handler, IEnumerable`1 httpMethods, String filePath, Int32 lineNumber) in /_/src/AdventOfCode.Site/Microsoft.AspNetCore.Http.RequestDelegateGenerator/Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator/GeneratedRouteBuilderExtensions.g.cs:line 495
2023-05-23T10:56:13.4254264Z    at Microsoft.AspNetCore.Builder.GenerateRouteBuilderEndpoints.MapGet(IEndpointRouteBuilder endpoints, String pattern, Func`2 handler, String filePath, Int32 lineNumber) in /_/src/AdventOfCode.Site/Microsoft.AspNetCore.Http.RequestDelegateGenerator/Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator/GeneratedRouteBuilderExtensions.g.cs:line 47
2023-05-23T10:56:13.4256914Z    at Program.<Main>$(String[] args) in /_/src/AdventOfCode.Site/Program.cs:line 125
2023-05-23T10:56:13.4257501Z    at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
2023-05-23T10:56:13.4258095Z    at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
2023-05-23T10:56:13.4259446Z --- End of stack trace from previous location ---
2023-05-23T10:56:13.4260102Z    at Microsoft.AspNetCore.Mvc.Testing.DeferredHostBuilder.DeferredHost.StartAsync(CancellationToken cancellationToken)
2023-05-23T10:56:13.4260856Z    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Start(IHost host)
2023-05-23T10:56:13.4262418Z    at MartinCostello.AdventOfCode.Api.HttpServerFixture.CreateHost(IHostBuilder builder) in /_/tests/AdventOfCode.Tests/Api/HttpServerFixture.cs:line 99
2023-05-23T10:56:13.4263185Z    at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.ConfigureHostBuilder(IHostBuilder hostBuilder)
2023-05-23T10:56:13.4264532Z    at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
2023-05-23T10:56:13.4265181Z    at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
2023-05-23T10:56:13.4266580Z    at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
2023-05-23T10:56:13.4267706Z    at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
2023-05-23T10:56:13.4268366Z    at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient()
2023-05-23T10:56:13.4269716Z    at MartinCostello.AdventOfCode.Api.ApiTests.Api_Returns_400_If_Puzzle_Content_Incorrect() in /_/tests/AdventOfCode.Tests/Api/ApiTests.cs:line 309
2023-05-23T10:56:13.4271000Z --- End of stack trace from previous location ---

.NET Version

8.0.100-preview.4.23260.5

Anything else?

Curiously, this issue didn't happen for me locally, but only once I pushed it up to the repo and the GitHub Actions CI ran. Maybe it's got something to do with deterministic builds/CI in the source paths?

@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label May 23, 2023
@martincostello
Copy link
Member Author

Curiously, this issue didn't happen for me locally, but only once I pushed it up to the repo and the GitHub Actions CI ran. Maybe it's got something to do with deterministic builds/CI in the source paths?

Looking at the code locally, I wonder if this is indeed the case. Locally the generated code is this:

            [(@"C:\Coding\martincostello\adventofcode\src\AdventOfCode.Site\Program.cs", 125)] = (
                (methodInfo, options) =>
                {
                    Debug.Assert(options?.EndpointBuilder != null, "EndpointBuilder not found.");
                    options.EndpointBuilder.Metadata.Add(new SourceKey(@"C:\Coding\martincostello\adventofcode\src\AdventOfCode.Site\Program.cs", 125));
                    return new RequestDelegateMetadataResult { EndpointMetadata = options.EndpointBuilder.Metadata.AsReadOnly() };
                },
            // snip

Maybe there's a mismatch between the physical paths on disk and the mapped paths the compiler gets in the context of the source generator?

@martincostello
Copy link
Member Author

Possible dupe of #47918.

@mitchdenny
Copy link
Member

Try pulling down the latest nightly and see if your problem is solved. I think this ended up getting fixed.

@mitchdenny mitchdenny added Theme: cloud native feature-rdg area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc NativeAOT Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 23, 2023
@ghost
Copy link

ghost commented May 23, 2023

Hi @martincostello. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@martincostello
Copy link
Member Author

Verified fixed with version 8.0.100-preview.5.23273.9 of the .NET SDK.

@ghost ghost locked as resolved and limited conversation to collaborators Jul 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-rdg NativeAOT Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Theme: cloud native
Projects
None yet
Development

No branches or pull requests

2 participants