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

Use source generators to compile Razor files #28807

Closed
wants to merge 1 commit into from

Conversation

pranavkm
Copy link
Contributor

@pranavkm pranavkm commented Dec 22, 2020

This change replaces the use of RazorGenerate and RazorTagHelper tasks, and rzc, with a source generator that discovers tag helpers, code generates .razor and .cshtml files, and compiles the .razor files. .cshtml files are written to disk that are used during .Views.dll compilation.

Some notes:

  • The SDK continues to ship rzc, and tasks to support < net6.0 apps. It might be possible to use the source generator in 3.1 and later apps, but there are some implications to doing this which need further thought and discussion. I've punted on this for the time being.
  • For 6.0 apps, a lot of the Razor pipeline that was previously driven by MSBuild is now part of a single source generator.
  • Discovering tag helpers afresh on every build is really expensive. It's much more effective to be able to cache some results and use it in consecutive builds. One strategy that works very well for this is to partition the results so that tag helpers from referenced assemblies are cached but not the ones from the app itself. Right now we're caching to disk which is not recommended., We'll work on getting APIs that allow caching in memory to avoid the additional serialization costs.

Open issues:

  • Figure out how source generators work in the IDE. Right now every key stroke results in the source generator running completely in the IDE. Caching results helps, but we need to do better. This is a problem for all source generators that the compiler team is aware of.
  • Make razor participate in EnC. There's some WIP work in this PR that hasn't been shown to work, but I'm leaving it in there to make my life easier.
  • Assess SDK change impact. The RazorSDK no longer invokes a bunch of targets that were previously part of the build pipeline . This was observed as part of the IdentityUI project that was part of our repo, but it's not clear how this affects projects in the wild.
Before After %
Cold start 3735 ms 2740 ms 26
Warm build 1321 ms 1074 ms 19
No-op build 323 ms 316 ms 2
Change to .cs file 713 ms 600 ms 16
Change to .cshtml file 632 ms 575 ms 9
Change to .razor file 641 ms 576 ms 10
Change to .razor file to add parameter 774 ms 591 ms 23

@pranavkm pranavkm changed the title Use source generators to compile .razor files Use source generators to compile Razor files Dec 22, 2020
@pranavkm pranavkm force-pushed the prkrishn/component-source-gen branch from 507be65 to 544efd1 Compare December 23, 2020 02:14
@mkArtakMSFT mkArtakMSFT added the area-razor.compiler This issue is related to the Razor compiler (now external) label Dec 23, 2020
@pranavkm pranavkm force-pushed the prkrishn/component-source-gen branch from 544efd1 to 0874ea6 Compare December 23, 2020 16:14
{
var path = item.Path;
var isComponent = path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase);
var isRazorView = !isComponent && path.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"!isComponent" is redundant check now. if path.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase) == true then path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase) = false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-razor.compiler This issue is related to the Razor compiler (now external)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants