From a712ccc98ae84d208bd5f0817ed8e6320ca163c6 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 15 Jun 2018 09:45:41 -0700 Subject: [PATCH] Fix up error message when compilation references are missing --- .../CompilationFailedExceptionFactory.cs | 5 ++-- .../Properties/Resources.Designer.cs | 12 ++++----- .../Resources.resx | 4 +-- .../AntiforgeryTestHelper.cs | 2 +- .../ErrorPageTests.cs | 5 ++-- .../CompilerFailedExceptionFactoryTest.cs | 25 +++++++++++++++++++ 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/CompilationFailedExceptionFactory.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/CompilationFailedExceptionFactory.cs index 0be6fa6b7d..02b096ec75 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/CompilationFailedExceptionFactory.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/CompilationFailedExceptionFactory.cs @@ -77,9 +77,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal string.Equals(CS0234, g.Id, StringComparison.OrdinalIgnoreCase) || string.Equals(CS0246, g.Id, StringComparison.OrdinalIgnoreCase))) { - additionalMessage = Resources.FormatCompilation_DependencyContextIsNotSpecified( - "Microsoft.NET.Sdk.Web", - "PreserveCompilationContext"); + additionalMessage = Resources.FormatCompilation_MissingReferences( + "CopyRefAssembliesToPublishDirectory"); } var compilationFailure = new CompilationFailure( diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Properties/Resources.Designer.cs index 3b6a518dd3..a2e6436e78 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Properties/Resources.Designer.cs @@ -267,18 +267,18 @@ namespace Microsoft.AspNetCore.Mvc.Razor => string.Format(CultureInfo.CurrentCulture, GetString("LayoutHasCircularReference"), p0, p1); /// - /// 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. /// - internal static string Compilation_DependencyContextIsNotSpecified + internal static string Compilation_MissingReferences { - get => GetString("Compilation_DependencyContextIsNotSpecified"); + get => GetString("Compilation_MissingReferences"); } /// - /// 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. /// - internal static string FormatCompilation_DependencyContextIsNotSpecified(object p0, object p1) - => string.Format(CultureInfo.CurrentCulture, GetString("Compilation_DependencyContextIsNotSpecified"), p0, p1); + internal static string FormatCompilation_MissingReferences(object p0) + => string.Format(CultureInfo.CurrentCulture, GetString("Compilation_MissingReferences"), p0); /// /// '{0}' cannot be empty. These locations are required to locate a view for rendering. diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Resources.resx b/src/Microsoft.AspNetCore.Mvc.Razor/Resources.resx index 19b0e8f053..bab3fddf83 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Resources.resx +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Resources.resx @@ -173,8 +173,8 @@ A circular layout reference was detected when rendering '{0}'. The layout page '{1}' has already been rendered. - - 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. '{0}' cannot be empty. These locations are required to locate a view for rendering. diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTestHelper.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTestHelper.cs index 17ff0bb735..43bd304d14 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTestHelper.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTestHelper.cs @@ -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) diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ErrorPageTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ErrorPageTests.cs index 644a4ac067..31a74644b1 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ErrorPageTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ErrorPageTests.cs @@ -6,7 +6,6 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Text.Encodings.Web; using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Razor.Internal; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -17,8 +16,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public class ErrorPageTests : IClassFixture> { private static readonly string PreserveCompilationContextMessage = HtmlEncoder.Default.Encode( - "One or more compilation references are missing. Ensure that your project is referencing " + - "'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false."); + "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."); public ErrorPageTests(MvcTestFixture fixture) { Client = fixture.CreateDefaultClient(); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CompilerFailedExceptionFactoryTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CompilerFailedExceptionFactoryTest.cs index 05e156a6ef..394bc2ae7d 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CompilerFailedExceptionFactoryTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CompilerFailedExceptionFactoryTest.cs @@ -2,10 +2,12 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; +using System.Linq; using System.Text; using Microsoft.AspNetCore.Mvc.Razor.Extensions; using Microsoft.AspNetCore.Razor.Language; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Text; using Xunit; @@ -43,6 +45,29 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal 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()), + syntaxTree.ToString(), + "Test", + emitResult.Diagnostics); + + // Assert + Assert.Collection( + exception.CompilationFailures, + failure => Assert.Equal(expected, failure.FailureSummary)); + } + [Fact] public void GetCompilationFailedResult_UsesPhysicalPath() {