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

[8.0] RouteHandlerAnalyzer throws InvalidOperationException #50836

Closed
adamsitnik opened this issue Sep 20, 2023 · 20 comments
Closed

[8.0] RouteHandlerAnalyzer throws InvalidOperationException #50836

adamsitnik opened this issue Sep 20, 2023 · 20 comments
Assignees
Labels
analyzer Indicates an issue which is related to analyzer experience area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Comments

@adamsitnik
Copy link
Member

I've hit the following error while working on Xabaril/AspNetCore.Diagnostics.HealthChecks#2044.

CSC : error AD0001: Analyzer 'Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Failed to resolve well-known type 'Microsoft.AspNetCore.Builde
r.EndpointRouteBuilderExtensions'.'. 
PS D:\projects\forks\healthChecks> dotnet --info
.NET SDK:
 Version:   8.0.100-rc.2.23429.6
 Commit:    4bafa2271a

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100-rc.2.23429.6\

Repro:

git clone https://github.com/adamsitnik/AspNetCore.Diagnostics.HealthChecks.git --branch reproBug reproBug
cd reproBug
dotnet build .\test\HealthChecks.UI.Tests\HealthChecks.UI.Tests.csproj
@adamsitnik adamsitnik added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Sep 20, 2023
@captainsafia captainsafia added analyzer Indicates an issue which is related to analyzer experience area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Sep 20, 2023
@captainsafia
Copy link
Member

Thanks for reporting this issue, @adamsitnik! We've gotten a few similar reports over the past few days including in #50868 and #50881. I'll close those in favor of using this one as the investigation.

#50868 highlights the framework reference being included as possibly related but I think that there might be a red herring considering the nature of the other bug reports. I'll dig in and see what I can find...I suspect upgrading runtime versions ithe more likely dilemma here...

@captainsafia
Copy link
Member

OK, it turns out that this is related to the fact that the HealthChecks UI package uses the same type name in their sources as we do in ours:

https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/4607fc5b338388d3fcf302fc60fb004961825b9a/src/HealthChecks.UI/Extensions/EndpointRouteBuilderExtensions.cs#L9

Since we use GetTypeByMetadataName, it will return null if types by the same name are encountered in different assemblies. We can be more defensive here and use GetTypesByMetadataName then filter to the assemblies that we care about.

@glen-84
Copy link
Contributor

glen-84 commented Nov 15, 2023

This is also affecting HotChocolate.

@vip32
Copy link

vip32 commented Nov 15, 2023

package AspNetCore.HealthChecks.UI (7.0.2) is also causing this issue to appear

@michaelmcneilnet
Copy link

michaelmcneilnet commented Nov 15, 2023

Is there any workaround for this? We're using HotChocolate and hitting this blocking issue on upgrading to DotNet8 today 😢

Did #51276 not make it into the final release?

@glen-84
Copy link
Contributor

glen-84 commented Nov 15, 2023

@michaelmcneilnet

Is there any workaround for this?

<PropertyGroup>
  <NoWarn>$(NoWarn);AD0001</NoWarn>
</PropertyGroup>

@michaelmcneilnet
Copy link

@michaelmcneilnet

Is there any workaround for this?

<PropertyGroup>
  <NoWarn>$(NoWarn);AD0001</NoWarn>
</PropertyGroup>

Thanks! This set me on the correct path, but in my case I had to do

<NoWarn>$(NoWarn);ASP0003;ASP0004;ASP0005;ASP0007;ASP0020;ASP0021;ASP0022;ASP0024</NoWarn>

This list showed up in one of the errors:

image

@tmitchel2
Copy link

