Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Workspace Failed #283

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions src/CodeFormatter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand All @@ -113,20 +115,20 @@ private static async Task<int> 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);
}
}
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);
Expand All @@ -135,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<T>(Func<T> 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<string, bool> ruleMap)
Expand Down