Skip to content

Commit

Permalink
publish DiagnosticsProduced even when there are no diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Apr 24, 2024
1 parent 9adb29a commit 389b3e8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 62 deletions.
18 changes: 8 additions & 10 deletions src/Microsoft.DotNet.Interactive.CSharp/CSharpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,16 +316,14 @@ await RunAsync(
// Publish the compilation diagnostics. This doesn't include the exception.
var kernelDiagnostics = diagnostics.Select(Diagnostic.FromCodeAnalysisDiagnostic).ToImmutableArray();

if (kernelDiagnostics.Length > 0)
{
var formattedDiagnostics =
diagnostics
.Select(d => d.ToString())
.Select(text => new FormattedValue(PlainTextFormatter.MimeType, text))
.ToImmutableArray();

context.Publish(new DiagnosticsProduced(kernelDiagnostics, submitCode, formattedDiagnostics));
}

var formattedDiagnostics =
diagnostics
.Select(d => d.ToString())
.Select(text => new FormattedValue(PlainTextFormatter.MimeType, text))
.ToImmutableArray();

context.Publish(new DiagnosticsProduced(kernelDiagnostics, submitCode, formattedDiagnostics));

// Report the compilation failure or exception
if (exception is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,13 @@ async Task IKernelCommandHandler<CompileProject>.HandleAsync(CompileProject comm
var result = await _workspaceServer.CompileAsync(request);

var diagnostics = GetDiagnostics(_buffer.Content, result).ToArray();
if (diagnostics.Any())
{
context.Publish(new DiagnosticsProduced(diagnostics, command));

context.Publish(new DiagnosticsProduced(diagnostics, command));

if (diagnostics.Any(d => d.Severity == CodeAnalysis.DiagnosticSeverity.Error))
{
context.Fail(command);
return;
}
if (diagnostics.Any(d => d.Severity == CodeAnalysis.DiagnosticSeverity.Error))
{
context.Fail(command);
return;
}

context.Publish(new AssemblyProduced(command, new Base64EncodedAssembly(result.Base64Assembly)));
Expand Down Expand Up @@ -189,10 +187,8 @@ async Task IKernelCommandHandler<RequestDiagnostics>.HandleAsync(RequestDiagnost
var result = await _workspaceServer.CompileAsync(request);

var diagnostics = GetDiagnostics(command.Code, result).ToArray();
if (diagnostics.Any())
{
context.Publish(new DiagnosticsProduced(diagnostics, command));
}

context.Publish(new DiagnosticsProduced(diagnostics, command));
}

async Task IKernelCommandHandler<RequestSignatureHelp>.HandleAsync(RequestSignatureHelp command, KernelInvocationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ protected override void Write(

if (block.Diagnostics.Any())
{

renderer.WriteLine(@"<div class=""notification is-danger"">");
renderer.WriteLine(SvgResources.ErrorSvg);

Expand Down
25 changes: 11 additions & 14 deletions src/Microsoft.DotNet.Interactive.Http/HttpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,17 @@ private async Task<HttpResponse> GetResponseWithTimingAsync(

private void PublishDiagnostics(KernelInvocationContext context, KernelCommand command, IReadOnlyCollection<Diagnostic> diagnostics)
{
if (diagnostics.Any())
{
var formattedDiagnostics =
diagnostics
.Select(d => d.ToString())
.Select(text => new FormattedValue(PlainTextFormatter.MimeType, text))
.ToArray();

context.Publish(
new DiagnosticsProduced(
diagnostics.Select(ToSerializedDiagnostic),
command,
formattedDiagnostics));
}
var formattedDiagnostics =
diagnostics
.Select(d => d.ToString())
.Select(text => new FormattedValue(PlainTextFormatter.MimeType, text))
.ToArray();

context.Publish(
new DiagnosticsProduced(
diagnostics.Select(ToSerializedDiagnostic),
command,
formattedDiagnostics));

static Interactive.Diagnostic ToSerializedDiagnostic(Diagnostic d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,9 @@ Task IKernelCommandHandler<RequestDiagnostics>.HandleAsync(
var code = requestDiagnostics.Code;

IsCompleteSubmission(code, out var parseErrors);

if (parseErrors.Length > 0)
{
var diagnostics = parseErrors.Select(ToDiagnostic);
context.Publish(new DiagnosticsProduced(diagnostics, requestDiagnostics));
}

var diagnostics = parseErrors.Select(ToDiagnostic);
context.Publish(new DiagnosticsProduced(diagnostics, requestDiagnostics));

return Task.CompletedTask;
}
Expand Down
28 changes: 15 additions & 13 deletions src/Microsoft.DotNet.Interactive.Tests/LanguageKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,19 +1139,21 @@ public async Task it_supports_csharp_11()
{
using var kernel = CreateKernel();

var events = kernel.KernelEvents.ToSubscribedList();

await kernel.SendAsync(new SubmitCode(@"public static int CheckSwitch(int[] values)
=> values switch
{
[1, 2, .., 10] => 1,
[1, 2] => 2,
[1, _] => 3,
[1, ..] => 4,
[..] => 50
};"));

events.OfType<DiagnosticsProduced>().Should().BeEmpty();
var result = await kernel.SendAsync(new SubmitCode(
"""
public static int CheckSwitch(int[] values)
=> values switch
{
[1, 2, .., 10] => 1,
[1, 2] => 2,
[1, _] => 3,
[1, ..] => 4,
[..] => 50
};
"""));

result.Events.Should().ContainSingle<DiagnosticsProduced>()
.Which.Diagnostics.Should().BeEmpty();
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.DotNet.Interactive.Commands;

namespace Microsoft.DotNet.Interactive.Events;
Expand All @@ -22,11 +21,6 @@ public DiagnosticsProduced(IEnumerable<Diagnostic> diagnostics,
throw new ArgumentNullException(nameof(diagnostics));
}

if (!diagnostics.Any())
{
throw new ArgumentException("At least one diagnostic required.", nameof(diagnostics));
}

_diagnostics = diagnostics.ToImmutableList();
FormattedDiagnostics = formattedDiagnostics ?? Array.Empty<FormattedValue>();
}
Expand Down

0 comments on commit 389b3e8

Please sign in to comment.