public static CompilationFailedException Create( RazorCodeDocument codeDocument, IEnumerable <RazorDiagnostic> diagnostics) { // If a SourceLocation does not specify a file path, assume it is produced from parsing the current file. var messageGroups = diagnostics.GroupBy( razorError => razorError.Span.FilePath ?? codeDocument.Source.FilePath, StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in messageGroups) { var filePath = group.Key; var fileContent = ReadContent(codeDocument, filePath); var compilationFailure = new CompilationFailure( filePath, fileContent, compiledContent: string.Empty, messages: group.Select(parserError => CreateDiagnosticMessage(parserError, filePath))); failures.Add(compilationFailure); } return(new CompilationFailedException(failures)); }
// Internal for unit testing internal CompilationResult GetCompilationFailedResult(RelativeFileInfo file, IEnumerable <RazorError> errors) { // If a SourceLocation does not specify a file path, assume it is produced // from parsing the current file. var messageGroups = errors .GroupBy(razorError => razorError.Location.FilePath ?? file.RelativePath, StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in messageGroups) { var filePath = group.Key; var fileContent = ReadFileContentsSafely(filePath); var compilationFailure = new CompilationFailure( filePath, fileContent, compiledContent: string.Empty, messages: group.Select(parserError => CreateDiagnosticMessage(parserError, filePath))); failures.Add(compilationFailure); } return(new CompilationResult(failures)); }
// Internal for unit testing internal CompilationResult GetCompilationFailedResult( string relativePath, string compilationContent, string assemblyName, IEnumerable <Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(IsError) .GroupBy(diagnostic => GetFilePath(relativePath, diagnostic), StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { // The error is in the generated code and does not have a mapping line pragma sourceFileContent = compilationContent; sourceFilePath = "Generated Code"; } else { sourceFileContent = ReadFileContentsSafely(_fileProvider, sourceFilePath); } var compilationFailure = new CompilationFailure(sourceFilePath, sourceFileContent, compilationContent, group.Select(d => d.ToDiagnosticMessage(_environment.RuntimeFramework))); failures.Add(compilationFailure); } return(CompilationResult.Failed(failures)); }
// Internal for unit testing internal CompilationResult GetCompilationFailedResult( string relativePath, string compilationContent, string assemblyName, IEnumerable <Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(IsError) .GroupBy(diagnostic => GetFilePath(relativePath, diagnostic), StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { // The error is in the generated code and does not have a mapping line pragma sourceFileContent = compilationContent; sourceFilePath = Resources.GeneratedCodeFileName; } else { sourceFileContent = ReadFileContentsSafely(_fileProvider, sourceFilePath); } string additionalMessage = null; if (group.Any(g => string.Equals(CS0234, g.Id, StringComparison.OrdinalIgnoreCase) || string.Equals(CS0246, g.Id, StringComparison.OrdinalIgnoreCase))) { additionalMessage = Resources.FormatCompilation_DependencyContextIsNotSpecified( "preserveCompilationContext", "buildOptions", "project.json"); } var compilationFailure = new CompilationFailure( sourceFilePath, sourceFileContent, compilationContent, group.Select(GetDiagnosticMessage), additionalMessage); failures.Add(compilationFailure); } return(new CompilationResult(failures)); }
public static CompilationFailedException Create( RazorCodeDocument codeDocument, string compilationContent, string assemblyName, IEnumerable <Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error) .GroupBy(diagnostic => GetFilePath(codeDocument, diagnostic), StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { // The error is in the generated code and does not have a mapping line pragma sourceFileContent = compilationContent; sourceFilePath = Resources.GeneratedCodeFileName; } else { sourceFileContent = ReadContent(codeDocument, sourceFilePath); } string additionalMessage = null; if (group.Any(g => string.Equals(CS0234, g.Id, StringComparison.OrdinalIgnoreCase) || string.Equals(CS0246, g.Id, StringComparison.OrdinalIgnoreCase))) { additionalMessage = string.Format(Resources.Compilation_MissingReferences, "CopyRefAssembliesToPublishDirectory"); // Resources Custom Tool switched to ResXFileCodeGenerator, MSBuild:_GenerateResxSource is not working without Microsoft Arcade SDK //additionalMessage = Resources.FormatCompilation_MissingReferences("CopyRefAssembliesToPublishDirectory"); } var compilationFailure = new CompilationFailure( sourceFilePath, sourceFileContent, compilationContent, group.Select(GetDiagnosticMessage), additionalMessage); failures.Add(compilationFailure); } return(new CompilationFailedException(failures)); }
public void EnsureSuccessful_ThrowsIfCompilationFailed() { // Arrange var compilationFailure = new CompilationFailure("test", Enumerable.Empty <Dnx.Runtime.DiagnosticMessage>()); var failures = new[] { compilationFailure }; var result = CompilationResult.Failed(failures); // Act and Assert Assert.Null(result.CompiledType); Assert.Same(failures, result.CompilationFailures); var exception = Assert.Throws <CompilationFailedException>(() => result.EnsureSuccessful()); var failure = Assert.Single(exception.CompilationFailures); Assert.Same(compilationFailure, failure); }
public static CompilationFailedException Create( RazorCodeDocument codeDocument, string compilationContent, string assemblyName, IEnumerable<Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error) .GroupBy(diagnostic => GetFilePath(codeDocument, diagnostic), StringComparer.Ordinal); var failures = new List<CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { // The error is in the generated code and does not have a mapping line pragma sourceFileContent = compilationContent; sourceFilePath = Resources.GeneratedCodeFileName; } else { sourceFileContent = ReadContent(codeDocument, sourceFilePath); } string additionalMessage = null; if (group.Any(g => string.Equals(CS0234, g.Id, StringComparison.OrdinalIgnoreCase) || string.Equals(CS0246, g.Id, StringComparison.OrdinalIgnoreCase))) { additionalMessage = Resources.FormatCompilation_DependencyContextIsNotSpecified( "Microsoft.NET.Sdk.Web", "PreserveCompilationContext"); } var compilationFailure = new CompilationFailure( sourceFilePath, sourceFileContent, compilationContent, group.Select(GetDiagnosticMessage), additionalMessage); failures.Add(compilationFailure); } return new CompilationFailedException(failures); }
public static CustomCompilationFailedException Create( RazorCodeDocument codeDocument, string compilationContent, string assemblyName, IEnumerable <Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error) .GroupBy(diagnostic => GetFilePath(codeDocument, diagnostic), StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { // The error is in the generated code and does not have a mapping line pragma sourceFileContent = compilationContent; sourceFilePath = "Generated Code"; } else { sourceFileContent = ReadContent(codeDocument, sourceFilePath); } string additionalMessage = null; if (group.Any(g => string.Equals(CS0234, g.Id, StringComparison.OrdinalIgnoreCase) || string.Equals(CS0246, g.Id, StringComparison.OrdinalIgnoreCase))) { additionalMessage = string.Format("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.", "CopyRefAssembliesToPublishDirectory"); } var compilationFailure = new CompilationFailure( sourceFilePath, sourceFileContent, compilationContent, group.Select(GetDiagnosticMessage), additionalMessage); failures.Add(compilationFailure); } return(new CustomCompilationFailedException(failures)); }
public static CompilationFailedException Create( RazorCodeDocument codeDocument, string compilationContent, string assemblyName, IEnumerable <Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error) .GroupBy(diagnostic => GetFilePath(codeDocument, diagnostic), StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { sourceFileContent = compilationContent; sourceFilePath = "Generated Code"; } else { sourceFileContent = ReadContent(codeDocument, sourceFilePath); } string additionalMessage = null; if (group.Any(g => string.Equals(CS0234, g.Id, StringComparison.OrdinalIgnoreCase) || string.Equals(CS0246, g.Id, StringComparison.OrdinalIgnoreCase))) { additionalMessage = "Dependency context not specified: Microsoft.NET.Sdk.Web, PreserveCompilationContext"; } var compilationFailure = new CompilationFailure( sourceFilePath, sourceFileContent, compilationContent, group.Select(GetDiagnosticMessage), additionalMessage); failures.Add(compilationFailure); } return(new CompilationFailedException(failures)); }
public void EnsureSuccessful_ThrowsIfCompilationFailed() { // Arrange var compilationFailure = new CompilationFailure( "test", sourceFileContent: string.Empty, compiledContent: string.Empty, messages: Enumerable.Empty<AspNet.Diagnostics.DiagnosticMessage>()); var failures = new[] { compilationFailure }; var result = new CompilationResult(failures); // Act and Assert Assert.Null(result.CompiledType); Assert.Same(failures, result.CompilationFailures); var exception = Assert.Throws<CompilationFailedException>(() => result.EnsureSuccessful()); var failure = Assert.Single(exception.CompilationFailures); Assert.Same(compilationFailure, failure); }
public void EnsureSuccessful_ThrowsIfCompilationFailed() { // Arrange var compilationFailure = new CompilationFailure( "test", sourceFileContent: string.Empty, compiledContent: string.Empty, messages: Enumerable.Empty <AspNet.Diagnostics.DiagnosticMessage>()); var failures = new[] { compilationFailure }; var result = new CompilationResult(failures); // Act and Assert Assert.Null(result.CompiledType); Assert.Same(failures, result.CompilationFailures); var exception = Assert.Throws <CompilationFailedException>(() => result.EnsureSuccessful()); var failure = Assert.Single(exception.CompilationFailures); Assert.Same(compilationFailure, failure); }
private CompilationException CreateException(RazorCodeDocument document) { var groups = document.ErrorSink.Errors.GroupBy(e => e.Location.FilePath, StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in groups) { var filePath = group.Key; var fileContent = document.Source.CreateReader().ReadToEnd(); var compilationFailure = new CompilationFailure( filePath, fileContent, compiledContent: string.Empty, messages: group.Select(e => e.ToDiagnosticMessage())); failures.Add(compilationFailure); } throw new CompilationException(failures); }
private CompilationException CreateException( RazorCodeDocument document, string relativePath, string generatedCode, string assemblyName, IEnumerable <Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(IsError) .GroupBy(diagnostic => GetFilePath(relativePath, diagnostic), StringComparer.Ordinal); var source = document.Source.CreateReader().ReadToEnd(); var failures = new List <CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { // The error is in the generated code and does not have a mapping line pragma sourceFileContent = source; sourceFilePath = "who cares"; } var failure = new CompilationFailure( sourceFilePath, source, generatedCode, group.Select(d => d.ToDiagnosticMessage())); failures.Add(failure); } throw new CompilationException(failures); }
private static void BuildCodeSnippetDiv(CompilationFailure failure, StringBuilder builder, ref int totalErrorsShown) { const int NumContextLines = 3; var fileName = failure.SourceFilePath; if (totalErrorsShown < MaxCompilationErrorsToShow && !string.IsNullOrEmpty(fileName)) { builder.Append(@"<div class=""codeSnippet"">") .AppendFormat(@"<div class=""filename""><code>{0}</code></div>", HtmlEncodeAndReplaceLineBreaks(fileName)) .AppendLine(); IEnumerable <string> fileContent; if (string.IsNullOrEmpty(failure.SourceFileContent)) { fileContent = File.ReadLines(fileName); } else { fileContent = failure.SourceFileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None); } foreach (var message in failure.Messages) { if (totalErrorsShown++ > MaxCompilationErrorsToShow) { break; } if (totalErrorsShown > 1) { builder.AppendLine("<br />"); } builder.Append(@"<div class=""error-message"">") .Append(HtmlEncodeAndReplaceLineBreaks(message.Message)) .Append("</div>"); // StartLine and EndLine are 1-based var startLine = message.StartLine - 1; var endLine = message.EndLine - 1; var preContextIndex = Math.Max(startLine - NumContextLines, 0); var index = preContextIndex + 1; foreach (var line in fileContent.Skip(preContextIndex).Take(startLine - preContextIndex)) { builder.Append(@"<div class=""line faded"">") .AppendFormat(@"<span class=""line-number"">{0}</span><code>", index++) .Append(HtmlEncodeAndReplaceLineBreaks(line)) .AppendLine("</code></div>"); } var numErrorLines = 1 + Math.Max(0, endLine - startLine); foreach (var line in fileContent.Skip(startLine).Take(numErrorLines)) { builder.Append(@"<div class=""line error"">") .AppendFormat(@"<span class=""line-number"">{0}</span><code>", index++) .Append(HtmlEncodeAndReplaceLineBreaks(line)) .AppendLine("</code></div>"); } foreach (var line in fileContent.Skip(message.EndLine).Take(NumContextLines)) { builder.Append(@"<div class=""line faded"">") .AppendFormat(@"<span class=""line-number"">{0}</span><code>", index++) .Append(HtmlEncodeAndReplaceLineBreaks(line)) .AppendLine("</code></div>"); } } builder.AppendLine("</div>"); // Close codeSnippet div } }
// Internal for unit testing internal CompilationResult GetCompilationFailedResult( string relativePath, string compilationContent, string assemblyName, IEnumerable<Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(IsError) .GroupBy(diagnostic => GetFilePath(relativePath, diagnostic), StringComparer.Ordinal); var failures = new List<CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { // The error is in the generated code and does not have a mapping line pragma sourceFileContent = compilationContent; sourceFilePath = Resources.GeneratedCodeFileName; } else { sourceFileContent = ReadFileContentsSafely(_fileProvider, sourceFilePath); } var compilationFailure = new CompilationFailure( sourceFilePath, sourceFileContent, compilationContent, group.Select(diagnostic => GetDiagnosticMessage(diagnostic, _environment.RuntimeFramework))); failures.Add(compilationFailure); } return new CompilationResult(failures); }
// Internal for unit testing internal CompilationResult GetCompilationFailedResult(RelativeFileInfo file, IEnumerable<RazorError> errors) { // If a SourceLocation does not specify a file path, assume it is produced // from parsing the current file. var messageGroups = errors .GroupBy(razorError => razorError.Location.FilePath ?? file.RelativePath, StringComparer.Ordinal); var failures = new List<CompilationFailure>(); foreach (var group in messageGroups) { var filePath = group.Key; var fileContent = ReadFileContentsSafely(filePath); var compilationFailure = new CompilationFailure( filePath, fileContent, compiledContent: string.Empty, messages: group.Select(parserError => CreateDiagnosticMessage(parserError, filePath))); failures.Add(compilationFailure); } return new CompilationResult(failures); }