Skip to content

Commit

Permalink
Report workspace messages for warnings/errors from the build hosts
Browse files Browse the repository at this point in the history
This connects our various error reporting systems so failures of the
build hosts can get passed along back to the existing reporting
mechanism for MSBuildWorkspace failures.
  • Loading branch information
jasonmalinowski committed Feb 1, 2024
1 parent 27e5f16 commit 56b4868
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.Contracts;
using Microsoft.Extensions.Logging;

namespace Microsoft.CodeAnalysis.MSBuild;

internal class DiagnosticReporterLoggerProvider : ILoggerProvider
{
private readonly DiagnosticReporter _reporter;

public DiagnosticReporterLoggerProvider(DiagnosticReporter reporter)
{
_reporter = reporter;
}

public ILogger CreateLogger(string categoryName)
{
return new Logger(_reporter, categoryName);
}

public void Dispose()
{
}

private sealed class Logger(DiagnosticReporter reporter, string categoryName) : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return null;
}

public bool IsEnabled(LogLevel logLevel)
{
return logLevel >= LogLevel.Warning;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
var kind = logLevel == LogLevel.Warning ? WorkspaceDiagnosticKind.Warning : WorkspaceDiagnosticKind.Failure;
var message = formatter(state, exception);
if (!string.IsNullOrEmpty(categoryName))
message = $"[{categoryName}] {message}";

reporter.Report(new WorkspaceDiagnostic(kind, message));
}
}
}
8 changes: 5 additions & 3 deletions src/Workspaces/Core/MSBuild/MSBuild/MSBuildProjectLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Roslyn.Utilities;
using MSB = Microsoft.Build;

Expand All @@ -24,6 +23,7 @@ public partial class MSBuildProjectLoader
private readonly SolutionServices _solutionServices;

private readonly DiagnosticReporter _diagnosticReporter;
private readonly Microsoft.Extensions.Logging.ILoggerFactory _loggerFactory;
private readonly PathResolver _pathResolver;
private readonly ProjectFileExtensionRegistry _projectFileExtensionRegistry;

Expand All @@ -33,11 +33,13 @@ public partial class MSBuildProjectLoader
internal MSBuildProjectLoader(
SolutionServices solutionServices,
DiagnosticReporter diagnosticReporter,
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory,
ProjectFileExtensionRegistry? projectFileExtensionRegistry,
ImmutableDictionary<string, string>? properties)
{
_solutionServices = solutionServices;
_diagnosticReporter = diagnosticReporter;
_loggerFactory = loggerFactory;
_pathResolver = new PathResolver(_diagnosticReporter);
_projectFileExtensionRegistry = projectFileExtensionRegistry ?? new ProjectFileExtensionRegistry(solutionServices, _diagnosticReporter);

Expand Down Expand Up @@ -188,7 +190,7 @@ public async Task<SolutionInfo> LoadSolutionInfoAsync(
}
}

var buildHostProcessManager = new BuildHostProcessManager(Properties);
var buildHostProcessManager = new BuildHostProcessManager(Properties, loggerFactory: _loggerFactory);
await using var _ = buildHostProcessManager.ConfigureAwait(false);

var worker = new Worker(
Expand Down Expand Up @@ -250,7 +252,7 @@ public async Task<ImmutableArray<ProjectInfo>> LoadProjectInfoAsync(
onPathFailure: reportingMode,
onLoaderFailure: reportingMode);

var buildHostProcessManager = new BuildHostProcessManager(Properties);
var buildHostProcessManager = new BuildHostProcessManager(Properties, loggerFactory: _loggerFactory);
await using var _ = buildHostProcessManager.ConfigureAwait(false);

var worker = new Worker(
Expand Down
7 changes: 5 additions & 2 deletions src/Workspaces/Core/MSBuild/MSBuild/MSBuildWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public sealed class MSBuildWorkspace : Workspace
private readonly NonReentrantLock _serializationLock = new();

private readonly MSBuildProjectLoader _loader;
private readonly Microsoft.Extensions.Logging.ILoggerFactory _loggerFactory;
private readonly ProjectFileExtensionRegistry _projectFileExtensionRegistry;
private readonly DiagnosticReporter _reporter;

Expand All @@ -41,7 +42,9 @@ private MSBuildWorkspace(
{
_reporter = new DiagnosticReporter(this);
_projectFileExtensionRegistry = new ProjectFileExtensionRegistry(Services.SolutionServices, _reporter);
_loader = new MSBuildProjectLoader(Services.SolutionServices, _reporter, _projectFileExtensionRegistry, properties);
_loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory([new DiagnosticReporterLoggerProvider(_reporter)],
new Microsoft.Extensions.Logging.LoggerFilterOptions() { MinLevel = Microsoft.Extensions.Logging.LogLevel.Warning });
_loader = new MSBuildProjectLoader(Services.SolutionServices, _reporter, _loggerFactory, _projectFileExtensionRegistry, properties);
}

/// <summary>
Expand Down Expand Up @@ -312,7 +315,7 @@ internal override bool TryApplyChanges(Solution newSolution, IProgress<CodeAnaly
try
{
Debug.Assert(_applyChangesBuildHostProcessManager == null);
_applyChangesBuildHostProcessManager = new BuildHostProcessManager(Properties);
_applyChangesBuildHostProcessManager = new BuildHostProcessManager(Properties, loggerFactory: _loggerFactory);

return base.TryApplyChanges(newSolution, progressTracker);
}
Expand Down

0 comments on commit 56b4868

Please sign in to comment.