Skip to content

Commit 3091787

Browse files
authored
chore: Add msbuild diagnostic logging (#74)
This PR adds logging msbuild workspace diagnostics after the index operation finishes. This is important to figure out why a index command may not have found all the proper files. MSBuild logs diagnostics in two levels Failure and Warning. When MSBuild reports a Failure I am logging these as an Error since these will block indexing of that project. For example ``` fail: Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace[0] Failure: Msbuild failed when processing the file '/Users/mmanela/dev/me/diffplex/DiffPlex.App/DiffPlex.App.csproj' with message: /usr/local/share/dotnet/sdk/8.0.307/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets: (90, 5): To build a project targeting Windows on this operating system, set the EnableWindowsTargeting property to true. (occurred 1 times) ``` However, when MSBuild reports a warning, I am logging at the debug level since these are noisier and don't often relate to issues that are blocking indexing. For example ``` dbug: Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace[0] Warning: Found project reference without a matching metadata reference: /Users/mmanela/dev/me/diffplex/DiffPlex/DiffPlex.csproj (occurred 9 times) ```
1 parent 3caa711 commit 3091787

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ScipDotnet/IndexCommandHandler.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Diagnostics;
22
using Google.Protobuf;
33
using Microsoft.CodeAnalysis.Elfie.Extensions;
4+
using Microsoft.CodeAnalysis.MSBuild;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.FileSystemGlobbing;
67
using Microsoft.Extensions.Hosting;
@@ -49,6 +50,33 @@ public static async Task<int> Process(
4950
nugetConfigPath
5051
);
5152
await ScipIndex(host, options);
53+
54+
55+
// Log msbuild workspace diagnostic information after the index command finishes
56+
// We log the MSBuild failures as error since they are often blocking issues
57+
// preventing indexing. However, we log msbuild warnings as debug since they
58+
// do not block indexing usually and are much noisier
59+
var workspaceLogger = host.Services.GetRequiredService<ILogger<MSBuildWorkspace>>();
60+
var workspaceService = host.Services.GetRequiredService<MSBuildWorkspace>();
61+
if (workspaceService.Diagnostics.Any())
62+
{
63+
var diagnosticGroups = workspaceService.Diagnostics
64+
.GroupBy(d => new { d.Kind, d.Message })
65+
.Select(g => new { g.Key.Kind, g.Key.Message, Count = g.Count() });
66+
foreach (var diagnostic in diagnosticGroups)
67+
{
68+
var message = $"{diagnostic.Kind}: {diagnostic.Message} (occurred {diagnostic.Count} times)";
69+
if (diagnostic.Kind == Microsoft.CodeAnalysis.WorkspaceDiagnosticKind.Failure)
70+
{
71+
workspaceLogger.LogError(message);
72+
}
73+
else
74+
{
75+
workspaceLogger.LogDebug(message);
76+
}
77+
}
78+
}
79+
5280
return 0;
5381
}
5482

0 commit comments

Comments
 (0)