Fix up error message when compilation references are missing

This commit is contained in:
Pranav K 2018-06-15 09:45:41 -07:00
parent 1aea6fd5bd
commit a712ccc98a
6 changed files with 38 additions and 15 deletions

View File

@ -77,9 +77,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
string.Equals(CS0234, g.Id, StringComparison.OrdinalIgnoreCase) || string.Equals(CS0234, g.Id, StringComparison.OrdinalIgnoreCase) ||
string.Equals(CS0246, g.Id, StringComparison.OrdinalIgnoreCase))) string.Equals(CS0246, g.Id, StringComparison.OrdinalIgnoreCase)))
{ {
additionalMessage = Resources.FormatCompilation_DependencyContextIsNotSpecified( additionalMessage = Resources.FormatCompilation_MissingReferences(
"Microsoft.NET.Sdk.Web", "CopyRefAssembliesToPublishDirectory");
"PreserveCompilationContext");
} }
var compilationFailure = new CompilationFailure( var compilationFailure = new CompilationFailure(

View File

@ -267,18 +267,18 @@ namespace Microsoft.AspNetCore.Mvc.Razor
=> string.Format(CultureInfo.CurrentCulture, GetString("LayoutHasCircularReference"), p0, p1); => string.Format(CultureInfo.CurrentCulture, GetString("LayoutHasCircularReference"), p0, p1);
/// <summary> /// <summary>
/// One or more compilation references are missing. Ensure that your project is referencing '{0}' and the '{1}' property is not set to false. /// One or more compilation references may be missing. If you're seeing this in a published application, set '{0}' to true in your project file to ensure files in the refs directory are published.
/// </summary> /// </summary>
internal static string Compilation_DependencyContextIsNotSpecified internal static string Compilation_MissingReferences
{ {
get => GetString("Compilation_DependencyContextIsNotSpecified"); get => GetString("Compilation_MissingReferences");
} }
/// <summary> /// <summary>
/// One or more compilation references are missing. Ensure that your project is referencing '{0}' and the '{1}' property is not set to false. /// One or more compilation references may be missing. If you're seeing this in a published application, set '{0}' to true in your project file to ensure files in the refs directory are published.
/// </summary> /// </summary>
internal static string FormatCompilation_DependencyContextIsNotSpecified(object p0, object p1) internal static string FormatCompilation_MissingReferences(object p0)
=> string.Format(CultureInfo.CurrentCulture, GetString("Compilation_DependencyContextIsNotSpecified"), p0, p1); => string.Format(CultureInfo.CurrentCulture, GetString("Compilation_MissingReferences"), p0);
/// <summary> /// <summary>
/// '{0}' cannot be empty. These locations are required to locate a view for rendering. /// '{0}' cannot be empty. These locations are required to locate a view for rendering.

View File

@ -173,8 +173,8 @@
<data name="LayoutHasCircularReference" xml:space="preserve"> <data name="LayoutHasCircularReference" xml:space="preserve">
<value>A circular layout reference was detected when rendering '{0}'. The layout page '{1}' has already been rendered.</value> <value>A circular layout reference was detected when rendering '{0}'. The layout page '{1}' has already been rendered.</value>
</data> </data>
<data name="Compilation_DependencyContextIsNotSpecified" xml:space="preserve"> <data name="Compilation_MissingReferences" xml:space="preserve">
<value>One or more compilation references are missing. Ensure that your project is referencing '{0}' and the '{1}' property is not set to false.</value> <value>One or more compilation references may be missing. If you're seeing this in a published application, set '{0}' to true in your project file to ensure files in the refs directory are published.</value>
</data> </data>
<data name="ViewLocationFormatsIsRequired" xml:space="preserve"> <data name="ViewLocationFormatsIsRequired" xml:space="preserve">
<value>'{0}' cannot be empty. These locations are required to locate a view for rendering.</value> <value>'{0}' cannot be empty. These locations are required to locate a view for rendering.</value>

View File

@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
} }
} }
throw new Exception($"Antiforgery token could not be located in {htmlDocument.TextContent}."); throw new Exception($"Antiforgery token could not be located in {htmlDocument.Source.Text}.");
} }
public static CookieMetadata RetrieveAntiforgeryCookie(HttpResponseMessage response) public static CookieMetadata RetrieveAntiforgeryCookie(HttpResponseMessage response)

View File

@ -6,7 +6,6 @@ using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Mvc.FunctionalTests namespace Microsoft.AspNetCore.Mvc.FunctionalTests
@ -17,8 +16,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public class ErrorPageTests : IClassFixture<MvcTestFixture<ErrorPageMiddlewareWebSite.Startup>> public class ErrorPageTests : IClassFixture<MvcTestFixture<ErrorPageMiddlewareWebSite.Startup>>
{ {
private static readonly string PreserveCompilationContextMessage = HtmlEncoder.Default.Encode( private static readonly string PreserveCompilationContextMessage = HtmlEncoder.Default.Encode(
"One or more compilation references are missing. Ensure that your project is referencing " + "One or more compilation references may be missing. " +
"'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false."); "If you're seeing this in a published application, set 'CopyRefAssembliesToPublishDirectory' to true in your project file to ensure files in the refs directory are published.");
public ErrorPageTests(MvcTestFixture<ErrorPageMiddlewareWebSite.Startup> fixture) public ErrorPageTests(MvcTestFixture<ErrorPageMiddlewareWebSite.Startup> fixture)
{ {
Client = fixture.CreateDefaultClient(); Client = fixture.CreateDefaultClient();

View File

@ -2,10 +2,12 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Mvc.Razor.Extensions; using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
using Xunit; using Xunit;
@ -43,6 +45,29 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
message.Message)); message.Message));
} }
[Fact]
public void GetCompilationFailedResult_WithMissingReferences()
{
// Arrange
var expected = "One or more compilation references may be missing. If you're seeing this in a published application, set 'CopyRefAssembliesToPublishDirectory' to true in your project file to ensure files in the refs directory are published.";
var compilation = CSharpCompilation.Create("Test", options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var syntaxTree = CSharpSyntaxTree.ParseText("@class Test { public string Test { get; set; } }");
compilation = compilation.AddSyntaxTrees(syntaxTree);
var emitResult = compilation.Emit(new MemoryStream());
// Act
var exception = CompilationFailedExceptionFactory.Create(
RazorCodeDocument.Create(RazorSourceDocument.Create("Test", "Index.cshtml"), Enumerable.Empty<RazorSourceDocument>()),
syntaxTree.ToString(),
"Test",
emitResult.Diagnostics);
// Assert
Assert.Collection(
exception.CompilationFailures,
failure => Assert.Equal(expected, failure.FailureSummary));
}
[Fact] [Fact]
public void GetCompilationFailedResult_UsesPhysicalPath() public void GetCompilationFailedResult_UsesPhysicalPath()
{ {