Skip to content

Commit

Permalink
Ensure execution diagnostics are logged as well
Browse files Browse the repository at this point in the history
  • Loading branch information
dibarbet committed Oct 21, 2023
1 parent a44f928 commit 35b29cf
Showing 1 changed file with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,23 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
{
tasks.Add(Task.Run(async () =>
{
var (errorKind, preferredBuildHostKind) = await LoadOrReloadProjectAsync(projectToLoad, buildHostProcessManager, cancellationToken);
if (errorKind is LSP.MessageType.Error)
var result = await LoadOrReloadProjectAsync(projectToLoad, buildHostProcessManager, cancellationToken);
if (result.FailureType is LSP.MessageType.Error)
{
// We should display a toast when the value of displayedToast is 0. This will also update the value to 1 meaning we won't send any more toasts.
var shouldShowToast = Interlocked.CompareExchange(ref displayedToast, value: 1, comparand: 0) == 0;
if (shouldShowToast)
{
string message;

if (preferredBuildHostKind == BuildHostProcessKind.NetFramework)
if (result.PreferredKind == BuildHostProcessKind.NetFramework)
message = LanguageServerResources.Projects_failed_to_load_because_MSBuild_could_not_be_found;
else if (preferredBuildHostKind == BuildHostProcessKind.Mono)
else if (result.PreferredKind == BuildHostProcessKind.Mono)
message = LanguageServerResources.Projects_failed_to_load_because_Mono_could_not_be_found;
else
message = string.Format(LanguageServerResources.There_were_problems_loading_project_0_See_log_for_details, Path.GetFileName(projectToLoad.Path));

await ShowToastNotification.ShowToastNotificationAsync(errorKind.Value, message, cancellationToken, ShowToastNotification.ShowCSharpLogsCommand);
await ShowToastNotification.ShowToastNotificationAsync(result.FailureType.Value, message, cancellationToken, ShowToastNotification.ShowCSharpLogsCommand);
}
}
}, cancellationToken));
Expand All @@ -193,7 +193,7 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
return binaryLogPath;
}

private async Task<(LSP.MessageType? failureType, BuildHostProcessKind? preferredKind)> LoadOrReloadProjectAsync(ProjectToLoad projectToLoad, BuildHostProcessManager buildHostProcessManager, CancellationToken cancellationToken)
private async Task<(LSP.MessageType? FailureType, BuildHostProcessKind? PreferredKind)> LoadOrReloadProjectAsync(ProjectToLoad projectToLoad, BuildHostProcessManager buildHostProcessManager, CancellationToken cancellationToken)
{
BuildHostProcessKind? preferredBuildHostKind = null;

Expand All @@ -207,11 +207,10 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
{
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.
_ = LogDiagnostics(projectPath, diagnosticLogItems);
// We have total failures in evaluation, no point in continuing.
return (LSP.MessageType.Error, preferredBuildHostKind);
}

Expand Down Expand Up @@ -257,7 +256,14 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
}

await _projectLoadTelemetryReporter.ReportProjectLoadTelemetryAsync(projectFileInfos, projectToLoad, cancellationToken);
_logger.LogInformation($"Successfully completed load of {projectPath}");
diagnosticLogItems = await loadedFile.GetDiagnosticLogItemsAsync(cancellationToken);
var errorLevel = LogDiagnostics(projectPath, diagnosticLogItems);
if (errorLevel == null)
{
_logger.LogInformation($"Successfully completed load of {projectToLoad}");
}

return (errorLevel, preferredBuildHostKind);
}

return (null, null);
Expand All @@ -268,13 +274,20 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
return (LSP.MessageType.Error, preferredBuildHostKind);
}

void LogDiagnostics(string projectPath, ImmutableArray<DiagnosticLogItem> diagnosticLogItems)
LSP.MessageType? LogDiagnostics(string projectPath, ImmutableArray<DiagnosticLogItem> diagnosticLogItems)
{
if (diagnosticLogItems.IsEmpty)
{
return null;
}

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;
}
}
}

0 comments on commit 35b29cf

Please sign in to comment.