From 4994de10a813939f28337dfb2f3e3c15e3b738ce Mon Sep 17 00:00:00 2001 From: James Welle Date: Sat, 9 Feb 2019 14:25:32 -0700 Subject: [PATCH 1/2] CancellationTokenSource Dispose - properly dispose of the instance when finished --- src/CodeFormatter/Program.cs | 42 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/CodeFormatter/Program.cs b/src/CodeFormatter/Program.cs index 42a7a8a2..a73a3784 100644 --- a/src/CodeFormatter/Program.cs +++ b/src/CodeFormatter/Program.cs @@ -63,30 +63,32 @@ private static void RunListRules() private static int RunFormat(CommandLineOptions options) { - var cts = new CancellationTokenSource(); - var ct = cts.Token; + using (var cts = new CancellationTokenSource()) + { + var ct = cts.Token; - Console.CancelKeyPress += delegate { cts.Cancel(); }; + Console.CancelKeyPress += delegate { cts.Cancel(); }; - try - { - RunFormatAsync(options, ct).Wait(ct); - Console.WriteLine("Completed formatting."); - return 0; - } - catch (AggregateException ex) - { - var typeLoadException = ex.InnerExceptions.FirstOrDefault() as ReflectionTypeLoadException; - if (typeLoadException == null) - throw; + try + { + RunFormatAsync(options, ct).Wait(ct); + Console.WriteLine("Completed formatting."); + return 0; + } + catch (AggregateException ex) + { + var typeLoadException = ex.InnerExceptions.FirstOrDefault() as ReflectionTypeLoadException; + if (typeLoadException == null) + throw; - Console.WriteLine("ERROR: Type loading error detected. In order to run this tool you need either Visual Studio 2015 or Microsoft Build Tools 2015 tools installed."); - Console.WriteLine(typeLoadException.StackTrace); - var messages = typeLoadException.LoaderExceptions.Select(e => e.Message).Distinct(); - foreach (var message in messages) - Console.WriteLine("- {0}", message); + Console.WriteLine("ERROR: Type loading error detected. In order to run this tool you need either Visual Studio 2015 or Microsoft Build Tools 2015 tools installed."); + Console.WriteLine(typeLoadException.StackTrace); + var messages = typeLoadException.LoaderExceptions.Select(e => e.Message).Distinct(); + foreach (var message in messages) + Console.WriteLine("- {0}", message); - return 1; + return 1; + } } } From 3dc606837dfcd63866e1f21cc9707254ae769ddf Mon Sep 17 00:00:00 2001 From: James Welle Date: Sat, 9 Feb 2019 14:27:16 -0700 Subject: [PATCH 2/2] WorkspaceFailed - added a Workspace.WorkspaceFailed event handler so that if the input file cannot be processed, an error will be reported on the command line instead of failing silently --- src/CodeFormatter/Program.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/CodeFormatter/Program.cs b/src/CodeFormatter/Program.cs index a73a3784..d354683c 100644 --- a/src/CodeFormatter/Program.cs +++ b/src/CodeFormatter/Program.cs @@ -115,12 +115,12 @@ private static async Task RunFormatAsync(CommandLineOptions options, Cancel } private static async Task RunFormatItemAsync(IFormattingEngine engine, string item, string language, CancellationToken cancellationToken) - { + { Console.WriteLine(Path.GetFileName(item)); string extension = Path.GetExtension(item); if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".rsp")) { - using (var workspace = ResponseFileWorkspace.Create()) + using (var workspace = CreateWorkspace(ResponseFileWorkspace.Create)) { Project project = workspace.OpenCommandLineProject(item, language); await engine.FormatProjectAsync(project, cancellationToken); @@ -128,7 +128,7 @@ private static async Task RunFormatItemAsync(IFormattingEngine engine, string it } else if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln")) { - using (var workspace = MSBuildWorkspace.Create()) + using (var workspace = CreateWorkspace(MSBuildWorkspace.Create)) { workspace.LoadMetadataForReferencedProjects = true; var solution = await workspace.OpenSolutionAsync(item, cancellationToken); @@ -137,13 +137,20 @@ private static async Task RunFormatItemAsync(IFormattingEngine engine, string it } else { - using (var workspace = MSBuildWorkspace.Create()) + using (var workspace = CreateWorkspace(MSBuildWorkspace.Create)) { workspace.LoadMetadataForReferencedProjects = true; var project = await workspace.OpenProjectAsync(item, cancellationToken); await engine.FormatProjectAsync(project, cancellationToken); } } + + T CreateWorkspace(Func workspaceFunc) where T : Workspace + { + var workspace = workspaceFunc(); + workspace.WorkspaceFailed += (sender, args) => Console.WriteLine($"ERROR: {args.Diagnostic.Message}"); + return workspace; + } } private static bool SetRuleMap(IFormattingEngine engine, ImmutableDictionary ruleMap)