Skip to content

Commit

Permalink
[dotnet-watch] Make pipe reads and writes fully async (#45495)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Dec 20, 2024
1 parent b810aa4 commit bfa1bbc
Show file tree
Hide file tree
Showing 11 changed files with 662 additions and 246 deletions.
6 changes: 3 additions & 3 deletions src/BuiltInTools/DotNetDeltaApplier/StartupHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void Initialize()
agent.Reporter.Report("Writing capabilities: " + agent.Capabilities, AgentMessageSeverity.Verbose);

var initPayload = new ClientInitializationPayload(agent.Capabilities);
initPayload.Write(pipeClient);
await initPayload.WriteAsync(pipeClient, CancellationToken.None);

while (pipeClient.IsConnected)
{
Expand All @@ -71,8 +71,8 @@ public static void Initialize()
var logEntries = agent.GetAndClearLogEntries(update.ResponseLoggingLevel);

// response:
pipeClient.WriteByte(UpdatePayload.ApplySuccessValue);
UpdatePayload.WriteLog(pipeClient, logEntries);
await pipeClient.WriteAsync((byte)UpdatePayload.ApplySuccessValue, CancellationToken.None);
await UpdatePayload.WriteLogAsync(pipeClient, logEntries, CancellationToken.None);
}
}
catch (Exception ex)
Expand Down
1 change: 1 addition & 0 deletions src/BuiltInTools/HotReloadAgent/AgentMessageSeverity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.DotNet.HotReload;

internal enum AgentMessageSeverity : byte
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- This source package is used by Visual Studio WebTools -->
<TargetFramework>$(VisualStudioServiceTargetFramework)</TargetFramework>
<!--
Intentionally pinned. This feature is supported in projects targeting 6.0 or newer.
At the same time source build requires us to not target 6.0, so we fall back to netstandard.
-->
<TargetFramework>netstandard2.1</TargetFramework>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<DebugType>none</DebugType>
<GenerateDependencyFile>false</GenerateDependencyFile>
Expand All @@ -19,9 +22,6 @@
<!-- Remove once https://github.com/NuGet/Home/issues/8583 is fixed -->
<NoWarn>$(NoWarn);NU5128</NoWarn>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<!-- Make sure the shared source files do not require any global usings -->
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ await browserRefreshServer.SendAndReceiveAsync(
anyFailure = true;
}

ReportLog(reporter, data.Log.Select(entry => (entry.Message, (AgentMessageSeverity)entry.Severity)));
foreach (var entry in data.Log)
{
ReportLogEntry(reporter, entry.Message, (AgentMessageSeverity)entry.Severity);
}
},
cancellationToken);

Expand Down
10 changes: 7 additions & 3 deletions src/BuiltInTools/dotnet-watch/HotReload/DefaultDeltaApplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ async Task<ImmutableArray<string>> ConnectAsync()

// When the client connects, the first payload it sends is the initialization payload which includes the apply capabilities.

var capabilities = ClientInitializationPayload.Read(_pipe).Capabilities;
var capabilities = (await ClientInitializationPayload.ReadAsync(_pipe, cancellationToken)).Capabilities;
Reporter.Verbose($"Capabilities: '{capabilities}'");
return capabilities.Split(' ').ToImmutableArray();
return [.. capabilities.Split(' ')];
}
catch (EndOfStreamException)
{
Expand Down Expand Up @@ -149,7 +149,11 @@ private async Task<bool> ReceiveApplyUpdateResult(CancellationToken cancellation
return false;
}

ReportLog(Reporter, UpdatePayload.ReadLog(_pipe));
await foreach (var (message, severity) in UpdatePayload.ReadLogAsync(_pipe, cancellationToken))
{
ReportLogEntry(Reporter, message, severity);
}

return true;
}
finally
Expand Down
29 changes: 13 additions & 16 deletions src/BuiltInTools/dotnet-watch/HotReload/DeltaApplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,21 @@ internal abstract class DeltaApplier(IReporter reporter) : IDisposable

public abstract void Dispose();

public static void ReportLog(IReporter reporter, IEnumerable<(string message, AgentMessageSeverity severity)> log)
public static void ReportLogEntry(IReporter reporter, string message, AgentMessageSeverity severity)
{
foreach (var (message, severity) in log)
switch (severity)
{
switch (severity)
{
case AgentMessageSeverity.Error:
reporter.Error(message);
break;

case AgentMessageSeverity.Warning:
reporter.Warn(message, emoji: "⚠");
break;

default:
reporter.Verbose(message, emoji: "🕵️");
break;
}
case AgentMessageSeverity.Error:
reporter.Error(message);
break;

case AgentMessageSeverity.Warning:
reporter.Warn(message, emoji: "⚠");
break;

default:
reporter.Verbose(message, emoji: "🕵️");
break;
}
}
}
Expand Down
Loading

0 comments on commit bfa1bbc

Please sign in to comment.