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

Allow RazorProjectEngine.Process to be cancelled #11334

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
69011ac
RazorProjectEngine: Use ArgHelper.ThrowIfNull throughout
DustinCampbell Nov 8, 2024
dc2d73a
RazorProjectEngine: Remove two unneeded wrapper methods
DustinCampbell Nov 8, 2024
1c6fb18
RazorProjectEngine: Remove a couple of unneeded ArgHelper.ThrowIfNull
DustinCampbell Nov 8, 2024
5dea634
RazorProjectEngine: Do not allow nullable file kind
DustinCampbell Nov 8, 2024
2f7872d
RazorProjectEngine: Remove ArgumentNullExceptions in ctor
DustinCampbell Nov 8, 2024
141e53b
RazorProjectEngine: Clean up CreateCodeDocumentCore
DustinCampbell Dec 26, 2024
32fd56f
Introduce FeatureCache<T> helper class
DustinCampbell Dec 26, 2024
a600ba0
RazorEngine: Use ArgHelper.ThrowIfNull
DustinCampbell Dec 26, 2024
3e8210c
RazorEngine: Use FeatureCache<T>
DustinCampbell Dec 26, 2024
9bf66aa
Use RazorEngine.GetFeatures<T>() throughout
DustinCampbell Dec 26, 2024
0c482c3
RazorProjectEngine: Use FeatureCache<T>
DustinCampbell Dec 26, 2024
64394f2
Use RazorProjectEngine.GetFeatures<T>() throughout
DustinCampbell Dec 26, 2024
c728852
RazorProjectEngine: Don't expose EngineFeatures
DustinCampbell Dec 26, 2024
3fa7d25
RazorProjectEngine: Rename ProjectFeatures to Features
DustinCampbell Dec 26, 2024
14ed4ac
RazorProjectEngine: Pass PooledArrayBuilder to GetImportSourceDocuments
DustinCampbell Dec 26, 2024
7c76b22
RazorProjectEngine: Extract imports logic to GetImportSources method
DustinCampbell Dec 26, 2024
52b1246
RazorEnginePhaseBase: Use ArgHelper.ThrowIfNull
DustinCampbell Dec 26, 2024
95d5d76
Improvements to Assumed
DustinCampbell Dec 26, 2024
5708be6
Update AssumeNotNull extensions to include optional message
DustinCampbell Dec 26, 2024
1e065d1
RazorEnginePhaseBase: Clean up and add Initialize method
DustinCampbell Dec 26, 2024
e36c4be
RazorEngineFeatureBase: Clean up and add Initialize method
DustinCampbell Dec 26, 2024
f5b2355
RazorProjectEngineFeatureBase: Clean up and add Initialize method
DustinCampbell Dec 26, 2024
9edb972
SourceGeneratorProjectEngine: Don't inherit from RazorProjectEngine
DustinCampbell Dec 26, 2024
cffdcbf
Thread CancellationToken into compile phases and cancel from tooling
DustinCampbell Dec 26, 2024
9d55eb4
Fix telemetry stack walking
DustinCampbell Jan 6, 2025
1956af7
Move ThrowIfCancellationRequested call
DustinCampbell Jan 6, 2025
a86086c
Merge branch 'main' into compiler-cancellation
DustinCampbell Jan 7, 2025
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 @@ -3,7 +3,6 @@

#nullable disable

using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable disable

using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable disable

using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable disable

using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable disable