I get 3 similar warnings in 3 different analyzers... (I'm using HotChocolate also)

warning AD0001: Analyzer 'Microsoft.AspNetCore.Analyzers.WebApplicationBuilder.WebApplicationBuilderAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Failed to resolve well-known type 'Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions'

warning AD0001: Analyzer 'Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Failed to resolve well-known type 'Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions'.'

warning AD0001: Analyzer 'Microsoft.AspNetCore.Analyzers.RouteEmbeddedLanguage.RoutePatternAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Failed to resolve well-known type 'Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions'

@jeremy-allocate
Copy link

jeremy-allocate commented Nov 15, 2023

I cannot believe this wasn't resolved before 8 went live to the world... the fix was supposedly merged a month ago: #51276

@captainsafia
Copy link
Member

Thanks for the feedback here folks! The actual fix to this issue is in #50969. I'm bumped CI and request for reviews on this PR so that we can get it in the next servicing cycle.

In the meantime, the workarounds identified in this comment should be sufficient for projects running into this with the dependencies at hand.

For context, this issue will manifest if any library references in your application are also declaring methods in the Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions static class. In this case, Roslyn's lookup will throw an exception when it discovers two types of the same name when resolving type symbols by name from metadata.

The fix here was to modify the behavior of our lookup to use alternative APIs that only checked for types based on name and assembly.

@captainsafia captainsafia removed this from the 8.0.0 milestone Nov 15, 2023
@vipwan
Copy link

vipwan commented Nov 16, 2023

1>CSC : warning AD0001: 分析器“Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer”引发类型为“System.InvalidOperationException”的异常,并显示消息“Failed to resolve well-known type 'Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions'.”。

The compilation result is not affected, but the error message is annoying

@saddamhossain
Copy link

I am facing the same issues when I use AspNetCore.HealthChecks.UI (version 7.0.2)

smfeest added a commit to smfeest/buttercup that referenced this issue Nov 19, 2023
The current version of .NET 8.0 contains a bug [1] in
`RouteHandlerAnalyzer` that causes it to throw
`InvalidOperationException` with message 'Failed to resolve well-known
type 'Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions' when
the application references any libraries that declare methods in the
`Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions` static
class.

A fix [2]  is expected [3] to be included in an upcoming .NET 8 patch
release.

[1] dotnet/aspnetcore#50836
[2] dotnet/aspnetcore#50969
[3] dotnet/aspnetcore#50836 (comment)
lazcool added a commit to DFE-Digital/fh-service-directory-admin-ui that referenced this issue Nov 20, 2023
@captainsafia
Copy link
Member

To follow-up on this, the fix for this has been merged and is expected to be released in the next servicing release of .NET 8. In the meantime, recommend using the workarounds mentioned above.

@onionhammer
Copy link

onionhammer commented Dec 29, 2023

Anyone else seeing this?

Request textDocument/semanticTokens/range failed.
Message: Failed to resolve well-known type 'Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions'.
Code: -32000

Using HotChocolate, but adding a simple route near the bottom of my file causes this error

app.MapGet("/graphql/getdocument", () => Results.Ok("hi"));

image

Error goes away if that line is commented out

@captainsafia
Copy link
Member

@onionhammer I haven't seen that one before.

My hunch tells me that textDocument/semanticTokens/range is an operation that triggers an analyzer run, but the analyzers fails to load (because of the issue discussed here), and that triggers cascading failures in the extension.

Does using the workarounds mentioned above resolve the issue with the C# extension?

@onionhammer
Copy link

onionhammer commented Dec 29, 2023

Does using the workarounds mentioned above resolve the issue with the C# extension?

No, I already had that NoWarn set up ages ago. This issue started happening more recently

@captainsafia
Copy link
Member

Does using the workarounds mentioned above resolve the issue with the C# extension?

No, I already had that NoWarn set up ages ago. This issue started happening more recently

@onionhammer OK, in that case. I'd recommend trying to see if updating to the upcoming .NET 8 release will resolve the issue. If not, I'd recommend filing an issue against the C# extension repo, especially if you are not seeing these exceptions in VS or when building from the commandline.

@onionhammer
Copy link

I only started seeing it after .net 8

@captainsafia
Copy link
Member

Hi everyone -- the bug fix has been released as part of today's (January 9th) monthly release. You can download it at https://dotnet.microsoft.com/en-us/download/dotnet/8.0. Let me know if there are any other issues!

@tmitchel2
Copy link

Great 👍🏻 thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer Indicates an issue which is related to analyzer experience area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Projects
Status: Done
Development

No branches or pull requests