diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationFailedException.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationFailedException.cs index f4ca0b7f20..23a597d0d9 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationFailedException.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationFailedException.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; -using Microsoft.Extensions.CompilationAbstractions; +using Microsoft.AspNet.Diagnostics; namespace Microsoft.AspNet.Mvc.Razor.Compilation { diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationResult.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationResult.cs index 16318d9b80..65bf7bf5fc 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationResult.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationResult.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; -using Microsoft.Extensions.CompilationAbstractions; +using Microsoft.AspNet.Diagnostics; namespace Microsoft.AspNet.Mvc.Razor.Compilation { diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RazorCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RazorCompilationService.cs index 92cadaa155..d231ef64e8 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RazorCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RazorCompilationService.cs @@ -5,11 +5,10 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using Microsoft.AspNet.Diagnostics; using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.Razor; using Microsoft.AspNet.Razor.CodeGenerators; -using Microsoft.Extensions.CompilationAbstractions; -using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.OptionsModel; namespace Microsoft.AspNet.Mvc.Razor.Compilation @@ -96,7 +95,8 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation var compilationFailure = new CompilationFailure( filePath, fileContent, - group.Select(parserError => CreateDiagnosticMessage(parserError, filePath))); + compiledContent: string.Empty, + messages: group.Select(parserError => CreateDiagnosticMessage(parserError, filePath))); failures.Add(compilationFailure); } @@ -106,11 +106,9 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation private DiagnosticMessage CreateDiagnosticMessage(RazorError error, string filePath) { return new DiagnosticMessage( - errorCode: null, message: error.Message, formattedMessage: $"{error} ({error.Location.LineIndex},{error.Location.CharacterIndex}) {error.Message}", filePath: filePath, - severity: DiagnosticMessageSeverity.Error, startLine: error.Location.LineIndex + 1, startColumn: error.Location.CharacterIndex, endLine: error.Location.LineIndex + 1, diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs index 76fb3eb28b..ecebb4a185 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs @@ -12,15 +12,16 @@ using System.Reflection.PortableExecutable; #if DOTNET5_5 using System.Runtime.Loader; #endif +using System.Runtime.Versioning; using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.Mvc.Razor.Internal; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Emit; using Microsoft.Dnx.Compilation.CSharp; -using Microsoft.Extensions.CompilationAbstractions; using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.OptionsModel; +using Microsoft.AspNet.Diagnostics; namespace Microsoft.AspNet.Mvc.Razor.Compilation { @@ -33,7 +34,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation private readonly ConcurrentDictionary _metadataFileCache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); - private readonly ILibraryExporter _libraryExporter; + private readonly Extensions.CompilationAbstractions.ILibraryExporter _libraryExporter; private readonly IApplicationEnvironment _environment; private readonly IFileProvider _fileProvider; private readonly Lazy> _applicationReferences; @@ -53,14 +54,14 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation /// /// The accessor for the used to load compiled assemblies. /// - /// The library manager that provides export and reference information. + /// The library manager that provides export and reference information. /// /// The that provides Roslyn compilation settings. /// /// The that was used to generate the code. public RoslynCompilationService( IApplicationEnvironment environment, - ILibraryExporter libraryExporter, + Extensions.CompilationAbstractions.ILibraryExporter libraryExporter, IMvcRazorHost host, IOptions optionsAccessor) { @@ -73,7 +74,6 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation _parseOptions = optionsAccessor.Value.ParseOptions; _compilationOptions = optionsAccessor.Value.CompilationOptions; - #if DOTNET5_5 _razorLoadContext = new RazorLoadContext(); #endif @@ -213,7 +213,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation sourceFilePath, sourceFileContent, compilationContent, - group.Select(d => d.ToDiagnosticMessage(_environment.RuntimeFramework))); + group.Select(diagnostic => GetDiagnosticMessage(diagnostic, _environment.RuntimeFramework))); failures.Add(compilationFailure); } @@ -269,7 +269,8 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation return references; } - private MetadataReference ConvertMetadataReference(IMetadataReference metadataReference) + private MetadataReference ConvertMetadataReference( + Extensions.CompilationAbstractions.IMetadataReference metadataReference) { var roslynReference = metadataReference as IRoslynMetadataReference; @@ -278,21 +279,21 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation return roslynReference.MetadataReference; } - var embeddedReference = metadataReference as IMetadataEmbeddedReference; + var embeddedReference = metadataReference as Extensions.CompilationAbstractions.IMetadataEmbeddedReference; if (embeddedReference != null) { return MetadataReference.CreateFromImage(embeddedReference.Contents); } - var fileMetadataReference = metadataReference as IMetadataFileReference; + var fileMetadataReference = metadataReference as Extensions.CompilationAbstractions.IMetadataFileReference; if (fileMetadataReference != null) { return CreateMetadataFileReference(fileMetadataReference.Path); } - var projectReference = metadataReference as IMetadataProjectReference; + var projectReference = metadataReference as Extensions.CompilationAbstractions.IMetadataProjectReference; if (projectReference != null) { using (var ms = new MemoryStream()) @@ -346,6 +347,19 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation return null; } + private static DiagnosticMessage GetDiagnosticMessage(Diagnostic diagnostic, FrameworkName targetFramework) + { + var mappedLineSpan = diagnostic.Location.GetMappedLineSpan(); + return new DiagnosticMessage( + diagnostic.GetMessage(), + RoslynDiagnosticFormatter.Format(diagnostic, targetFramework), + mappedLineSpan.Path, + mappedLineSpan.StartLinePosition.Line + 1, + mappedLineSpan.StartLinePosition.Character + 1, + mappedLineSpan.EndLinePosition.Line + 1, + mappedLineSpan.EndLinePosition.Character + 1); + } + #if DOTNET5_5 private class RazorLoadContext : AssemblyLoadContext { diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/CompilationResultTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/CompilationResultTest.cs index 41ffba39db..665cf84ef4 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/CompilationResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/CompilationResultTest.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Linq; -using Microsoft.Extensions.CompilationAbstractions; +using Microsoft.AspNet.Diagnostics; using Xunit; namespace Microsoft.AspNet.Mvc.Razor.Compilation @@ -13,7 +13,11 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation public void EnsureSuccessful_ThrowsIfCompilationFailed() { // Arrange - var compilationFailure = new CompilationFailure("test", Enumerable.Empty()); + var compilationFailure = new CompilationFailure( + "test", + sourceFileContent: string.Empty, + compiledContent: string.Empty, + messages: Enumerable.Empty()); var failures = new[] { compilationFailure }; var result = new CompilationResult(failures);