diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorDesignTimeCSharpLoweringPhase.cs b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorDesignTimeCSharpLoweringPhase.cs index aba0563eb2..8849039cbf 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorDesignTimeCSharpLoweringPhase.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorDesignTimeCSharpLoweringPhase.cs @@ -36,11 +36,13 @@ namespace Microsoft.AspNetCore.Razor.Evolution }; var visitor = new CSharpRenderer(renderingContext); visitor.VisitDocument(irDocument); + + var combinedErrors = syntaxTree.Diagnostics.Concat(renderingContext.ErrorSink.Errors).ToList(); var csharpDocument = new RazorCSharpDocument() { GeneratedCode = renderingContext.Writer.GenerateCode(), LineMappings = renderingContext.LineMappings, - Diagnostics = renderingContext.ErrorSink.Errors + Diagnostics = combinedErrors }; codeDocument.SetCSharpDocument(csharpDocument); diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorRuntimeCSharpLoweringPhase.cs b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorRuntimeCSharpLoweringPhase.cs index 3d983dbc13..2b5f9d5b03 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorRuntimeCSharpLoweringPhase.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorRuntimeCSharpLoweringPhase.cs @@ -37,11 +37,13 @@ namespace Microsoft.AspNetCore.Razor.Evolution var visitor = new CSharpRenderer(renderingContext); visitor.VisitDocument(irDocument); + + var combinedErrors = syntaxTree.Diagnostics.Concat(renderingContext.ErrorSink.Errors).ToList(); var csharpDocument = new RazorCSharpDocument() { GeneratedCode = renderingContext.Writer.GenerateCode(), LineMappings = renderingContext.LineMappings, - Diagnostics = renderingContext.ErrorSink.Errors + Diagnostics = combinedErrors }; codeDocument.SetCSharpDocument(csharpDocument); diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/BasicIntegrationTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/BasicIntegrationTest.cs index b7eca0a135..595334297e 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/BasicIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/BasicIntegrationTest.cs @@ -1,6 +1,7 @@ // 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.Evolution.Legacy; using Xunit; namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests @@ -70,5 +71,49 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests Assert.NotNull(document.GetSyntaxTree()); Assert.NotNull(document.GetIRDocument()); } + + [Fact] + public void CSharpDocument_Runtime_PreservesParserErrors() + { + // Arrange + var engine = RazorEngine.Create(); + + var document = RazorCodeDocument.Create(TestRazorSourceDocument.Create("@!!!")); + + // Act + engine.Process(document); + + // Assert + var csharpDocument = document.GetCSharpDocument(); + var error = Assert.Single(csharpDocument.Diagnostics); + Assert.Equal( + new RazorError( + LegacyResources.FormatParseError_Unexpected_Character_At_Start_Of_CodeBlock_CS("!"), + new SourceLocation(1, 0, 1), + length: 1), + error); + } + + [Fact] + public void CSharpDocument_DesignTime_PreservesParserErrors() + { + // Arrange + var engine = RazorEngine.CreateDesignTime(); + + var document = RazorCodeDocument.Create(TestRazorSourceDocument.Create("@{")); + + // Act + engine.Process(document); + + // Assert + var csharpDocument = document.GetCSharpDocument(); + var error = Assert.Single(csharpDocument.Diagnostics); + Assert.Equal( + new RazorError( + LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(LegacyResources.BlockName_Code, "}", "{"), + new SourceLocation(1, 0, 1), + length: 1), + error); + } } }