aspnetcore: [10.0-preview.1] ASP.NET `RouteHandlerAnalyzer` - `System.IndexOutOfRangeException`

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I see some warnings like:

CSC : warning AD0001: Analyzer 'Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer' threw an exception of type 'System.IndexOutOfRangeException' with message 'Index was outside the bounds of the array.'. [c:\Users\erwin\bank-api\BankApi.Service.Stable\BankApi.Service.Stable.csproj]

When i build the .NET 10 version of my project: https://github.com/erwinkramer/bank-api/tree/feature/dotnet10, it didn’t happen on .NET 9. See the diff here: https://github.com/erwinkramer/bank-api/pull/6/files

Expected Behavior

Should just be as .NET 9, no warnings.

Steps To Reproduce

Build .NET 9 version on: https://github.com/erwinkramer/bank-api/tree/main - this doesn’t throw the warnings Build .NET 10 version on: https://github.com/erwinkramer/bank-api/tree/feature/dotnet10 - this throws the warnings

Exceptions (if any)

No response

.NET Version

10.0.100-preview.1.25120.13

Anything else?

Using VS Code

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 23 (22 by maintainers)

Most upvoted comments

@captainsafia Setting the sdk version in global.json did not resolve the issue in VS

I think we have done our due diligence and can be happy on the premise it is no longer flagging the warnings via dotnet build, taking the points you have made about VS into consideration.

I will give it a retry throughout newer preview versions of VS.

At some point I will try to look at adding some tests on minimal api’s calling an external library within this repo - either via the samples or tests folder as we have previously discussed.

Looks like 10.0.100-preview.3.25163.13 contains 00b6dbed5d8bb56caaa491c1cd4d3328edb78ef7, so looks like it should be.

@benhopkinstech No problem – I set the PR to auto-merge. If you’d like any guidance on the testing stuff let me know, I’ve done something similar for the source generator tests but those use a different abstractions so not all concepts might translate.

An alternative thought that crossed my mind: we can consider beefing up the sample app to include some endpoints defined in a class library. That should give us a smoke test in our build for these issues.

@benhopkinstech Great job finding this root cause! I suspect that this is happening because type arguments might not be stored in metadata so there’s no way to resolve them for external references from the analyzer?

The pickle is that we don’t have the best infrastructure for testing cross-reference APIs in our analyzers at the moment. That would have helped us catch this issue and perhaps other ones.

If you’re open to being nerdsniped to seeing if we can refactor our test infrastucture to support this, I would appreciate that.

If not, I’m comfotable merging the PR with the addition of a comment in-source to indicate what issues is resolved.

I will take a look tomorrow and create a simple repro locally.

Then I’ll see what tests I can come up with once I get a better understanding of what situations this comes up under.

I know I have personally used methods on endpoints in groups without the lambda and not experienced this error but this was after making use of IEndpointRouteBuilder.

@erwinkramer This is actually to do with your project structure, specifically around mapping your endpoints.

For example bankItems.MapGet("/", BankOperation.GetAllBanks) should be bankItems.MapGet("/", () => BankOperation.GetAllBanks)

Doing this across all of your endpoints in BankApi.Service.Beta and BankApi.Service.Stable resolves this issue in both .NET 9 and .NET 10 versions.

There are ways you can get this to work without having to wrap each endpoints method in lambda expressions but this would require some additional work in your project.

Please see this article which you might be able to take some inspiration from.

@erwinkramer Sorry for the issues you ran into with the build. I’ll admit that I dev primarily on Mac OS so tend to not exercise some of the Windows-specific codepaths.

@wtgodbe Does this error seem familiar to you?

On the other hand, @benhopkinstech has taken a stab at a bug fix for this. We just need to figure out what endpoint on the Bank API is exactly causing this issue.