Skip to content

Commit 5ed6b45

Browse files
authored
Merge pull request #21667 from unoplatform/dev/jela/hr-console
feat: Log HR diagnostics to the console when devserver HR is enabled
2 parents c15e259 + d938182 commit 5ed6b45

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

src/Uno.UI.RemoteControl.Server.Processors/HotReload/ServerHotReloadProcessor.MetadataUpdate.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private async ValueTask ProcessSolutionChanged(HotReloadServerOperation hotReloa
281281
}
282282
else
283283
{
284-
await hotReload.Complete(HotReloadServerResult.Failed);
284+
await hotReload.Complete(HotReloadServerResult.Failed, diagnostics: hotReloadDiagnostics);
285285
}
286286

287287
return;
@@ -296,7 +296,7 @@ private async ValueTask ProcessSolutionChanged(HotReloadServerOperation hotReloa
296296
_reporter.Verbose(CSharpDiagnosticFormatter.Instance.Format(diagnostic, CultureInfo.InvariantCulture));
297297
}
298298

299-
await hotReload.Complete(HotReloadServerResult.RudeEdit);
299+
await hotReload.Complete(HotReloadServerResult.RudeEdit, diagnostics: hotReloadDiagnostics);
300300
return;
301301
}
302302

src/Uno.UI.RemoteControl.Server.Processors/HotReload/ServerHotReloadProcessor.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
using System.Collections.Generic;
44
using System.Collections.Immutable;
55
using System.Diagnostics;
6+
using System.Globalization;
67
using System.IO;
78
using System.Linq;
89
using System.Reactive.Linq;
910
using System.Threading;
1011
using System.Threading.Tasks;
12+
using Microsoft.CodeAnalysis;
13+
using Microsoft.CodeAnalysis.CSharp;
1114
using Microsoft.Extensions.Logging;
1215
using Uno.Disposables;
1316
using Uno.Extensions;
@@ -249,8 +252,13 @@ void LoadInfos(HotReloadServerOperation? operation)
249252
state = HotReloadState.Processing;
250253
}
251254

255+
var diagnosticsResult = operation.Diagnostics
256+
?.Where(d => d.Severity >= DiagnosticSeverity.Warning)
257+
.Select(d => CSharpDiagnosticFormatter.Instance.Format(d, CultureInfo.InvariantCulture))
258+
.ToImmutableList() ?? ImmutableList<string>.Empty;
259+
252260
foundCompleting |= operation == completing;
253-
infos.Add(new(operation.Id, operation.StartTime, operation.FilePaths, operation.CompletionTime, operation.Result));
261+
infos.Add(new(operation.Id, operation.StartTime, operation.FilePaths, operation.CompletionTime, operation.Result, diagnosticsResult));
254262
operation = operation.Previous!;
255263
}
256264
}
@@ -281,6 +289,7 @@ private class HotReloadServerOperation
281289
private ImmutableHashSet<string> _filePaths;
282290
private int /* HotReloadResult */ _result = -1;
283291
private CancellationTokenSource? _deferredCompletion;
292+
private ImmutableArray<Diagnostic>? _diagnostics;
284293

285294
// In VS we forcefully request to VS to hot-reload application, but in some cases the changes are not detected by VS and it returns a NoChanges result.
286295
// In such cases we can retry the hot-reload request to VS to let it process the file updates.
@@ -297,6 +306,8 @@ private class HotReloadServerOperation
297306

298307
public ImmutableHashSet<string> FilePaths => _filePaths;
299308

309+
public ImmutableArray<Diagnostic>? Diagnostics => _diagnostics;
310+
300311
public HotReloadServerResult? Result => _result is -1 ? null : (HotReloadServerResult)_result;
301312

302313
/// <param name="previous">The previous hot-reload operation which has to be considered as aborted when this new one completes.</param>
@@ -384,10 +395,10 @@ public async ValueTask DeferComplete(HotReloadServerResult result, Exception? ex
384395
}
385396
}
386397

387-
public ValueTask Complete(HotReloadServerResult result, Exception? exception = null)
388-
=> Complete(result, exception, isFromNext: false);
398+
public ValueTask Complete(HotReloadServerResult result, Exception? exception = null, ImmutableArray<Diagnostic>? diagnostics = null)
399+
=> Complete(result, exception, isFromNext: false, diagnostics: diagnostics);
389400

390-
private async ValueTask Complete(HotReloadServerResult result, Exception? exception, bool isFromNext)
401+
private async ValueTask Complete(HotReloadServerResult result, Exception? exception, bool isFromNext, ImmutableArray<Diagnostic>? diagnostics)
391402
{
392403
if (_result is -1
393404
&& result is HotReloadServerResult.NoChanges
@@ -416,6 +427,8 @@ private async ValueTask Complete(HotReloadServerResult result, Exception? except
416427
return; // Already completed
417428
}
418429

430+
_diagnostics = diagnostics;
431+
419432
CompletionTime = DateTimeOffset.Now;
420433
await _timeout.DisposeAsync();
421434

@@ -425,7 +438,8 @@ private async ValueTask Complete(HotReloadServerResult result, Exception? except
425438
await _previous.Complete(
426439
HotReloadServerResult.Aborted,
427440
new TimeoutException("An more recent hot-reload operation has completed."),
428-
isFromNext: true);
441+
isFromNext: true,
442+
diagnostics: null);
429443
}
430444

431445
if (!isFromNext) // Only the head of the list should request update

src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.Status.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ public void ReportServerStatus(HotReloadStatusMessage status)
104104
_serverState = status.State; // Do not override the state if it has already been set (debugger attached with dev-server)
105105
}
106106
ImmutableInterlocked.Update(ref _serverOperations, UpdateOperations, status.Operations);
107+
108+
// For tooling purposes, we dump the diagnostics in the log of the application.
109+
foreach (var serverOp in status.Operations)
110+
{
111+
owner.ReportDiagnostics(serverOp.Diagnostics);
112+
}
113+
107114
NotifyStatusChanged();
108115

109116
static ImmutableDictionary<long, HotReloadServerOperationData> UpdateOperations(ImmutableDictionary<long, HotReloadServerOperationData> history, IImmutableList<HotReloadServerOperationData> udpated)
@@ -164,6 +171,21 @@ private Status BuildStatus()
164171
}
165172
}
166173

174+
#if HAS_UNO_WINUI
175+
private void ReportDiagnostics(IImmutableList<string>? diagnostics)
176+
{
177+
if (diagnostics is { Count: > 0 })
178+
{
179+
_log.Error("The Hot Reload compilation failed with the following errors:");
180+
181+
foreach (var operation in diagnostics)
182+
{
183+
_log.Error(operation);
184+
}
185+
}
186+
}
187+
#endif
188+
167189
public class HotReloadClientOperation
168190
{
169191
#region Current

src/Uno.UI.RemoteControl/HotReload/Messages/HotReloadStatusMessage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ public record HotReloadServerOperationData(
3636
DateTimeOffset StartTime,
3737
ImmutableHashSet<string> FilePaths,
3838
DateTimeOffset? EndTime,
39-
HotReloadServerResult? Result);
39+
HotReloadServerResult? Result,
40+
IImmutableList<string>? Diagnostics);
4041
}

0 commit comments

Comments
 (0)