Skip to content

Commit

Permalink
Ensure diagnostics are logged before proceeding with further project …
Browse files Browse the repository at this point in the history
…loading
  • Loading branch information
dibarbet committed Oct 19, 2023
1 parent e3a0875 commit 53b5f4a
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.ProjectTelemetry;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.ProjectSystem;
using Microsoft.CodeAnalysis.Shared.TestHooks;
Expand Down Expand Up @@ -196,6 +197,7 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
{
BuildHostProcessKind? preferredBuildHostKind = null;

ImmutableArray<DiagnosticLogItem>? projectLoadDiagnostics = null;
try
{
var projectPath = projectToLoad.Path;
Expand All @@ -205,6 +207,15 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
if (await buildHost.IsProjectFileSupportedAsync(projectPath, cancellationToken))
{
var loadedFile = await buildHost.LoadProjectFileAsync(projectPath, cancellationToken);
var diagnosticLogItems = await loadedFile.GetDiagnosticLogItemsAsync(cancellationToken);
LogDiagnostics(projectPath, diagnosticLogItems);

if (diagnosticLogItems.Any(item => item.Kind is WorkspaceDiagnosticKind.Failure))
{
// If there were any total failures, no point in continuing.
return (LSP.MessageType.Error, preferredBuildHostKind);
}

var loadedProjectInfos = await loadedFile.GetProjectFileInfosAsync(cancellationToken);

// The out-of-proc build host supports more languages than we may actually have Workspace binaries for, so ensure we can actually process that
Expand Down Expand Up @@ -247,23 +258,7 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
}

await _projectLoadTelemetryReporter.ReportProjectLoadTelemetryAsync(projectFileInfos, projectToLoad, cancellationToken);

var diagnosticLogItems = await loadedFile.GetDiagnosticLogItemsAsync(cancellationToken);
if (diagnosticLogItems.Any())
{
foreach (var logItem in diagnosticLogItems)
{
var projectName = Path.GetFileName(projectPath);
_logger.Log(logItem.Kind is WorkspaceDiagnosticKind.Failure ? LogLevel.Error : LogLevel.Warning, $"{logItem.Kind} while loading {logItem.ProjectFilePath}: {logItem.Message}");
}

return (diagnosticLogItems.Any(logItem => logItem.Kind is WorkspaceDiagnosticKind.Failure) ? LSP.MessageType.Error : LSP.MessageType.Warning, preferredBuildHostKind);
}
else
{
_logger.LogInformation($"Successfully completed load of {projectPath}");
return (null, null);
}
_logger.LogInformation($"Successfully completed load of {projectPath}");
}

return (null, null);
Expand All @@ -273,5 +268,14 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
_logger.LogError(e, $"Exception thrown while loading {projectToLoad.Path}");
return (LSP.MessageType.Error, preferredBuildHostKind);
}

void LogDiagnostics(string projectPath, ImmutableArray<DiagnosticLogItem> diagnosticLogItems)
{
foreach (var logItem in diagnosticLogItems)
{
var projectName = Path.GetFileName(projectPath);
_logger.Log(logItem.Kind is WorkspaceDiagnosticKind.Failure ? LogLevel.Error : LogLevel.Warning, $"{logItem.Kind} while loading {logItem.ProjectFilePath}: {logItem.Message}");
}
}
}
}

0 comments on commit 53b5f4a

Please sign in to comment.