Skip to content

Commit

Permalink
Ensure the Razor generator doesn't generate when it's suppressed
Browse files Browse the repository at this point in the history
dotnet#24928 made a change to the Razor
generator to ensure that even if the generator is suppressed, it's
still going to run and cache it's outputs so later runs aren't a
from-scratch run on a performance-critical path. However, a bug
meant that if the generator was suppressed, it'd still run the first
time it's invoked, and will still output files even though it was
supposed to be suppressed.

The approach taken here is to suppress the addition of the files, but
at the very end of the chain only -- the generation and walking in
the middle of the incremental chain is left untouched, so that still
has the existing caching behavior.

This fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1545938
but we're still considering dotnet/roslyn#61675
as a tactical fix instead.
  • Loading branch information
jasonmalinowski committed Jun 3, 2022
1 parent 7462e68 commit 4dc8967
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/RazorSdk/SourceGenerators/RazorSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,17 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
return string.Equals(a.csharpDocument.GeneratedCode, b.csharpDocument.GeneratedCode, StringComparison.Ordinal);
}, static a => StringComparer.Ordinal.GetHashCode(a.csharpDocument));

context.RegisterSourceOutput(generatedOutput, static (context, pair) =>
context.RegisterSourceOutput(generatedOutput.Combine(isGeneratorSuppressed), static (context, pair) =>
{
var (hintName, csharpDocument) = pair;
var ((hintName, csharpDocument), isGeneratorSuppressed) = pair;

// We kept the generated trees around to maintain caches, but if we're suppressed we don't actually want to output in the very end;
// doing this this late into the pipeline means everything else is still maintained.
if (isGeneratorSuppressed)
{
return;
}

RazorSourceGeneratorEventSource.Log.AddSyntaxTrees(hintName);
for (var i = 0; i < csharpDocument.Diagnostics.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ internal sealed class Pages_Index : global::Microsoft.AspNetCore.Mvc.Razor.Razor
}

[Fact]
public async Task SourceGenerator_DoesNotUpdateSources_WhenSourceGeneratorIsSuppressed()
public async Task SourceGenerator_DoesNotOutput_WhenSourceGeneratorIsSuppressed()
{
// Regression test for https://github.com/dotnet/aspnetcore/issues/36227
var project = CreateTestProject(new()
Expand Down Expand Up @@ -2333,8 +2333,9 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.
driver = driver.WithUpdatedAnalyzerConfigOptions(suppressedOptions);

// results should be the same (even though we changed text)
result = RunGenerator(compilation!, ref driver)
.VerifyOutputsMatch(result);
var suppressedResult = RunGenerator(compilation!, ref driver);
Assert.Empty(suppressedResult.GeneratedSources);
Assert.Empty(suppressedResult.Diagnostics);

// now unsuppress and re-run
driver = driver.WithUpdatedAnalyzerConfigOptions(optionsProvider);
Expand Down

0 comments on commit 4dc8967

Please sign in to comment.