Description
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 ---
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) =>
aspnetcore/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/Endpoint.cs
Lines 164 to 170 in 58940e3
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);