dotnet / dotnet/roslyn

[Cohosting] `@bind-<prop>` tag helper descriptors don't get created

Open
#85,600 0 comments 0 reactions 1 assignee Claimed by @chsienki View on GitHub
Area-Razor Area-Razor-Compiler
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

I've spent an inordinate amount of time trying to work out why generating an event handler for `@bind-Value:set` doesn't work in our cohosting tests and I've finally gotten to the bottom of it.
This test: https://github.com/dotnet/razor/blob/8babb3ad96fe6ac7a28619b18b2f3a0c352e3d1a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CodeActions/GenerateEventHandlerTests.cs#L59

In the regular tooling code we discover tag helpers by creating a `TagHelperDescriptorProviderContext` with no target symbol, here: https://github.com/dotnet/razor/blob/8babb3ad96fe6ac7a28619b18b2f3a0c352e3d1a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/ProjectExtensions.cs#L55
In the source generator when finding tag helpers in references, the assembly symbol is used as the target symbol: https://github.com/dotnet/razor/blob/8babb3ad96fe6ac7a28619b18b2f3a0c352e3d1a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/RazorSourceGenerator.cs#L214

However, when the `BindTagHelperDescriptorProvider` runs, if the target symbol isn't null, it won't run unless the target symbol (ie assembly) is the same assembly as `Microsoft.AspNetCore.Components.BindConverter` is defined in. `BindConverter` is contained in `Microsoft.AspNetCore.Components.dll` and all of the standard components, like `InputText` etc., are in `Microsoft.AspNetCore.Components.Web.dll`.

Changing the source generator to pass `null` as the target symbol makes the test pass, but it also makes things take a _lot_ longer. Removing the logic in `BindTagHelperDescriptorProvider` also works, but there is a test that specifically validates the behaviour: https://github.com/dotnet/razor/blob/8babb3ad96fe6ac7a28619b18b2f3a0c352e3d1a/src/Compiler/Microsoft.CodeAnalysis.Razor/test/BindTagHelperDescriptorProviderTest.cs#L223

I'm guessing things work fine at runtime because the tag helpers are only in place to help tooling. I can't personally see any reason to keep the logic in `BindTagHelperDescriptorProvider` but maybe someone else has some more context. I removed it and checked that the generate methods tests pass, but I haven't run all of the rest of the compiler tests to know if there is any other impact.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.