diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation/Internal/PrecompileRunCommand.cs b/src/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation/Internal/PrecompileRunCommand.cs index e5278364a1..8cda93e115 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation/Internal/PrecompileRunCommand.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation/Internal/PrecompileRunCommand.cs @@ -57,6 +57,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal var results = GenerateCode(); var success = true; + foreach (var result in results) { if (!result.GeneratorResults.Success) @@ -64,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal success = false; foreach (var error in result.GeneratorResults.ParserErrors) { - Application.Error.WriteLine($"{error.Location.FilePath} ({error.Location.LineIndex}): {error.Message}"); + Application.Error.WriteLine($"{result.ViewFileInfo.FullPath} ({error.Location.LineIndex}): {error.Message}"); } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/ApplicationWithParseErrorsTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/ApplicationWithParseErrorsTest.cs new file mode 100644 index 0000000000..bca6533535 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/ApplicationWithParseErrorsTest.cs @@ -0,0 +1,68 @@ +// 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; +using System.IO; +using System.Linq; +using Microsoft.AspNetCore.Server.IntegrationTesting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Testing; +using Xunit; + +namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation +{ + public class ApplicationWithParseErrorsTest : IClassFixture + { + public ApplicationWithParseErrorsTest(ApplicationWithParseErrorsFixture fixture) + { + Fixture = fixture; + } + + public ApplicationWithParseErrorsFixture Fixture { get; } + + public static TheoryData SupportedFlavorsTheoryData => RuntimeFlavors.SupportedFlavorsTheoryData; + + [Theory] + [MemberData(nameof(SupportedFlavorsTheoryData))] + public void PublishingPrintsParseErrors(RuntimeFlavor flavor) + { + var indexPath = Path.Combine(Fixture.ApplicationPath, "Views", "Home", "Index.cshtml"); + var viewImportsPath = Path.Combine(Fixture.ApplicationPath, "Views", "Home", "About.cshtml"); + var expectedErrors = new[] + { + indexPath + " (0): The code block is missing a closing \"}\" character. Make sure you have a matching \"}\" character for all the \"{\" characters within this block, and that none of the \"}\" characters are being interpreted as markup.", + viewImportsPath + " (1): A space or line break was encountered after the \"@\" character. Only valid identifiers, keywords, comments, \"(\" and \"{\" are valid at the start of a code block and they must occur immediately following \"@\" with no space in between.", + + }; + using (var deployer = Fixture.CreateDeployment(flavor)) + { + // Act & Assert + Assert.Throws(() => deployer.Deploy()); + + // Assert + var output = Fixture.TestSink.Writes.Select(w => w.State.ToString().Trim()).ToList(); + + foreach (var error in expectedErrors) + { + Assert.Contains(error, output); + } + } + } + + + public class ApplicationWithParseErrorsFixture : ApplicationTestFixture + { + public ApplicationWithParseErrorsFixture() + : base("ApplicationWithParseErrors") + { + } + + public TestSink TestSink { get; } = new TestSink(); + + protected override ILogger CreateLogger(RuntimeFlavor flavor) + { + return new TestLoggerFactory(TestSink, enabled: true).CreateLogger($"{ApplicationName}:{flavor}"); + } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/Infrastructure/ApplicationTestFixture.cs b/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/Infrastructure/ApplicationTestFixture.cs index c1d27d43e5..d805ddc687 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/Infrastructure/ApplicationTestFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/Infrastructure/ApplicationTestFixture.cs @@ -39,9 +39,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation public IApplicationDeployer CreateDeployment(RuntimeFlavor flavor) { - Logger = new LoggerFactory() - .AddConsole() - .CreateLogger($"{ApplicationName}:{flavor}"); + Logger = CreateLogger(flavor); if (!_isRestored) { @@ -87,6 +85,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation return ApplicationDeployerFactory.Create(deploymentParameters, Logger); } + protected virtual ILogger CreateLogger(RuntimeFlavor flavor) + { + return new LoggerFactory() + .AddConsole() + .CreateLogger($"{ApplicationName}:{flavor}"); + } + protected virtual void Restore() { RestoreProject(ApplicationPath); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests.csproj b/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests.csproj index 5c3fc9f160..a12b3c95aa 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests.csproj +++ b/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests.csproj @@ -4,11 +4,9 @@ netcoreapp1.1 $(DefineConstants);__remove_this_to__GENERATE_BASELINES - - @@ -17,6 +15,7 @@ + diff --git a/testapps/ApplicationWithParseErrors/ApplicationWithParseErrors.csproj b/testapps/ApplicationWithParseErrors/ApplicationWithParseErrors.csproj new file mode 100644 index 0000000000..22347b4ca5 --- /dev/null +++ b/testapps/ApplicationWithParseErrors/ApplicationWithParseErrors.csproj @@ -0,0 +1,22 @@ + + + netcoreapp1.1;net451 + true + Exe + win7-x64 + true + + + + 1.1.0-* + All + + + + + + + + + + diff --git a/testapps/ApplicationWithParseErrors/Program.cs b/testapps/ApplicationWithParseErrors/Program.cs new file mode 100644 index 0000000000..0ceb49039f --- /dev/null +++ b/testapps/ApplicationWithParseErrors/Program.cs @@ -0,0 +1,26 @@ +using System.IO; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; + +namespace ApplicationWithParseErrors +{ + public class Program + { + public static void Main(string[] args) + { + var config = new ConfigurationBuilder() + .AddCommandLine(args) + .AddEnvironmentVariables(prefix: "ASPNETCORE_") + .Build(); + + var host = new WebHostBuilder() + .UseConfiguration(config) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .Build(); + + host.Run(); + } + } +} diff --git a/testapps/ApplicationWithParseErrors/Startup.cs b/testapps/ApplicationWithParseErrors/Startup.cs new file mode 100644 index 0000000000..b573e39a8c --- /dev/null +++ b/testapps/ApplicationWithParseErrors/Startup.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace ApplicationWithParseErrors +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc(); + } + + public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) + { + loggerFactory.AddConsole(); + app.UseMvcWithDefaultRoute(); + } + } +} diff --git a/testapps/ApplicationWithParseErrors/Views/Home/About.cshtml b/testapps/ApplicationWithParseErrors/Views/Home/About.cshtml new file mode 100644 index 0000000000..ddda42f574 --- /dev/null +++ b/testapps/ApplicationWithParseErrors/Views/Home/About.cshtml @@ -0,0 +1,2 @@ +@using ApplicationWithParseErrors +@ diff --git a/testapps/ApplicationWithParseErrors/Views/Home/Index.cshtml b/testapps/ApplicationWithParseErrors/Views/Home/Index.cshtml new file mode 100644 index 0000000000..94ef1191a9 --- /dev/null +++ b/testapps/ApplicationWithParseErrors/Views/Home/Index.cshtml @@ -0,0 +1,4 @@ +@{ + +} + \ No newline at end of file