Skip to content

RDG file name mismatch between SyntaxTree.FilePath and [CallerFilePath] causes runtime KeyNotFoundException  #47918

Closed
@halter73

Description

@halter73

SyntaxTree.FilePath and [CallerFilePath] can give different filenames for the same compilation on our own public AzDO PR build pipeline which ultimately causes runtime KeyNotFoundExceptions in code relying on RDG.

Example runtime stack trace:

System.Collections.Generic.KeyNotFoundException : The given key '(/_/src/Identity/Core/src/IdentityApiEndpointRouteBuilderExtensions.cs, 38)' was not present in the dictionary.
Stack trace
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>F69328E0708B4B584C5AACA22FE2C51A1CF192D6622828F613FC57C583CA77B63__GeneratedRouteBuilderExtensionsCore.MapCore(IEndpointRouteBuilder routes, String pattern, Delegate handler, IEnumerable`1 httpMethods, String filePath, Int32 lineNumber) in /_/src/Identity/Core/src/Microsoft.AspNetCore.Http.RequestDelegateGenerator/Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator/GeneratedRouteBuilderExtensions.g.cs:line 306
   at Microsoft.AspNetCore.Builder.GenerateRouteBuilderEndpoints.MapPost(IEndpointRouteBuilder endpoints, String pattern, Func`3 handler, String filePath, Int32 lineNumber) in /_/src/Identity/Core/src/Microsoft.AspNetCore.Http.RequestDelegateGenerator/Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator/GeneratedRouteBuilderExtensions.g.cs:line 47
   at Microsoft.AspNetCore.Routing.IdentityApiEndpointRouteBuilderExtensions.MapIdentityApi[TUser](IEndpointRouteBuilder endpoints) in /_/src/Identity/Core/src/IdentityApiEndpointRouteBuilderExtensions.cs:line 38
   at Microsoft.AspNetCore.Identity.FunctionalTests.MapIdentityTests.CreateAppAsync[TUser,TContext](Action`1 configureServices) in /_/src/Identity/test/Identity.FunctionalTests/MapIdentityTests.cs:line 216
   at Microsoft.AspNetCore.Identity.FunctionalTests.MapIdentityTests.CanReadBearerTokenFromQueryString() in /_/src/Identity/test/Identity.FunctionalTests/MapIdentityTests.cs:line 155
--- End of stack trace from previous location ---

https://dev.azure.com/dnceng-public/public/_build/results?buildId=253772&view=ms.vss-test-web.build-test-results-tab&runId=4932292&resultId=111661&paneView=debug

The disassembly of Microsoft.AspNetCore.Identity.dll from the PR shows RDG used "D:\a_work\1\s\src\Identity\Core\src\IdentityApiEndpointRouteBuilderExtensions.cs" from SyntaxTree.FilePath as the filename for the source key:

    file static class GeneratedRouteBuilderExtensionsCore
    {
        private static readonly Dictionary<(string, int), (MetadataPopulator, RequestDelegateFactoryFunc)> map = new()
        {
            [(@"D:\a\_work\1\s\src\Identity\Core\src\IdentityApiEndpointRouteBuilderExtensions.cs", 38)] = (
                (methodInfo, options) =>

private static (string, int) GetLocation(IInvocationOperation operation)
{
var filePath = operation.Syntax.SyntaxTree.FilePath;
var span = operation.Syntax.SyntaxTree.GetLineSpan(operation.Syntax.Span);
var lineNumber = span.StartLinePosition.Line + 1;
return (filePath, lineNumber);
}

But Roslyn used a different filename at the call site of the generated MapPost method for its [CallerFilePath] parameter. It used "/_/src/Identity/Core/src/IdentityApiEndpointRouteBuilderExtensions.cs" instead inside the same assembly:

    public static IEndpointConventionBuilder MapIdentityApi<TUser>(this IEndpointRouteBuilder endpoints) where TUser : class, new()
    {
        ArgumentNullException.ThrowIfNull(endpoints);
        var routeGroup = endpoints.MapGroup("");
        routeGroup.MapPost("/register",
            new Func<RegisterRequest, IServiceProvider, System.Threading.Tasks.Task<Results<Ok, ValidationProblem>>>(<>c__0<TUser>.<>9.<MapIdentityApi>b__0_0),
            "/_/src/Identity/Core/src/IdentityApiEndpointRouteBuilderExtensions.cs", 38);

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcbugThis issue describes a behavior which is not expected - a bug.feature-rdgold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions