Fix up some error handling
- Don't flood console with compilation errors if the framework isn't installed. - Split errors into unspecified reference assembly base path and uninstalled framework.
This commit is contained in:
parent
537ba800f5
commit
41516f5a37
3 changed files with 70 additions and 28 deletions
|
@ -7,5 +7,8 @@ namespace Microsoft.Extensions.ProjectModel
|
||||||
{
|
{
|
||||||
// Target framework not installed
|
// Target framework not installed
|
||||||
public static readonly string DOTNET1011 = nameof(DOTNET1011);
|
public static readonly string DOTNET1011 = nameof(DOTNET1011);
|
||||||
|
|
||||||
|
// Reference assemblies location not specified
|
||||||
|
public static readonly string DOTNET1012 = nameof(DOTNET1012);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,20 +171,36 @@ namespace Microsoft.Extensions.ProjectModel
|
||||||
DiagnosticMessageSeverity.Warning));
|
DiagnosticMessageSeverity.Warning));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requiresFrameworkAssemblies && !frameworkReferenceResolver.IsInstalled(TargetFramework))
|
if (requiresFrameworkAssemblies)
|
||||||
{
|
{
|
||||||
var frameworkInfo = Project.GetTargetFramework(TargetFramework);
|
var frameworkInfo = Project.GetTargetFramework(TargetFramework);
|
||||||
|
|
||||||
// If there was an attempt to use reference assemblies but they were not installed
|
if (string.IsNullOrEmpty(ReferenceAssembliesPath))
|
||||||
// report an error
|
{
|
||||||
diagnostics.Add(new DiagnosticMessage(
|
// If there was an attempt to use reference assemblies but they were not installed
|
||||||
ErrorCodes.DOTNET1011,
|
// report an error
|
||||||
$"Framework not installed: {TargetFramework.DotNetFrameworkName}",
|
diagnostics.Add(new DiagnosticMessage(
|
||||||
filePath: Project.ProjectFilePath,
|
ErrorCodes.DOTNET1012,
|
||||||
severity: DiagnosticMessageSeverity.Error,
|
$"The reference assemblies directory was not specified. You can set the location using the DOTNET_REFERENCE_ASSEMBLIES_PATH environment variable.",
|
||||||
startLine: frameworkInfo.Line,
|
filePath: Project.ProjectFilePath,
|
||||||
startColumn: frameworkInfo.Column
|
severity: DiagnosticMessageSeverity.Error,
|
||||||
));
|
startLine: frameworkInfo.Line,
|
||||||
|
startColumn: frameworkInfo.Column
|
||||||
|
));
|
||||||
|
}
|
||||||
|
else if (!frameworkReferenceResolver.IsInstalled(TargetFramework))
|
||||||
|
{
|
||||||
|
// If there was an attempt to use reference assemblies but they were not installed
|
||||||
|
// report an error
|
||||||
|
diagnostics.Add(new DiagnosticMessage(
|
||||||
|
ErrorCodes.DOTNET1011,
|
||||||
|
$"Framework not installed: {TargetFramework.DotNetFrameworkName} in {ReferenceAssembliesPath}",
|
||||||
|
filePath: Project.ProjectFilePath,
|
||||||
|
severity: DiagnosticMessageSeverity.Error,
|
||||||
|
startLine: frameworkInfo.Line,
|
||||||
|
startColumn: frameworkInfo.Column
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a library manager
|
// Create a library manager
|
||||||
|
|
|
@ -108,11 +108,6 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
// Create the library exporter
|
// Create the library exporter
|
||||||
var exporter = context.CreateExporter(configuration);
|
var exporter = context.CreateExporter(configuration);
|
||||||
|
|
||||||
var diagnostics = new List<DiagnosticMessage>();
|
|
||||||
|
|
||||||
// Collect dependency diagnostics
|
|
||||||
diagnostics.AddRange(context.LibraryManager.GetAllDiagnostics());
|
|
||||||
|
|
||||||
// Gather exports for the project
|
// Gather exports for the project
|
||||||
var dependencies = exporter.GetDependencies().ToList();
|
var dependencies = exporter.GetDependencies().ToList();
|
||||||
|
|
||||||
|
@ -151,6 +146,29 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
Reporter.Output.WriteLine($"Compiling {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}");
|
Reporter.Output.WriteLine($"Compiling {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}");
|
||||||
var sw = Stopwatch.StartNew();
|
var sw = Stopwatch.StartNew();
|
||||||
|
|
||||||
|
var diagnostics = new List<DiagnosticMessage>();
|
||||||
|
var missingFrameworkDiagnostics = new List<DiagnosticMessage>();
|
||||||
|
|
||||||
|
// Collect dependency diagnostics
|
||||||
|
foreach (var diag in context.LibraryManager.GetAllDiagnostics())
|
||||||
|
{
|
||||||
|
if (diag.ErrorCode == ErrorCodes.DOTNET1011 ||
|
||||||
|
diag.ErrorCode == ErrorCodes.DOTNET1012)
|
||||||
|
{
|
||||||
|
missingFrameworkDiagnostics.Add(diag);
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnostics.Add(diag);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (missingFrameworkDiagnostics.Count > 0)
|
||||||
|
{
|
||||||
|
// The framework isn't installed so we should short circuit the rest of the compilation
|
||||||
|
// so we don't get flooded with errors
|
||||||
|
PrintSummary(missingFrameworkDiagnostics, sw);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Dump dependency data
|
// Dump dependency data
|
||||||
// TODO: Turn on only if verbose, we can look at the response
|
// TODO: Turn on only if verbose, we can look at the response
|
||||||
// file anyways
|
// file anyways
|
||||||
|
@ -240,11 +258,6 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
})
|
})
|
||||||
.Execute();
|
.Execute();
|
||||||
|
|
||||||
foreach (var diag in diagnostics)
|
|
||||||
{
|
|
||||||
PrintDiagnostic(diag);
|
|
||||||
}
|
|
||||||
|
|
||||||
var success = result.ExitCode == 0;
|
var success = result.ExitCode == 0;
|
||||||
|
|
||||||
if (success && compilationOptions.EmitEntryPoint.GetValueOrDefault())
|
if (success && compilationOptions.EmitEntryPoint.GetValueOrDefault())
|
||||||
|
@ -255,10 +268,7 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
runtimeContext.CreateExporter(configuration));
|
runtimeContext.CreateExporter(configuration));
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintSummary(success, diagnostics);
|
PrintSummary(diagnostics, sw, success);
|
||||||
|
|
||||||
Reporter.Output.WriteLine($"Time elapsed {sw.Elapsed}");
|
|
||||||
Reporter.Output.WriteLine();
|
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
@ -423,14 +433,16 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
return "\"" + input.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
|
return "\"" + input.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void PrintSummary(bool success, List<DiagnosticMessage> diagnostics)
|
private static void PrintSummary(List<DiagnosticMessage> diagnostics, Stopwatch sw, bool success = true)
|
||||||
{
|
{
|
||||||
|
PrintDiagnostics(diagnostics);
|
||||||
|
|
||||||
Reporter.Output.WriteLine();
|
Reporter.Output.WriteLine();
|
||||||
|
|
||||||
var errorCount = diagnostics.Count(d => d.Severity == DiagnosticMessageSeverity.Error);
|
var errorCount = diagnostics.Count(d => d.Severity == DiagnosticMessageSeverity.Error);
|
||||||
var warningCount = diagnostics.Count(d => d.Severity == DiagnosticMessageSeverity.Warning);
|
var warningCount = diagnostics.Count(d => d.Severity == DiagnosticMessageSeverity.Warning);
|
||||||
|
|
||||||
if (errorCount > 0)
|
if (errorCount > 0 || !success)
|
||||||
{
|
{
|
||||||
Reporter.Output.WriteLine("Compilation failed.".Red());
|
Reporter.Output.WriteLine("Compilation failed.".Red());
|
||||||
}
|
}
|
||||||
|
@ -443,6 +455,9 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
Reporter.Output.WriteLine($" {errorCount} Error(s)");
|
Reporter.Output.WriteLine($" {errorCount} Error(s)");
|
||||||
|
|
||||||
Reporter.Output.WriteLine();
|
Reporter.Output.WriteLine();
|
||||||
|
|
||||||
|
Reporter.Output.WriteLine($"Time elapsed {sw.Elapsed}");
|
||||||
|
Reporter.Output.WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool AddResources(Project project, List<string> compilerArgs, string intermediateOutputPath)
|
private static bool AddResources(Project project, List<string> compilerArgs, string intermediateOutputPath)
|
||||||
|
@ -553,6 +568,14 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void PrintDiagnostics(List<DiagnosticMessage> diagnostics)
|
||||||
|
{
|
||||||
|
foreach (var diag in diagnostics)
|
||||||
|
{
|
||||||
|
PrintDiagnostic(diag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void PrintDiagnostic(DiagnosticMessage diag)
|
private static void PrintDiagnostic(DiagnosticMessage diag)
|
||||||
{
|
{
|
||||||
switch (diag.Severity)
|
switch (diag.Severity)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue