Skip to content

Commit a845656

Browse files
TagHelperCollection Part 4: Rewrite Tag Helper Discovery(!) (#12507)
| [Prelude](#12503) | [Part 1](#12504) | [Part 2](#12505) | [Part 3](#12506) | Part 4 | [Part 5](#12509) | > [!WARNING] > This pull request contains breaking changes for the RazorSdk. Once this is merged and flows to the VMR, dotnet/dotnet@dc17a09 will need to be cherry-picked to resolve build breaks in `src/sdk/src/RazorSdk`. Previously, tag helpers were discovered by `ITagHelperDescriptorProvider`. Each provider was responsible for walking a compilation's assemblies and producing `TagHelperDescriptors` from the types within. This change inverts the tag helper discovery process by introducing `ITagHelperDiscoveryService` and moving the tag helper construction logic into a set of `TagHelperProducers`. The `ITagHelperDiscoveryService` performs a single walk of the compilation or assembly and calls the producers as needed. Importantly, the new process allows a more expansive cache to be maintained. There is now a per-assembly cache that holds onto `TagHelperCollection` instances. The old cache that was owned by providers via `TagHelperCollector` has been removed. To complete this change, `ITagHelperDescriptorProvider` and related types have been _deleted_. ---- CI Build: https://dev.azure.com/dnceng/internal/_build/results?buildId=2842196&view=results Toolset Run: https://dev.azure.com/dnceng/internal/_build/results?buildId=2842249&view=results
2 parents 261d2e1 + f4a353a commit a845656

File tree

76 files changed

+3199
-2941
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+3199
-2941
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X;
1010

1111
// This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory.
12-
public class ViewComponentTagHelperDescriptorProviderTest
12+
public class ViewComponentTagHelperProducerTest
1313
{
1414
[Fact]
1515
public void DescriptorProvider_FindsVCTH()
@@ -24,12 +24,13 @@ public class StringParameterViewComponent
2424

2525
var compilation = MvcShim.BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
2626

27-
var context = new TagHelperDescriptorProviderContext(compilation);
28-
29-
var provider = new ViewComponentTagHelperDescriptorProvider()
27+
var projectEngine = RazorProjectEngine.CreateEmpty(static b =>
3028
{
31-
Engine = RazorProjectEngine.CreateEmpty().Engine,
32-
};
29+
b.Features.Add(new ViewComponentTagHelperProducer.Factory());
30+
b.Features.Add(new TagHelperDiscoveryService());
31+
});
32+
33+
Assert.True(projectEngine.Engine.TryGetFeature(out ITagHelperDiscoveryService? service));
3334

3435
var expectedDescriptor = TagHelperDescriptorBuilder.CreateViewComponent("__Generated__StringParameterViewComponentTagHelper", TestCompilation.AssemblyName)
3536
.TypeName("__Generated__StringParameterViewComponentTagHelper")
@@ -55,9 +56,9 @@ public class StringParameterViewComponent
5556
.Build();
5657

5758
// Act
58-
provider.Execute(context);
59+
var result = service.GetTagHelpers(compilation);
5960

6061
// Assert
61-
Assert.Single(context.Results, d => d.Equals(expectedDescriptor));
62+
Assert.Single(result, d => d.Equals(expectedDescriptor));
6263
}
6364
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X;
1010

1111
// This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory.
12-
public class ViewComponentTagHelperDescriptorProviderTest
12+
public class ViewComponentTagHelperProducerTest
1313
{
1414
[Fact]
1515
public void DescriptorProvider_FindsVCTH()
@@ -24,12 +24,13 @@ public class StringParameterViewComponent
2424

2525
var compilation = MvcShim.BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
2626

27-
var context = new TagHelperDescriptorProviderContext(compilation);
28-
29-
var provider = new ViewComponentTagHelperDescriptorProvider()
27+
var projectEngine = RazorProjectEngine.CreateEmpty(static b =>
3028
{
31-
Engine = RazorProjectEngine.CreateEmpty().Engine,
32-
};
29+
b.Features.Add(new ViewComponentTagHelperProducer.Factory());
30+
b.Features.Add(new TagHelperDiscoveryService());
31+
});
32+
33+
Assert.True(projectEngine.Engine.TryGetFeature(out ITagHelperDiscoveryService? service));
3334

3435
var expectedDescriptor = TagHelperDescriptorBuilder.CreateViewComponent("__Generated__StringParameterViewComponentTagHelper", TestCompilation.AssemblyName)
3536
.TypeName("__Generated__StringParameterViewComponentTagHelper")
@@ -55,9 +56,9 @@ public class StringParameterViewComponent
5556
.Build();
5657

5758
// Act
58-
provider.Execute(context);
59+
var result = service.GetTagHelpers(compilation);
5960

6061
// Assert
61-
Assert.Single(context.Results, d => d.Equals(expectedDescriptor));
62+
Assert.Single(result, d => d.Equals(expectedDescriptor));
6263
}
6364
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions;
1010

1111
// This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory.
12-
public class ViewComponentTagHelperDescriptorProviderTest
12+
public class ViewComponentTagHelperProducerTest
1313
{
1414
[Fact]
1515
public void DescriptorProvider_FindsVCTH()
@@ -24,12 +24,13 @@ public class StringParameterViewComponent
2424

2525
var compilation = TestCompilation.Create().AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
2626

27-
var context = new TagHelperDescriptorProviderContext(compilation);
28-
29-
var provider = new ViewComponentTagHelperDescriptorProvider()
27+
var projectEngine = RazorProjectEngine.CreateEmpty(static b =>
3028
{
31-
Engine = RazorProjectEngine.CreateEmpty().Engine,
32-
};
29+
b.Features.Add(new ViewComponentTagHelperProducer.Factory());
30+
b.Features.Add(new TagHelperDiscoveryService());
31+
});
32+
33+
Assert.True(projectEngine.Engine.TryGetFeature(out ITagHelperDiscoveryService? service));
3334

3435
var expectedDescriptor = TagHelperDescriptorBuilder.CreateViewComponent("__Generated__StringParameterViewComponentTagHelper", TestCompilation.AssemblyName)
3536
.TypeName("__Generated__StringParameterViewComponentTagHelper")
@@ -55,9 +56,9 @@ public class StringParameterViewComponent
5556
.Build();
5657

5758
// Act
58-
provider.Execute(context);
59+
var result = service.GetTagHelpers(compilation);
5960

6061
// Assert
61-
Assert.Single(context.Results, d => d.Equals(expectedDescriptor));
62+
Assert.Single(result, d => d.Equals(expectedDescriptor));
6263
}
6364
}

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/RazorProjectEngineTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private static void AssertDefaultFeatures(RazorProjectEngine engine)
8989
feature => Assert.IsType<InheritsDirectivePass>(feature),
9090
feature => Assert.IsType<MetadataAttributePass>(feature),
9191
feature => Assert.IsType<PreallocatedTagHelperAttributeOptimizationPass>(feature),
92+
feature => Assert.IsType<TagHelperDiscoveryService>(feature),
9293
feature => Assert.IsType<ViewCssScopePass>(feature));
9394
}
9495

0 commit comments

Comments
 (0)