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
@@ -0,0 +1,9 @@
namespace Microsoft.NET.Sdk.Razor.SourceGenerators;

internal static class RazorCohostingOptions
{
/// <summary>
/// True if razor is running in the cohosting mode
/// </summary>
internal static bool UseRazorCohostServer { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public partial class RazorSourceGenerator : IIncrementalGenerator
{
private static RazorSourceGeneratorEventSource Log => RazorSourceGeneratorEventSource.Log;

internal static bool UseRazorCohostServer { get; set; } = false;

// Testing usage only.
private readonly string? _testSuppressUniqueIds;

Expand All @@ -40,7 +38,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var compilation = context.CompilationProvider;

// 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, _) => !UseRazorCohostServer && suppress);
var isGeneratorSuppressed = analyzerConfigOptions.CheckGlobalFlagSet("SuppressRazorSourceGenerator").Select((suppress, _) => !RazorCohostingOptions.UseRazorCohostServer && suppress);
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 @@ -20,21 +20,10 @@ public void SetOptions(RemoteClientInitializationOptions options)
{
_options = options;

if (options.UseRazorCohostServer)
{
// ensure the source generator is in the correct mode
SetGeneratorToCohostMode();
}
// ensure the source generator is in the correct mode
RazorCohostingOptions.UseRazorCohostServer = options.UseRazorCohostServer;
}

/// <summary>
/// Sets the generator into cohosting mode.
/// </summary>
/// <remarks>
/// This is explicitly a separate method so that if not used we don't unnecessarily JIT any of the generator code.
/// </remarks>
private static void SetGeneratorToCohostMode() => RazorSourceGenerator.UseRazorCohostServer = true;

public override bool SupportsFileManipulation => _options.SupportsFileManipulation;

public override string CSharpVirtualDocumentSuffix => _options.CSharpVirtualDocumentSuffix;
Expand Down