Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var parseOptions = context.ParseOptionsProvider;
var compilation = context.CompilationProvider;

// We might get initialized before razor tooling has a chance to set this, so check it any time something changes. It's extremely cheap and we'll stop propagating directly below if it didn't changed.
var useRazorCohostServer = context.AdditionalTextsProvider.Collect().Combine(context.CompilationProvider).Select((_, _) => RazorCohostingOptions.UseRazorCohostServer);

// determine if we should suppress this run and filter out all the additional files and references if so
var isGeneratorSuppressed = analyzerConfigOptions.CheckGlobalFlagSet("SuppressRazorSourceGenerator").Select((suppress, _) => !RazorCohostingOptions.UseRazorCohostServer && suppress);
var isGeneratorSuppressed = analyzerConfigOptions.CheckGlobalFlagSet("SuppressRazorSourceGenerator").Combine(useRazorCohostServer).Select((suppress, _) => !suppress.Right && suppress.Left);
var additionalTexts = context.AdditionalTextsProvider.EmptyOrCachedWhen(isGeneratorSuppressed, true);
var metadataRefs = context.MetadataReferencesProvider.EmptyOrCachedWhen(isGeneratorSuppressed, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3447,5 +3447,66 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.

"""");
}

[Fact]
public async Task UseRazorCohostServer_CanOverride_Suppression()
{
using var eventListener = new RazorEventListener();
var project = CreateTestProject(new()
{
["Component.Razor"] = "<h1>Hello world</h1>",
});
var compilation = await project.GetCompilationAsync();

// Start with the generator suppressed
var (driver, additionalTexts, optionsProvider) = await GetDriverWithAdditionalTextAndProviderAsync(project, configureGlobalOptions: (o) =>
{
o.TestGlobalOptions["build_property.SuppressRazorSourceGenerator"] = "true";
});

// results are empty, and no recorded steps run
var result = RunGenerator(compilation!, ref driver).VerifyPageOutput();
Assert.DoesNotContain("DocumentsWithSuppression", (IReadOnlyDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>)result.TrackedSteps);
Assert.DoesNotContain("ImplementationSourceOutput", (IReadOnlyDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>)result.TrackedOutputSteps);

// now enable UseRazorCohostServer
RazorCohostingOptions.UseRazorCohostServer = true;

// unfortunately if we re-run we'll still be in the suppressed mode. Because nothing in the compilation has changed
// there is no event that the source generator can 'trigger' on to re-evaluate.
result = RunGenerator(compilation!, ref driver).VerifyPageOutput();

// but if we make a trivial change to the compilation, we will re-run and get results
compilation = compilation!.WithAssemblyName(compilation.AssemblyName);

result = RunGenerator(compilation!, ref driver).VerifyPageOutput(@"
#pragma checksum ""Component.razor"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""6b5db227a6aa2228c777b0771108b184b1fc5df3""
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#line default
#line hidden
#nullable restore
public partial class Component : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, ""<h1>Hello world</h1>"");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
");
}
}
}
Loading