using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private sealed class TestPhase : RazorEnginePhaseBase
{
public int CallCount;

protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
Interlocked.Increment(ref CallCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
#nullable disable

using System;
using System.Threading;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
using Microsoft.AspNetCore.Razor.Language.Intermediate;

namespace Microsoft.AspNetCore.Razor.Language;

internal class DefaultRazorCSharpLoweringPhase : RazorEnginePhaseBase, IRazorCSharpLoweringPhase
{
protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var documentNode = codeDocument.GetDocumentIntermediateNode();
ThrowForMissingDocumentDependency(documentNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Threading;

namespace Microsoft.AspNetCore.Razor.Language;

Expand All @@ -14,7 +15,7 @@ protected override void OnInitialized()
Passes = Engine.GetFeatures<IRazorDirectiveClassifierPass>().OrderByAsArray(static x => x.Order);
}

protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var irDocument = codeDocument.GetDocumentIntermediateNode();
ThrowForMissingDocumentDependency(irDocument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Threading;

namespace Microsoft.AspNetCore.Razor.Language;

Expand All @@ -14,7 +15,7 @@ protected override void OnInitialized()
Passes = Engine.GetFeatures<IRazorDocumentClassifierPass>().OrderByAsArray(p => p.Order);
}

protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var irDocument = codeDocument.GetDocumentIntermediateNode();
ThrowForMissingDocumentDependency(irDocument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
Expand All @@ -28,7 +29,7 @@ protected override void OnInitialized()
_optionsFeature = GetRequiredFeature<IRazorCodeGenerationOptionsFeature>();
}

protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var syntaxTree = codeDocument.GetSyntaxTree();
ThrowForMissingDocumentDependency(syntaxTree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Threading;

namespace Microsoft.AspNetCore.Razor.Language;

Expand All @@ -14,7 +15,7 @@ protected override void OnInitialized()
Passes = Engine.GetFeatures<IRazorOptimizationPass>().OrderByAsArray(static x => x.Order);
}

protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var documentNode = codeDocument.GetDocumentIntermediateNode();
ThrowForMissingDocumentDependency(documentNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading;
using Microsoft.AspNetCore.Razor.PooledObjects;

namespace Microsoft.AspNetCore.Razor.Language;
Expand All @@ -15,7 +16,7 @@ protected override void OnInitialized()
_optionsFeature = GetRequiredFeature<IRazorParserOptionsFeature>();
}

protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var options = codeDocument.GetParserOptions() ?? _optionsFeature.AssumeNotNull().GetOptions();
var syntaxTree = RazorSyntaxTree.Parse(codeDocument.Source, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Threading;

namespace Microsoft.AspNetCore.Razor.Language;

Expand All @@ -14,7 +15,7 @@ protected override void OnInitialized()
Passes = Engine.GetFeatures<IRazorSyntaxTreePass>().OrderByAsArray(static x => x.Order);
}

protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var syntaxTree = codeDocument.GetSyntaxTree();
ThrowForMissingDocumentDependency(syntaxTree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.AspNetCore.Razor.Language.Syntax;
Expand All @@ -15,7 +16,7 @@ namespace Microsoft.AspNetCore.Razor.Language;

internal sealed partial class DefaultRazorTagHelperContextDiscoveryPhase : RazorEnginePhaseBase
{
protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var syntaxTree = codeDocument.GetPreTagHelperSyntaxTree() ?? codeDocument.GetSyntaxTree();
ThrowForMissingDocumentDependency(syntaxTree);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable enable

using System.Threading;
using Microsoft.AspNetCore.Razor.Language.Legacy;

namespace Microsoft.AspNetCore.Razor.Language;

internal sealed class DefaultRazorTagHelperRewritePhase : RazorEnginePhaseBase
{
protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var syntaxTree = codeDocument.GetPreTagHelperSyntaxTree();
var context = codeDocument.GetTagHelperContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading;

namespace Microsoft.AspNetCore.Razor.Language;

public interface IRazorEnginePhase
{
RazorEngine Engine { get; }

void Initialize(RazorEngine engine);
void Execute(RazorCodeDocument codeDocument);
void Execute(RazorCodeDocument codeDocument, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace Microsoft.AspNetCore.Razor.Language;

Expand Down Expand Up @@ -31,13 +32,16 @@ internal RazorEngine(ImmutableArray<IRazorEngineFeature> features, ImmutableArra
}
}

public void Process(RazorCodeDocument codeDocument)
public void Process(RazorCodeDocument codeDocument, CancellationToken cancellationToken = default)
{
ArgHelper.ThrowIfNull(codeDocument);

cancellationToken.ThrowIfCancellationRequested();

foreach (var phase in Phases)
{
phase.Execute(codeDocument);
phase.Execute(codeDocument, cancellationToken);
DustinCampbell marked this conversation as resolved.
Show resolved Hide resolved
cancellationToken.ThrowIfCancellationRequested();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public void Initialize(RazorEngine engine)
OnInitialized();
}

public void Execute(RazorCodeDocument codeDocument)
public void Execute(RazorCodeDocument codeDocument, CancellationToken cancellationToken = default)
{
ArgHelper.ThrowIfNull(codeDocument);
Assumed.NotNull(_engine, Resources.PhaseMustBeInitialized);

ExecuteCore(codeDocument);
ExecuteCore(codeDocument, cancellationToken);
}

protected T GetRequiredFeature<T>()
Expand Down Expand Up @@ -62,5 +62,5 @@ protected virtual void OnInitialized()
{
}

protected abstract void ExecuteCore(RazorCodeDocument codeDocument);
protected abstract void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.Language.Extensions;
Expand Down Expand Up @@ -45,30 +46,31 @@ public ImmutableArray<TFeature> GetFeatures<TFeature>()
where TFeature : class, IRazorProjectEngineFeature
=> _featureCache.GetFeatures<TFeature>();

public RazorCodeDocument Process(RazorProjectItem projectItem)
public RazorCodeDocument Process(RazorProjectItem projectItem, CancellationToken cancellationToken = default)
{
ArgHelper.ThrowIfNull(projectItem);

var codeDocument = CreateCodeDocumentCore(projectItem);
ProcessCore(codeDocument);
ProcessCore(codeDocument, cancellationToken);
return codeDocument;
}

public RazorCodeDocument Process(
RazorSourceDocument source,
string fileKind,
ImmutableArray<RazorSourceDocument> importSources,
IReadOnlyList<TagHelperDescriptor>? tagHelpers)
IReadOnlyList<TagHelperDescriptor>? tagHelpers,
CancellationToken cancellationToken = default)
{
ArgHelper.ThrowIfNull(source);
ArgHelper.ThrowIfNull(fileKind);

var codeDocument = CreateCodeDocumentCore(source, fileKind, importSources, tagHelpers, cssScope: null, configureParser: null, configureCodeGeneration: null);
ProcessCore(codeDocument);
ProcessCore(codeDocument, cancellationToken);
return codeDocument;
}

public RazorCodeDocument ProcessDeclarationOnly(RazorProjectItem projectItem)
public RazorCodeDocument ProcessDeclarationOnly(RazorProjectItem projectItem, CancellationToken cancellationToken = default)
{
ArgHelper.ThrowIfNull(projectItem);

Expand All @@ -77,15 +79,16 @@ public RazorCodeDocument ProcessDeclarationOnly(RazorProjectItem projectItem)
builder.SuppressPrimaryMethodBody = true;
});

ProcessCore(codeDocument);
ProcessCore(codeDocument, cancellationToken);
return codeDocument;
}

public RazorCodeDocument ProcessDeclarationOnly(
RazorSourceDocument source,
string fileKind,
ImmutableArray<RazorSourceDocument> importSources,
IReadOnlyList<TagHelperDescriptor>? tagHelpers)
IReadOnlyList<TagHelperDescriptor>? tagHelpers,
CancellationToken cancellationToken = default)
{
ArgHelper.ThrowIfNull(source);
ArgHelper.ThrowIfNull(fileKind);
Expand All @@ -95,30 +98,31 @@ public RazorCodeDocument ProcessDeclarationOnly(
builder.SuppressPrimaryMethodBody = true;
});

ProcessCore(codeDocument);
ProcessCore(codeDocument, cancellationToken);
return codeDocument;
}

public RazorCodeDocument ProcessDesignTime(RazorProjectItem projectItem)
public RazorCodeDocument ProcessDesignTime(RazorProjectItem projectItem, CancellationToken cancellationToken = default)
{
ArgHelper.ThrowIfNull(projectItem);

var codeDocument = CreateCodeDocumentDesignTimeCore(projectItem);
ProcessCore(codeDocument);
ProcessCore(codeDocument, cancellationToken);
return codeDocument;
}

public RazorCodeDocument ProcessDesignTime(
RazorSourceDocument source,
string fileKind,
ImmutableArray<RazorSourceDocument> importSources,
IReadOnlyList<TagHelperDescriptor>? tagHelpers)
IReadOnlyList<TagHelperDescriptor>? tagHelpers,
CancellationToken cancellationToken = default)
{
ArgHelper.ThrowIfNull(source);
ArgHelper.ThrowIfNull(fileKind);

var codeDocument = CreateCodeDocumentDesignTimeCore(source, fileKind, importSources, tagHelpers, configureParser: null, configureCodeGeneration: null);
ProcessCore(codeDocument);
ProcessCore(codeDocument, cancellationToken);
return codeDocument;
}

Expand Down Expand Up @@ -225,11 +229,11 @@ private RazorCodeDocument CreateCodeDocumentDesignTimeCore(
return codeDocument;
}

private void ProcessCore(RazorCodeDocument codeDocument)
private void ProcessCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
ArgHelper.ThrowIfNull(codeDocument);

Engine.Process(codeDocument);
Engine.Process(codeDocument, cancellationToken);
}

private TFeature GetRequiredFeature<TFeature>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Threading;
using Microsoft.AspNetCore.Razor.Language;

namespace Microsoft.AspNetCore.Razor.ProjectEngineHost;
Expand All @@ -9,7 +10,7 @@ internal class UnsupportedCSharpLoweringPhase : RazorEnginePhaseBase, IRazorCSha
{
internal const string UnsupportedDisclaimer = "// Razor CSharp output is not supported for this project's version of Razor.";

protected override void ExecuteCore(RazorCodeDocument codeDocument)
protected override void ExecuteCore(RazorCodeDocument codeDocument, CancellationToken cancellationToken)
{
var documentNode = codeDocument.GetDocumentIntermediateNode();
ThrowForMissingDocumentDependency(documentNode);
Expand Down
Loading
Loading