Reacting to Diagnostics changes

This commit is contained in:
Pranav K 2015-12-07 17:29:41 -08:00
parent 6467d0d475
commit 218613bc0a
5 changed files with 35 additions and 19 deletions

View File

@ -4,7 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.Extensions.CompilationAbstractions; using Microsoft.AspNet.Diagnostics;
namespace Microsoft.AspNet.Mvc.Razor.Compilation namespace Microsoft.AspNet.Mvc.Razor.Compilation
{ {

View File

@ -3,7 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.CompilationAbstractions; using Microsoft.AspNet.Diagnostics;
namespace Microsoft.AspNet.Mvc.Razor.Compilation namespace Microsoft.AspNet.Mvc.Razor.Compilation
{ {

View File

@ -5,11 +5,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Razor; using Microsoft.AspNet.Razor;
using Microsoft.AspNet.Razor.CodeGenerators; using Microsoft.AspNet.Razor.CodeGenerators;
using Microsoft.Extensions.CompilationAbstractions;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.OptionsModel; using Microsoft.Extensions.OptionsModel;
namespace Microsoft.AspNet.Mvc.Razor.Compilation namespace Microsoft.AspNet.Mvc.Razor.Compilation
@ -96,7 +95,8 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
var compilationFailure = new CompilationFailure( var compilationFailure = new CompilationFailure(
filePath, filePath,
fileContent, fileContent,
group.Select(parserError => CreateDiagnosticMessage(parserError, filePath))); compiledContent: string.Empty,
messages: group.Select(parserError => CreateDiagnosticMessage(parserError, filePath)));
failures.Add(compilationFailure); failures.Add(compilationFailure);
} }
@ -106,11 +106,9 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
private DiagnosticMessage CreateDiagnosticMessage(RazorError error, string filePath) private DiagnosticMessage CreateDiagnosticMessage(RazorError error, string filePath)
{ {
return new DiagnosticMessage( return new DiagnosticMessage(
errorCode: null,
message: error.Message, message: error.Message,
formattedMessage: $"{error} ({error.Location.LineIndex},{error.Location.CharacterIndex}) {error.Message}", formattedMessage: $"{error} ({error.Location.LineIndex},{error.Location.CharacterIndex}) {error.Message}",
filePath: filePath, filePath: filePath,
severity: DiagnosticMessageSeverity.Error,
startLine: error.Location.LineIndex + 1, startLine: error.Location.LineIndex + 1,
startColumn: error.Location.CharacterIndex, startColumn: error.Location.CharacterIndex,
endLine: error.Location.LineIndex + 1, endLine: error.Location.LineIndex + 1,

View File

@ -12,15 +12,16 @@ using System.Reflection.PortableExecutable;
#if DOTNET5_5 #if DOTNET5_5
using System.Runtime.Loader; using System.Runtime.Loader;
#endif #endif
using System.Runtime.Versioning;
using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Mvc.Razor.Internal; using Microsoft.AspNet.Mvc.Razor.Internal;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit; using Microsoft.CodeAnalysis.Emit;
using Microsoft.Dnx.Compilation.CSharp; using Microsoft.Dnx.Compilation.CSharp;
using Microsoft.Extensions.CompilationAbstractions;
using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.OptionsModel; using Microsoft.Extensions.OptionsModel;
using Microsoft.AspNet.Diagnostics;
namespace Microsoft.AspNet.Mvc.Razor.Compilation namespace Microsoft.AspNet.Mvc.Razor.Compilation
{ {
@ -33,7 +34,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
private readonly ConcurrentDictionary<string, AssemblyMetadata> _metadataFileCache = private readonly ConcurrentDictionary<string, AssemblyMetadata> _metadataFileCache =
new ConcurrentDictionary<string, AssemblyMetadata>(StringComparer.OrdinalIgnoreCase); new ConcurrentDictionary<string, AssemblyMetadata>(StringComparer.OrdinalIgnoreCase);
private readonly ILibraryExporter _libraryExporter; private readonly Extensions.CompilationAbstractions.ILibraryExporter _libraryExporter;
private readonly IApplicationEnvironment _environment; private readonly IApplicationEnvironment _environment;
private readonly IFileProvider _fileProvider; private readonly IFileProvider _fileProvider;
private readonly Lazy<List<MetadataReference>> _applicationReferences; private readonly Lazy<List<MetadataReference>> _applicationReferences;
@ -53,14 +54,14 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
/// <param name="loaderAccessor"> /// <param name="loaderAccessor">
/// The accessor for the <see cref="IAssemblyLoadContext"/> used to load compiled assemblies. /// The accessor for the <see cref="IAssemblyLoadContext"/> used to load compiled assemblies.
/// </param> /// </param>
/// <param name="libraryManager">The library manager that provides export and reference information.</param> /// <param name="libraryExporter">The library manager that provides export and reference information.</param>
/// <param name="compilerOptionsProvider"> /// <param name="compilerOptionsProvider">
/// The <see cref="ICompilerOptionsProvider"/> that provides Roslyn compilation settings. /// The <see cref="ICompilerOptionsProvider"/> that provides Roslyn compilation settings.
/// </param> /// </param>
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param> /// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
public RoslynCompilationService( public RoslynCompilationService(
IApplicationEnvironment environment, IApplicationEnvironment environment,
ILibraryExporter libraryExporter, Extensions.CompilationAbstractions.ILibraryExporter libraryExporter,
IMvcRazorHost host, IMvcRazorHost host,
IOptions<RazorViewEngineOptions> optionsAccessor) IOptions<RazorViewEngineOptions> optionsAccessor)
{ {
@ -73,7 +74,6 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
_parseOptions = optionsAccessor.Value.ParseOptions; _parseOptions = optionsAccessor.Value.ParseOptions;
_compilationOptions = optionsAccessor.Value.CompilationOptions; _compilationOptions = optionsAccessor.Value.CompilationOptions;
#if DOTNET5_5 #if DOTNET5_5
_razorLoadContext = new RazorLoadContext(); _razorLoadContext = new RazorLoadContext();
#endif #endif
@ -213,7 +213,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
sourceFilePath, sourceFilePath,
sourceFileContent, sourceFileContent,
compilationContent, compilationContent,
group.Select(d => d.ToDiagnosticMessage(_environment.RuntimeFramework))); group.Select(diagnostic => GetDiagnosticMessage(diagnostic, _environment.RuntimeFramework)));
failures.Add(compilationFailure); failures.Add(compilationFailure);
} }
@ -269,7 +269,8 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
return references; return references;
} }
private MetadataReference ConvertMetadataReference(IMetadataReference metadataReference) private MetadataReference ConvertMetadataReference(
Extensions.CompilationAbstractions.IMetadataReference metadataReference)
{ {
var roslynReference = metadataReference as IRoslynMetadataReference; var roslynReference = metadataReference as IRoslynMetadataReference;
@ -278,21 +279,21 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
return roslynReference.MetadataReference; return roslynReference.MetadataReference;
} }
var embeddedReference = metadataReference as IMetadataEmbeddedReference; var embeddedReference = metadataReference as Extensions.CompilationAbstractions.IMetadataEmbeddedReference;
if (embeddedReference != null) if (embeddedReference != null)
{ {
return MetadataReference.CreateFromImage(embeddedReference.Contents); return MetadataReference.CreateFromImage(embeddedReference.Contents);
} }
var fileMetadataReference = metadataReference as IMetadataFileReference; var fileMetadataReference = metadataReference as Extensions.CompilationAbstractions.IMetadataFileReference;
if (fileMetadataReference != null) if (fileMetadataReference != null)
{ {
return CreateMetadataFileReference(fileMetadataReference.Path); return CreateMetadataFileReference(fileMetadataReference.Path);
} }
var projectReference = metadataReference as IMetadataProjectReference; var projectReference = metadataReference as Extensions.CompilationAbstractions.IMetadataProjectReference;
if (projectReference != null) if (projectReference != null)
{ {
using (var ms = new MemoryStream()) using (var ms = new MemoryStream())
@ -346,6 +347,19 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
return null; 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 #if DOTNET5_5
private class RazorLoadContext : AssemblyLoadContext private class RazorLoadContext : AssemblyLoadContext
{ {

View File

@ -2,7 +2,7 @@
// 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.Linq; using System.Linq;
using Microsoft.Extensions.CompilationAbstractions; using Microsoft.AspNet.Diagnostics;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.Razor.Compilation namespace Microsoft.AspNet.Mvc.Razor.Compilation
@ -13,7 +13,11 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
public void EnsureSuccessful_ThrowsIfCompilationFailed() public void EnsureSuccessful_ThrowsIfCompilationFailed()
{ {
// Arrange // Arrange
var compilationFailure = new CompilationFailure("test", Enumerable.Empty<Microsoft.Extensions.CompilationAbstractions.DiagnosticMessage>()); var compilationFailure = new CompilationFailure(
"test",
sourceFileContent: string.Empty,
compiledContent: string.Empty,
messages: Enumerable.Empty<AspNet.Diagnostics.DiagnosticMessage>());
var failures = new[] { compilationFailure }; var failures = new[] { compilationFailure };
var result = new CompilationResult(failures); var result = new CompilationResult(failures);