Skip to content

Commit b959fbc

Browse files
authored
[wasm] Fix failing CI test, and misc fixes (#72293)
* Emit exit code also when a process fails in MonoAOTCompiler, and InstallWorkloadFromArtifacts * [wasm] host: Use a simple console formatter to emit the output without .. any prefixes, but still using the logger. * [wasm] host: Try to find path for the js engine * [wasm] Host: ensure process event handlers have completed before exiting
1 parent 349d811 commit b959fbc

File tree

8 files changed

+104
-15
lines changed

8 files changed

+104
-15
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
using System;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace Microsoft.WebAssembly.AppHost;
10+
11+
internal static class ConsoleLoggerExtensions
12+
{
13+
public static ILoggingBuilder AddPassThroughConsole(this ILoggingBuilder builder) =>
14+
builder
15+
.AddConsole(options => options.FormatterName = nameof(PassThroughConsoleFormatter))
16+
.AddConsoleFormatter<PassThroughConsoleFormatter, PassThroughConsoleFormatterOptions>();
17+
18+
public static ILoggingBuilder AddPassThroughConsole(
19+
this ILoggingBuilder builder, Action<PassThroughConsoleFormatterOptions> configure) =>
20+
builder
21+
.AddConsole(options => options.FormatterName = nameof(PassThroughConsoleFormatter))
22+
.AddConsoleFormatter<PassThroughConsoleFormatter, PassThroughConsoleFormatterOptions>(configure);
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
using Microsoft.Extensions.Logging.Console;
7+
8+
namespace Microsoft.WebAssembly.AppHost;
9+
internal sealed class PassThroughConsoleFormatterOptions : ConsoleFormatterOptions
10+
{
11+
public string Prefix = string.Empty;
12+
}

src/mono/wasm/host/JSEngineHost.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,21 @@ private async Task<int> RunAsync()
4848
_ => throw new CommandLineException($"Unsupported engine {_args.Host}")
4949
};
5050

51+
string? engineBinaryPath;
5152
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
5253
{
5354
if (engineBinary.Equals("node"))
54-
engineBinary = FindEngineInPath(engineBinary + ".exe"); // NodeJS ships as .exe rather than .cmd
55+
engineBinaryPath = FindEngineInPath(engineBinary + ".exe"); // NodeJS ships as .exe rather than .cmd
5556
else
56-
engineBinary = FindEngineInPath(engineBinary + ".cmd");
57+
engineBinaryPath = FindEngineInPath(engineBinary + ".cmd");
5758
}
59+
else
60+
{
61+
engineBinaryPath = FindEngineInPath(engineBinary);
62+
}
63+
64+
if (engineBinaryPath is null)
65+
throw new CommandLineException($"Cannot find host {engineBinary} in PATH");
5866

5967
if (_args.CommonConfig.Debugging)
6068
throw new CommandLineException($"Debugging not supported with {_args.Host}");
@@ -99,7 +107,7 @@ private async Task<int> RunAsync()
99107
return exitCode;
100108
}
101109

102-
private static string FindEngineInPath(string engineBinary)
110+
private static string? FindEngineInPath(string engineBinary)
103111
{
104112
if (File.Exists(engineBinary) || Path.IsPathRooted(engineBinary))
105113
return engineBinary;
@@ -116,6 +124,6 @@ private static string FindEngineInPath(string engineBinary)
116124
return fullPath;
117125
}
118126

119-
return engineBinary;
127+
return null;
120128
}
121129
}

src/mono/wasm/host/Program.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ public static async Task<int> Main(string[] args)
3030

3131
using CancellationTokenSource cts = new();
3232
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
33-
builder.AddSimpleConsole(options =>
34-
{
35-
options.SingleLine = true;
36-
options.TimestampFormat = "[HH:mm:ss] ";
37-
})
38-
.AddFilter("DevToolsProxy", LogLevel.Information)
39-
.AddFilter("FirefoxMonoProxy", LogLevel.Information)
40-
.AddFilter("host", LogLevel.Trace)
41-
.AddFilter(null, LogLevel.Warning));
33+
builder
34+
.AddPassThroughConsole()
35+
.AddFilter("DevToolsProxy", LogLevel.Information)
36+
.AddFilter("FirefoxMonoProxy", LogLevel.Information)
37+
.AddFilter("host", LogLevel.Trace)
38+
.AddFilter(null, LogLevel.Warning));
4239

4340
ILogger logger = loggerFactory.CreateLogger("host");
4441
try
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
using System;
7+
using System.IO;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.Extensions.Logging.Abstractions;
10+
using Microsoft.Extensions.Logging.Console;
11+
using Microsoft.Extensions.Options;
12+
13+
namespace Microsoft.WebAssembly.AppHost;
14+
15+
internal sealed class PassThroughConsoleFormatter : ConsoleFormatter, IDisposable
16+
{
17+
private readonly IDisposable? _optionsReloadToken;
18+
private PassThroughConsoleFormatterOptions? _formatterOptions;
19+
20+
public PassThroughConsoleFormatter(IOptionsMonitor<PassThroughConsoleFormatterOptions> options)
21+
: base("PassThroughConsoleFormatter") =>
22+
(_optionsReloadToken, _formatterOptions) =
23+
(options.OnChange((options, _) => ReloadLoggerOptions(options)), options.CurrentValue);
24+
25+
private void ReloadLoggerOptions(PassThroughConsoleFormatterOptions options) => _formatterOptions = options;
26+
27+
public override void Write<TState>(
28+
in LogEntry<TState> logEntry,
29+
IExternalScopeProvider? scopeProvider,
30+
TextWriter textWriter)
31+
{
32+
string? message =
33+
logEntry.Formatter?.Invoke(
34+
logEntry.State, logEntry.Exception);
35+
36+
if (message is null)
37+
{
38+
return;
39+
}
40+
41+
if (!string.IsNullOrEmpty(_formatterOptions!.Prefix))
42+
textWriter.Write(_formatterOptions!.Prefix);
43+
textWriter.WriteLine(message);
44+
}
45+
46+
public void Dispose() => _optionsReloadToken?.Dispose();
47+
}

src/mono/wasm/host/Utils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public static async Task<int> TryRunProcess(
5252
process.BeginErrorReadLine();
5353

5454
await process.WaitForExitAsync();
55+
// Ensure all async handlers have been called
56+
process.WaitForExit();
5557
return process.ExitCode;
5658
}
5759
}

src/tasks/AotCompilerTask/MonoAOTCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ private bool PrecompileLibrary(PrecompileArguments args)
941941

942942
if (exitCode != 0)
943943
{
944-
Log.LogError($"Precompiling failed for {assembly}.{Environment.NewLine}{output}");
944+
Log.LogError($"Precompiling failed for {assembly} with exit code {exitCode}.{Environment.NewLine}{output}");
945945
return false;
946946
}
947947

src/tasks/WorkloadBuildTasks/InstallWorkloadFromArtifacts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private bool InstallPacks(ITaskItem workloadId, string nugetConfigContents)
108108
debugMessageImportance: MessageImportance.High);
109109
if (exitCode != 0)
110110
{
111-
Log.LogError($"workload install failed: {output}");
111+
Log.LogError($"workload install failed with exit code {exitCode}: {output}");
112112

113113
foreach (string dir in Directory.EnumerateDirectories(Path.Combine(SdkDir, "sdk-manifests"), "*", SearchOption.AllDirectories))
114114
Log.LogMessage(MessageImportance.Low, $"\t{Path.Combine(SdkDir, "sdk-manifests", dir)}");

0 commit comments

Comments
 (0)