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

Suppress generating source checksums when configured #31089

Merged
1 commit merged into from
Mar 20, 2021
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 @@ -10,5 +10,8 @@
<RazorUpToDateReloadFileTypes>$(RazorUpToDateReloadFileTypes.Replace('.cshtml', ''))</RazorUpToDateReloadFileTypes>

<AddCshtmlFilesToDotNetWatchList>false</AddCshtmlFilesToDotNetWatchList>

<!-- Generate checksum attributes used to determine if a compiled view is out-of-sync with any of it's inputs -->
<GenerateRazorMetadataSourceChecksumAttributes>true</GenerateRazorMetadataSourceChecksumAttributes>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public override RazorCodeGenerationOptions Build()
SuppressMetadataAttributes,
SuppressPrimaryMethodBody,
SuppressNullabilityEnforcement,
OmitMinimizedComponentAttributeValues);
OmitMinimizedComponentAttributeValues)
{
SuppressMetadataSourceChecksumAttributes = SuppressMetadataSourceChecksumAttributes,
};
}

public override void SetDesignTime(bool designTime)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -92,6 +92,12 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
return;
}

if (documentNode.Options.SuppressMetadataSourceChecksumAttributes)
{
// Checksum attributes are turned off (or options not populated), nothing to do.
return;
}

// Checksum of the main source
var checksum = codeDocument.Source.GetChecksum();
var checksumAlgorithm = codeDocument.Source.GetChecksumAlgorithm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ public static RazorCodeGenerationOptions CreateDesignTime(Action<RazorCodeGenera
/// </remarks>
public virtual bool SuppressMetadataAttributes { get; protected set; }

/// <summary>
/// Gets a value that indicates whether to suppress the <c>RazorSourceChecksumAttribute</c>.
/// <para>
/// Used by default in .NET 6 apps since including a type-level attribute that changes on every
/// edit are treated as rude edits by hot reload.
/// </para>
/// </summary>
internal bool SuppressMetadataSourceChecksumAttributes { get; set; }

/// <summary>
/// Gets or sets a value that determines if an empty body is generated for the primary method.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public abstract class RazorCodeGenerationOptionsBuilder
/// </remarks>
public virtual bool SuppressMetadataAttributes { get; set; }

/// <summary>
/// Gets a value that indicates whether to suppress the <c>RazorSourceChecksumAttribute</c>.
/// <para>
/// Used by default in .NET 6 apps since including a type-level attribute that changes on every
/// edit are treated as rude edits by hot reload.
/// </para>
/// </summary>
internal bool SuppressMetadataSourceChecksumAttributes { get; set; }

/// <summary>
/// Gets or sets a value that determines if an empty body is generated for the primary method.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Razor.Language.Components;
Expand Down Expand Up @@ -381,6 +381,60 @@ public void Execute_HasRequiredInfo_AndImport_AddsItemAndSourceChecksum()
Assert.Equal("/Foo/Import.cshtml", checksum.Identifier);
}

[Fact]
public void Execute_SuppressMetadataSourceChecksumAttributes_DoesNotGenerateSourceChecksumAttributes()
{
// Arrange
var engine = CreateEngine();
var pass = new MetadataAttributePass()
{
Engine = engine,
};

var sourceDocument = TestRazorSourceDocument.Create("", new RazorSourceDocumentProperties(null, "Foo\\Bar.cshtml"));
var import = TestRazorSourceDocument.Create("@using System", new RazorSourceDocumentProperties(null, "Foo\\Import.cshtml"));
var codeDocument = RazorCodeDocument.Create(sourceDocument, new[] { import, });

var irDocument = new DocumentIntermediateNode()
{
DocumentKind = "test",
Options = RazorCodeGenerationOptions.Create(o => o.SuppressMetadataSourceChecksumAttributes = true),
};
var builder = IntermediateNodeBuilder.Create(irDocument);
var @namespace = new NamespaceDeclarationIntermediateNode
{
Annotations =
{
[CommonAnnotations.PrimaryNamespace] = CommonAnnotations.PrimaryNamespace,
},
Content = "Some.Namespace"
};
builder.Push(@namespace);
var @class = new ClassDeclarationIntermediateNode
{
Annotations =
{
[CommonAnnotations.PrimaryClass] = CommonAnnotations.PrimaryClass,
},
ClassName = "Test",
};
builder.Add(@class);

// Act
pass.Execute(codeDocument, irDocument);

// Assert
Assert.Equal(2, irDocument.Children.Count);

var item = Assert.IsType<RazorCompiledItemAttributeIntermediateNode>(irDocument.Children[0]);
Assert.Equal("/Foo/Bar.cshtml", item.Identifier);
Assert.Equal("test", item.Kind);
Assert.Equal("Some.Namespace.Test", item.TypeName);

var child = Assert.Single(@namespace.Children);
Assert.IsType<ClassDeclarationIntermediateNode>(child);
}

private static RazorEngine CreateEngine()
{
return RazorProjectEngine.Create(b =>
Expand Down