diff --git a/src/RazorSdk/SourceGenerators/RazorSourceGenerator.cs b/src/RazorSdk/SourceGenerators/RazorSourceGenerator.cs index 974d2fb44c84..8a9412e741ad 100644 --- a/src/RazorSdk/SourceGenerators/RazorSourceGenerator.cs +++ b/src/RazorSdk/SourceGenerators/RazorSourceGenerator.cs @@ -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++) { diff --git a/src/Tests/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs b/src/Tests/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs index ecc572339b20..fe6b51d320e8 100644 --- a/src/Tests/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs +++ b/src/Tests/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs @@ -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() @@ -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);