From d2937e42772571eb64721f728f1d9d5932e1dbbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Thu, 15 Feb 2024 15:02:08 +0100 Subject: [PATCH 01/10] Migration of #97213 --- .../scenarios/BuildWasmAppsJobsList.txt | 1 + .../Blazor/BlazorWasmTestBase.cs | 4 + .../TestAppScenarios/AppSettingsTests.cs | 2 +- .../TestAppScenarios/AppTestBase.cs | 21 ++-- .../TestAppScenarios/DebugLevelTests.cs | 109 ++++++++++++++++++ .../DownloadResourceProgressTests.cs | 2 +- .../TestAppScenarios/LazyLoadingTests.cs | 4 +- .../LibraryInitializerTests.cs | 4 +- .../TestAppScenarios/SatelliteLoadingTests.cs | 2 +- .../Wasm.Build.Tests/WasmTemplateTestBase.cs | 6 +- src/mono/wasm/build/WasmApp.targets | 3 - src/mono/wasm/runtime/lazyLoading.ts | 2 +- src/mono/wasm/runtime/loader/assets.ts | 2 +- src/mono/wasm/runtime/loader/config.ts | 12 +- src/mono/wasm/runtime/loader/globals.ts | 4 +- src/mono/wasm/runtime/startup.ts | 10 +- src/mono/wasm/runtime/types/internal.ts | 2 +- .../GenerateWasmBootJson.cs | 17 ++- 18 files changed, 169 insertions(+), 38 deletions(-) create mode 100644 src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTests.cs diff --git a/eng/testing/scenarios/BuildWasmAppsJobsList.txt b/eng/testing/scenarios/BuildWasmAppsJobsList.txt index 51bb6bcbb3f29..9b48d609dc711 100644 --- a/eng/testing/scenarios/BuildWasmAppsJobsList.txt +++ b/eng/testing/scenarios/BuildWasmAppsJobsList.txt @@ -43,3 +43,4 @@ Wasm.Build.Tests.WasmTemplateTests Wasm.Build.Tests.WorkloadTests Wasm.Build.Tests.TestAppScenarios.DownloadResourceProgressTests Wasm.Build.Tests.MT.Blazor.SimpleMultiThreadedTests +Wasm.Build.Tests.TestAppScenarios.DebugLevelTests diff --git a/src/mono/wasm/Wasm.Build.Tests/Blazor/BlazorWasmTestBase.cs b/src/mono/wasm/Wasm.Build.Tests/Blazor/BlazorWasmTestBase.cs index 1d13051dd7344..dbf0e848f7513 100644 --- a/src/mono/wasm/Wasm.Build.Tests/Blazor/BlazorWasmTestBase.cs +++ b/src/mono/wasm/Wasm.Build.Tests/Blazor/BlazorWasmTestBase.cs @@ -75,6 +75,10 @@ public string CreateBlazorWasmTemplateProject(string id) if (options.ExpectSuccess && options.AssertAppBundle) { + // Because we do relink in Release publish by default + if (options.Config == "Release") + options = options with { ExpectedFileType = NativeFilesType.Relinked }; + AssertBundle(res.Output, options with { IsPublish = true }); } diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppSettingsTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppSettingsTests.cs index 96f2c4ebd6a1b..5d028cc238909 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppSettingsTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppSettingsTests.cs @@ -28,7 +28,7 @@ public async Task LoadAppSettingsBasedOnApplicationEnvironment(string applicatio CopyTestAsset("WasmBasicTestApp", "AppSettingsTests"); PublishProject("Debug"); - var result = await RunSdkStyleApp(new( + var result = await RunSdkStyleAppForPublish(new( Configuration: "Debug", TestScenario: "AppSettingsTest", BrowserQueryString: new Dictionary { ["applicationEnvironment"] = applicationEnvironment } diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppTestBase.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppTestBase.cs index dc6fb9b490e7f..771daff1f5d46 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppTestBase.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppTestBase.cs @@ -36,15 +36,15 @@ protected void CopyTestAsset(string assetName, string generatedProjectNamePrefix _projectDir = Path.Combine(_projectDir!, "App"); } - protected void BuildProject(string configuration) + protected void BuildProject(string configuration, params string[] extraArgs) { - (CommandResult result, _) = BlazorBuild(new BlazorBuildOptions(Id, configuration)); + (CommandResult result, _) = BlazorBuild(new BlazorBuildOptions(Id, configuration), extraArgs); result.EnsureSuccessful(); } - protected void PublishProject(string configuration) + protected void PublishProject(string configuration, params string[] extraArgs) { - (CommandResult result, _) = BlazorPublish(new BlazorBuildOptions(Id, configuration)); + (CommandResult result, _) = BlazorPublish(new BlazorBuildOptions(Id, configuration), extraArgs); result.EnsureSuccessful(); } @@ -52,7 +52,13 @@ protected void PublishProject(string configuration) .WithWorkingDirectory(_projectDir!) .WithEnvironmentVariable("NUGET_PACKAGES", _nugetPackagesDir); - protected async Task RunSdkStyleApp(RunOptions options) + protected Task RunSdkStyleAppForBuild(RunOptions options) + => RunSdkStyleApp(options, BlazorRunHost.DotnetRun); + + protected Task RunSdkStyleAppForPublish(RunOptions options) + => RunSdkStyleApp(options, BlazorRunHost.WebServer); + + private async Task RunSdkStyleApp(RunOptions options, BlazorRunHost host = BlazorRunHost.DotnetRun) { string queryString = "?test=" + options.TestScenario; if (options.BrowserQueryString != null) @@ -67,9 +73,10 @@ protected async Task RunSdkStyleApp(RunOptions options) CheckCounter: false, Config: options.Configuration, OnConsoleMessage: OnConsoleMessage, - QueryString: queryString); + QueryString: queryString, + Host: host); - await BlazorRunForBuildWithDotnetRun(blazorRunOptions); + await BlazorRunTest(blazorRunOptions); void OnConsoleMessage(IConsoleMessage msg) { diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTests.cs new file mode 100644 index 0000000000000..3dfe4c467f79f --- /dev/null +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTests.cs @@ -0,0 +1,109 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +#nullable enable + +namespace Wasm.Build.Tests.TestAppScenarios; + +public class DebugLevelTests : AppTestBase +{ + public DebugLevelTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) + : base(output, buildContext) + { + } + + private void AssertDebugLevel(RunResult result, int value) + { + Assert.Collection( + result.TestOutput, + m => Assert.Equal($"WasmDebugLevel: {value}", m) + ); + } + + [Theory] + [InlineData("Debug")] + [InlineData("Release")] + public async Task BuildWithDefaultLevel(string configuration) + { + CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_BuildWithDefaultLevel_{configuration}"); + BuildProject(configuration); + + var result = await RunSdkStyleAppForBuild(new( + Configuration: configuration, + TestScenario: "DebugLevelTest" + )); + AssertDebugLevel(result, -1); + } + + [Theory] + [InlineData("Debug", 1)] + [InlineData("Release", 1)] + [InlineData("Debug", 0)] + [InlineData("Release", 0)] + public async Task BuildWithExplicitValue(string configuration, int debugLevel) + { + CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_BuildWithExplicitValue_{configuration}"); + BuildProject(configuration, $"-p:WasmDebugLevel={debugLevel}"); + + var result = await RunSdkStyleAppForBuild(new( + Configuration: configuration, + TestScenario: "DebugLevelTest" + )); + AssertDebugLevel(result, debugLevel); + } + + [Theory] + [InlineData("Debug")] + [InlineData("Release")] + public async Task PublishWithDefaultLevel(string configuration) + { + CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_PublishWithDefaultLevel_{configuration}"); + PublishProject(configuration); + + var result = await RunSdkStyleAppForPublish(new( + Configuration: configuration, + TestScenario: "DebugLevelTest" + )); + AssertDebugLevel(result, 0); + } + + [Theory] + [InlineData("Debug", 1)] + [InlineData("Release", 1)] + [InlineData("Debug", -1)] + [InlineData("Release", -1)] + public async Task PublishWithExplicitValue(string configuration, int debugLevel) + { + CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_PublishWithExplicitValue_{configuration}"); + PublishProject(configuration, $"-p:WasmDebugLevel={debugLevel}"); + + var result = await RunSdkStyleAppForPublish(new( + Configuration: configuration, + TestScenario: "DebugLevelTest" + )); + AssertDebugLevel(result, debugLevel); + } + + [Theory] + [InlineData("Debug")] + [InlineData("Release")] + public async Task PublishWithDefaultLevelAndPdbs(string configuration) + { + CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_PublishWithDefaultLevelAndPdbs_{configuration}"); + PublishProject(configuration, $"-p:CopyOutputSymbolsToPublishDirectory=true"); + + var result = await RunSdkStyleAppForPublish(new( + Configuration: configuration, + TestScenario: "DebugLevelTest" + )); + AssertDebugLevel(result, -1); + } +} diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DownloadResourceProgressTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DownloadResourceProgressTests.cs index 70f9b4f1507d2..7cc55ebd07ae2 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DownloadResourceProgressTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DownloadResourceProgressTests.cs @@ -28,7 +28,7 @@ public async Task DownloadProgressFinishes(bool failAssemblyDownload) CopyTestAsset("WasmBasicTestApp", $"DownloadResourceProgressTests_{failAssemblyDownload}"); PublishProject("Debug"); - var result = await RunSdkStyleApp(new( + var result = await RunSdkStyleAppForPublish(new( Configuration: "Debug", TestScenario: "DownloadResourceProgressTest", BrowserQueryString: new Dictionary { ["failAssemblyDownload"] = failAssemblyDownload.ToString().ToLowerInvariant() } diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/LazyLoadingTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/LazyLoadingTests.cs index 8f37a47e18860..cf16a0536a38d 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/LazyLoadingTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/LazyLoadingTests.cs @@ -26,7 +26,7 @@ public async Task LoadLazyAssemblyBeforeItIsNeeded() CopyTestAsset("WasmBasicTestApp", "LazyLoadingTests"); PublishProject("Debug"); - var result = await RunSdkStyleApp(new(Configuration: "Debug", TestScenario: "LazyLoadingTest")); + var result = await RunSdkStyleAppForPublish(new(Configuration: "Debug", TestScenario: "LazyLoadingTest")); Assert.True(result.TestOutput.Any(m => m.Contains("FirstName")), "The lazy loading test didn't emit expected message with JSON"); } @@ -36,7 +36,7 @@ public async Task FailOnMissingLazyAssembly() CopyTestAsset("WasmBasicTestApp", "LazyLoadingTests"); PublishProject("Debug"); - var result = await RunSdkStyleApp(new( + var result = await RunSdkStyleAppForPublish(new( Configuration: "Debug", TestScenario: "LazyLoadingTest", BrowserQueryString: new Dictionary { ["loadRequiredAssembly"] = "false" }, diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/LibraryInitializerTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/LibraryInitializerTests.cs index 6f68a96ad1d61..e985ad23d89a0 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/LibraryInitializerTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/LibraryInitializerTests.cs @@ -29,7 +29,7 @@ public async Task LoadLibraryInitializer() CopyTestAsset("WasmBasicTestApp", "LibraryInitializerTests_LoadLibraryInitializer"); PublishProject("Debug"); - var result = await RunSdkStyleApp(new(Configuration: "Debug", TestScenario: "LibraryInitializerTest")); + var result = await RunSdkStyleAppForPublish(new(Configuration: "Debug", TestScenario: "LibraryInitializerTest")); Assert.Collection( result.TestOutput, m => Assert.Equal("LIBRARY_INITIALIZER_TEST = 1", m) @@ -42,7 +42,7 @@ public async Task AbortStartupOnError() CopyTestAsset("WasmBasicTestApp", "LibraryInitializerTests_AbortStartupOnError"); PublishProject("Debug"); - var result = await RunSdkStyleApp(new( + var result = await RunSdkStyleAppForPublish(new( Configuration: "Debug", TestScenario: "LibraryInitializerTest", BrowserQueryString: new Dictionary { ["throwError"] = "true" }, diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/SatelliteLoadingTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/SatelliteLoadingTests.cs index 31dcb65582869..2088e1522ad73 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/SatelliteLoadingTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/SatelliteLoadingTests.cs @@ -29,7 +29,7 @@ public async Task LoadSatelliteAssembly() CopyTestAsset("WasmBasicTestApp", "SatelliteLoadingTests"); BuildProject("Debug"); - var result = await RunSdkStyleApp(new(Configuration: "Debug", TestScenario: "SatelliteAssembliesTest")); + var result = await RunSdkStyleAppForBuild(new(Configuration: "Debug", TestScenario: "SatelliteAssembliesTest")); Assert.Collection( result.TestOutput, m => Assert.Equal("default: hello", m), diff --git a/src/mono/wasm/Wasm.Build.Tests/WasmTemplateTestBase.cs b/src/mono/wasm/Wasm.Build.Tests/WasmTemplateTestBase.cs index a7ae3fba70ed3..1cc70d823ef14 100644 --- a/src/mono/wasm/Wasm.Build.Tests/WasmTemplateTestBase.cs +++ b/src/mono/wasm/Wasm.Build.Tests/WasmTemplateTestBase.cs @@ -21,7 +21,7 @@ protected WasmTemplateTestBase(ITestOutputHelper output, SharedBuildPerTestClass _provider.BundleDirName = "AppBundle"; } - public string CreateWasmTemplateProject(string id, string template = "wasmbrowser", string extraArgs = "", bool runAnalyzers = true) + public string CreateWasmTemplateProject(string id, string template = "wasmbrowser", string extraArgs = "", bool runAnalyzers = true, string? extraProperties = null) { InitPaths(id); InitProjectDir(_projectDir, addNuGetSourceForLocalPackages: true); @@ -42,7 +42,9 @@ public string CreateWasmTemplateProject(string id, string template = "wasmbrowse .EnsureSuccessful(); string projectfile = Path.Combine(_projectDir!, $"{id}.csproj"); - string extraProperties = string.Empty; + if (extraProperties == null) + extraProperties = string.Empty; + extraProperties += "true"; if (runAnalyzers) extraProperties += "true"; diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 0a22f3f82d528..679ecb515042d 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -142,9 +142,6 @@ false - - -1 - true .wasm diff --git a/src/mono/wasm/runtime/lazyLoading.ts b/src/mono/wasm/runtime/lazyLoading.ts index 55bcfd67101e8..85058f89819b7 100644 --- a/src/mono/wasm/runtime/lazyLoading.ts +++ b/src/mono/wasm/runtime/lazyLoading.ts @@ -26,7 +26,7 @@ export async function loadLazyAssembly(assemblyNameToLoad: string): Promise debugBuild=true & debugLevel=-1 => -1 - // - Build (release) => debugBuild=true & debugLevel=0 => 0 - // - Publish (debug) => debugBuild=false & debugLevel=-1 => 0 - // - Publish (release) => debugBuild=false & debugLevel=0 => 0 - config.debugLevel = hasDebuggingEnabled(config) ? config.debugLevel : 0; - if (config.diagnosticTracing === undefined && BuildConfiguration === "Debug") { config.diagnosticTracing = true; } @@ -264,14 +257,13 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi } } -export function hasDebuggingEnabled(config: MonoConfigInternal): boolean { +export function isDebuggingSupported(): boolean { // Copied from blazor MonoDebugger.ts/attachDebuggerHotkey if (!globalThis.navigator) { return false; } - const hasReferencedPdbs = !!config.resources!.pdb; - return (hasReferencedPdbs || config.debugLevel != 0) && (loaderHelpers.isChromium || loaderHelpers.isFirefox); + return loaderHelpers.isChromium || loaderHelpers.isFirefox; } async function loadBootConfig(module: DotnetModuleInternal): Promise { diff --git a/src/mono/wasm/runtime/loader/globals.ts b/src/mono/wasm/runtime/loader/globals.ts index 88b0d472de3ca..0bd081ed8a82c 100644 --- a/src/mono/wasm/runtime/loader/globals.ts +++ b/src/mono/wasm/runtime/loader/globals.ts @@ -12,7 +12,7 @@ import { assertIsControllablePromise, createPromiseController, getPromiseControl import { mono_download_assets, resolve_single_asset_path, retrieve_asset_download } from "./assets"; import { setup_proxy_console } from "./logging"; import { invokeLibraryInitializers } from "./libraryInitializers"; -import { hasDebuggingEnabled } from "./config"; +import { isDebuggingSupported } from "./config"; import { logDownloadStatsToConsole, purgeUnusedCacheEntriesAsync } from "./assetsCache"; export const ENVIRONMENT_IS_NODE = typeof process == "object" && typeof process.versions == "object" && typeof process.versions.node == "string"; @@ -102,9 +102,9 @@ export function setLoaderGlobals( logDownloadStatsToConsole, purgeUnusedCacheEntriesAsync, - hasDebuggingEnabled, retrieve_asset_download, invokeLibraryInitializers, + isDebuggingSupported, // from wasm-feature-detect npm package exceptions, diff --git a/src/mono/wasm/runtime/startup.ts b/src/mono/wasm/runtime/startup.ts index c385844dfd30b..e7349d494752d 100644 --- a/src/mono/wasm/runtime/startup.ts +++ b/src/mono/wasm/runtime/startup.ts @@ -543,7 +543,7 @@ async function mono_wasm_before_memory_snapshot() { if (runtimeHelpers.config.browserProfilerOptions) mono_wasm_init_browser_profiler(runtimeHelpers.config.browserProfilerOptions); - mono_wasm_load_runtime("unused", runtimeHelpers.config.debugLevel); + mono_wasm_load_runtime(); // we didn't have snapshot yet and the feature is enabled. Take snapshot now. if (runtimeHelpers.config.startupMemoryCache) { @@ -556,17 +556,21 @@ async function mono_wasm_before_memory_snapshot() { endMeasure(mark, MeasuredBlock.memorySnapshot); } -export function mono_wasm_load_runtime(unused?: string, debugLevel?: number): void { +export function mono_wasm_load_runtime(): void { mono_log_debug("mono_wasm_load_runtime"); try { const mark = startMeasure(); + let debugLevel = runtimeHelpers.config.debugLevel; if (debugLevel == undefined) { debugLevel = 0; if (runtimeHelpers.config.debugLevel) { debugLevel = 0 + debugLevel; } } - cwraps.mono_wasm_load_runtime(unused || "unused", debugLevel); + if (!loaderHelpers.isDebuggingSupported() || !runtimeHelpers.config.resources!.pdb) { + debugLevel = 0; + } + cwraps.mono_wasm_load_runtime("unused", debugLevel); endMeasure(mark, MeasuredBlock.loadRuntime); } catch (err: any) { diff --git a/src/mono/wasm/runtime/types/internal.ts b/src/mono/wasm/runtime/types/internal.ts index a91b21973c8d5..6deb3ed0362a0 100644 --- a/src/mono/wasm/runtime/types/internal.ts +++ b/src/mono/wasm/runtime/types/internal.ts @@ -145,7 +145,6 @@ export type LoaderHelpers = { out(message: string): void; err(message: string): void; - hasDebuggingEnabled(config: MonoConfig): boolean, retrieve_asset_download(asset: AssetEntry): Promise; onDownloadResourceProgress?: (resourcesLoaded: number, totalResources: number) => void; logDownloadStatsToConsole: () => void; @@ -155,6 +154,7 @@ export type LoaderHelpers = { invokeLibraryInitializers: (functionName: string, args: any[]) => Promise, libraryInitializers?: { scriptName: string, exports: any }[]; + isDebuggingSupported(): boolean, isChromium: boolean, isFirefox: boolean diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs index ef42b6fa952f1..d5dc83e4252df 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs @@ -71,6 +71,8 @@ public class GenerateWasmBootJson : Task public ITaskItem[] LazyLoadedAssemblies { get; set; } + public bool IsPublish { get; set; } + public override bool Execute() { using var fileStream = File.Create(OutputPath); @@ -101,7 +103,6 @@ public void WriteBootJson(Stream output, string entryAssemblyName) if (IsTargeting80OrLater()) { - result.debugLevel = ParseOptionalInt(DebugLevel) ?? (DebugBuild ? 1 : 0); result.mainAssemblyName = entryAssemblyName; result.globalizationMode = GetGlobalizationMode().ToString().ToLowerInvariant(); @@ -329,6 +330,20 @@ public void WriteBootJson(Stream output, string entryAssemblyName) } } + if (IsTargeting80OrLater()) + { + int? debugLevel = ParseOptionalInt(DebugLevel); + + // If user didn't give us a value, check if we have any PDB. + if (debugLevel == null && result.resources?.pdb?.Count > 0) + debugLevel = -1; + + // Fallback to -1 for build, or 0 for publish + debugLevel ??= IsPublish ? 0 : -1; + + result.debugLevel = debugLevel.Value; + } + if (ConfigurationFiles != null) { foreach (var configFile in ConfigurationFiles) From b357fd698a3c7e2d12993de236f214aa6f1fe622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Thu, 15 Feb 2024 18:07:57 +0100 Subject: [PATCH 02/10] Fix test case DebugLevelTest --- src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js b/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js index 076f37a62a6d8..e5073a01f1b45 100644 --- a/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js +++ b/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js @@ -94,6 +94,10 @@ try { case "DownloadResourceProgressTest": exit(0); break; + case "DebugLevelTest": + testOutput("WasmDebugLevel: " + config.debugLevel); + exit(0); + break; default: console.error(`Unknown test case: ${testCase}`); exit(3); From d46b0ebca07f4de28ba4148f58cf1e9d6c14ef49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 16 Feb 2024 09:30:35 +0100 Subject: [PATCH 03/10] Fix json --- .../testassets/WasmBasicTestApp/App/LazyLoadingTest.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mono/wasm/testassets/WasmBasicTestApp/App/LazyLoadingTest.cs b/src/mono/wasm/testassets/WasmBasicTestApp/App/LazyLoadingTest.cs index 5aba6b1ee48a0..797956d23f8e4 100644 --- a/src/mono/wasm/testassets/WasmBasicTestApp/App/LazyLoadingTest.cs +++ b/src/mono/wasm/testassets/WasmBasicTestApp/App/LazyLoadingTest.cs @@ -4,6 +4,7 @@ using Library; using System; using System.Text.Json; +using System.Text.Json.Serialization; using System.Runtime.InteropServices.JavaScript; public partial class LazyLoadingTest @@ -13,7 +14,12 @@ public static void Run() { // System.Text.Json is marked as lazy loaded in the csproj ("BlazorWebAssemblyLazyLoad"), this method can be called only after the assembly is lazy loaded // In the test case it is done in the JS before call to this method - var text = JsonSerializer.Serialize(new Person("John", "Doe")); + var text = JsonSerializer.Serialize(new Person("John", "Doe"), PersonJsonSerializerContext.Default.Person); TestOutput.WriteLine(text); } } + +[JsonSerializable(typeof(Person))] +public partial class PersonJsonSerializerContext : JsonSerializerContext +{ +} \ No newline at end of file From d679369a694681d6f5e338337ffcff04efeaa3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Wed, 13 Mar 2024 17:30:23 +0100 Subject: [PATCH 04/10] Add missing changes to browser targets --- .../Microsoft.NET.Sdk.WebAssembly.Browser.targets | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets b/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets index 324f36cad7957..b13b13a02bcff 100644 --- a/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets +++ b/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets @@ -212,9 +212,6 @@ Copyright (c) .NET Foundation. All rights reserved. <_BlazorWebAssemblyStartupMemoryCache>$(BlazorWebAssemblyStartupMemoryCache) <_BlazorWebAssemblyJiterpreter>$(BlazorWebAssemblyJiterpreter) <_BlazorWebAssemblyRuntimeOptions>$(BlazorWebAssemblyRuntimeOptions) - <_WasmDebugLevel>$(WasmDebugLevel) - <_WasmDebugLevel Condition="'$(_WasmDebugLevel)' == ''">0 - <_WasmDebugLevel Condition="'$(_WasmDebugLevel)' == '0' and ('$(DebuggerSupport)' == 'true' or '$(Configuration)' == 'Debug')">-1 $(OutputPath)$(PublishDirName)\ @@ -373,7 +370,7 @@ Copyright (c) .NET Foundation. All rights reserved. AssemblyPath="@(IntermediateAssembly)" Resources="@(_WasmOutputWithHash)" DebugBuild="true" - DebugLevel="$(_WasmDebugLevel)" + DebugLevel="$(WasmDebugLevel)" LinkerEnabled="false" CacheBootResources="$(BlazorCacheBootResources)" OutputPath="$(_WasmBuildBootJsonPath)" @@ -389,7 +386,8 @@ Copyright (c) .NET Foundation. All rights reserved. Extensions="@(WasmBootConfigExtension)" TargetFrameworkVersion="$(TargetFrameworkVersion)" ModuleAfterConfigLoaded="@(WasmModuleAfterConfigLoaded)" - ModuleAfterRuntimeReady="@(WasmModuleAfterRuntimeReady)" /> + ModuleAfterRuntimeReady="@(WasmModuleAfterRuntimeReady)" + IsPublish="false" /> @@ -566,7 +564,7 @@ Copyright (c) .NET Foundation. All rights reserved. AssemblyPath="@(IntermediateAssembly)" Resources="@(_WasmPublishBootResourceWithHash)" DebugBuild="false" - DebugLevel="$(_WasmDebugLevel)" + DebugLevel="$(WasmDebugLevel)" LinkerEnabled="$(PublishTrimmed)" CacheBootResources="$(BlazorCacheBootResources)" OutputPath="$(IntermediateOutputPath)blazor.publish.boot.json" @@ -582,7 +580,8 @@ Copyright (c) .NET Foundation. All rights reserved. Extensions="@(WasmBootConfigExtension)" TargetFrameworkVersion="$(TargetFrameworkVersion)" ModuleAfterConfigLoaded="@(WasmModuleAfterConfigLoaded)" - ModuleAfterRuntimeReady="@(WasmModuleAfterRuntimeReady)" /> + ModuleAfterRuntimeReady="@(WasmModuleAfterRuntimeReady)" + IsPublish="true" /> From ebd95d5d1783af159d3a33eade40fea301377dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Sun, 2 Jun 2024 13:29:03 +0200 Subject: [PATCH 05/10] Merge f190f34e7440833d5330aabfd8c1c0e5abce7902 --- ...ugLevelTests.cs => DebugLevelTestsBase.cs} | 4 +- .../WasmAppBuilderDebugLevelTests.cs | 70 +++++++++++++++++++ .../WasmSdkDebugLevelTests.cs | 46 ++++++++++++ src/mono/wasm/build/WasmApp.targets | 4 +- .../BootJsonBuilderHelper.cs | 32 ++++++++- .../GenerateWasmBootJson.cs | 24 ++----- src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 12 ++-- 7 files changed, 164 insertions(+), 28 deletions(-) rename src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/{DebugLevelTests.cs => DebugLevelTestsBase.cs} (95%) create mode 100644 src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmAppBuilderDebugLevelTests.cs create mode 100644 src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmSdkDebugLevelTests.cs diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTestsBase.cs similarity index 95% rename from src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTests.cs rename to src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTestsBase.cs index 3dfe4c467f79f..eb1ca09d90797 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTestsBase.cs @@ -13,9 +13,9 @@ namespace Wasm.Build.Tests.TestAppScenarios; -public class DebugLevelTests : AppTestBase +public class DebugLevelTestsBase : AppTestBase { - public DebugLevelTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) + public DebugLevelTestsBase(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) : base(output, buildContext) { } diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmAppBuilderDebugLevelTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmAppBuilderDebugLevelTests.cs new file mode 100644 index 0000000000000..83809b3c2a9a3 --- /dev/null +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmAppBuilderDebugLevelTests.cs @@ -0,0 +1,70 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +#nullable enable + +namespace Wasm.Build.Tests.TestAppScenarios; + +public class WasmAppBuilderDebugLevelTests : DebugLevelTestsBase +{ + public WasmAppBuilderDebugLevelTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) + : base(output, buildContext) + { + } + + protected override void SetupProject(string projectId) + { + Id = $"{projectId}_{GetRandomId()}"; + string projectfile = CreateWasmTemplateProject(Id, "wasmconsole"); + string projectDir = Path.GetDirectoryName(projectfile)!; + string mainJs = Path.Combine(projectDir, "main.mjs"); + string mainJsContent = File.ReadAllText(mainJs); + mainJsContent = mainJsContent + .Replace("import { dotnet }", "import { dotnet, exit }") + .Replace("await runMainAndExit()", "console.log('TestOutput -> WasmDebugLevel: ' + config.debugLevel); exit(0)"); + File.WriteAllText(mainJs, mainJsContent); + } + + protected override Task RunForBuild(string configuration) + { + CommandResult res = new RunCommand(s_buildEnv, _testOutput) + .WithWorkingDirectory(_projectDir!) + .ExecuteWithCapturedOutput($"run --no-silent --no-build -c {configuration}"); + + return Task.FromResult(ProcessRunOutput(res)); + } + + private RunResult ProcessRunOutput(CommandResult res) + { + var output = res.Output.Split(Environment.NewLine); + _testOutput.WriteLine($"DEBUG: parsed lines '{String.Join(", ", output)}'"); + + var prefix = "[] TestOutput -> "; + var testOutput = output + .Where(l => l.StartsWith(prefix)) + .Select(l => l.Substring(prefix.Length)) + .ToArray(); + + _testOutput.WriteLine($"DEBUG: testOutput '{String.Join(", ", testOutput)}'"); + return new RunResult(res.ExitCode, testOutput, output, []); + } + + protected override Task RunForPublish(string configuration) + { + // WasmAppBuilder does publish to the same folder as build (it overrides the output), + // and thus using dotnet run work correctly for publish as well. + CommandResult res = new RunCommand(s_buildEnv, _testOutput) + .WithWorkingDirectory(_projectDir!) + .ExecuteWithCapturedOutput($"run --no-silent --no-build -c {configuration}"); + + return Task.FromResult(ProcessRunOutput(res)); + } +} diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmSdkDebugLevelTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmSdkDebugLevelTests.cs new file mode 100644 index 0000000000000..b3d1a0316c49e --- /dev/null +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmSdkDebugLevelTests.cs @@ -0,0 +1,46 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +#nullable enable + +namespace Wasm.Build.Tests.TestAppScenarios; + +public class WasmSdkDebugLevelTests : DebugLevelTestsBase +{ + public WasmSdkDebugLevelTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) + : base(output, buildContext) + { + } + + protected override void SetupProject(string projectId) => CopyTestAsset("WasmBasicTestApp", projectId, "App"); + + protected override Task RunForBuild(string configuration) => RunSdkStyleAppForBuild(new( + Configuration: configuration, + TestScenario: "DebugLevelTest" + )); + + protected override Task RunForPublish(string configuration) => RunSdkStyleAppForPublish(new( + Configuration: configuration, + TestScenario: "DebugLevelTest" + )); + + [Theory] + [InlineData("Debug")] + [InlineData("Release")] + public async Task PublishWithDefaultLevelAndPdbs(string configuration) + { + SetupProject($"DebugLevelTests_PublishWithDefaultLevelAndPdbs_{configuration}"); + PublishProject(configuration, assertAppBundle: false, extraArgs: $"-p:CopyOutputSymbolsToPublishDirectory=true"); + + var result = await RunForPublish(configuration); + AssertDebugLevel(result, -1); + } +} diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 679ecb515042d..5de18ac8d25b4 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -207,7 +207,7 @@ + Properties="_WasmInNestedPublish_UniqueProperty_XYZ=true;;WasmBuildingForNestedPublish=true;DeployOnBuild=;_IsPublishing=;_WasmIsPublishing=$(_IsPublishing)"> @@ -417,6 +417,7 @@ <_WasmAppIncludeThreadsWorker Condition="'$(WasmEnableThreads)' == 'true' or '$(MonoWasmBuildVariant)' == 'multithread'">true <_WasmPThreadPoolSize Condition="'$(_WasmPThreadPoolSize)' == ''">-1 + <_WasmIsPublishing Condition="'$(_WasmIsPublishing)' == '' and '$(_IsPublishing)' != ''">$(_IsPublishing) @@ -442,6 +443,7 @@ ExtraConfig="@(WasmExtraConfig)" NativeAssets="@(WasmNativeAsset)" DebugLevel="$(WasmDebugLevel)" + IsPublish="$(_WasmIsPublishing)" IncludeThreadsWorker="$(_WasmAppIncludeThreadsWorker)" PThreadPoolSize="$(_WasmPThreadPoolSize)" UseWebcil="$(WasmEnableWebcil)" diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs index 07c05113ac843..748283e74e673 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs @@ -10,7 +10,7 @@ namespace Microsoft.NET.Sdk.WebAssembly { - public class BootJsonBuilderHelper(TaskLoggingHelper Log) + public class BootJsonBuilderHelper(TaskLoggingHelper Log, string DebugLevel, bool IsPublish) { public void ComputeResourcesHash(BootJsonData bootConfig) { @@ -73,5 +73,35 @@ static void AddDictionary(StringBuilder sb, Dictionary? res) return null; } + + public int GetDebugLevel(bool hasPdb) + { + int? debugLevel = ParseOptionalInt(DebugLevel); + + // If user didn't give us a value, check if we have any PDB. + if (debugLevel == null && hasPdb) + debugLevel = -1; + + // Fallback to -1 for build, or 0 for publish + debugLevel ??= IsPublish ? 0 : -1; + + return debugLevel.Value; + } + + public bool? ParseOptionalBool(string value) + { + if (string.IsNullOrEmpty(value) || !bool.TryParse(value, out var boolValue)) + return null; + + return boolValue; + } + + public int? ParseOptionalInt(string value) + { + if (string.IsNullOrEmpty(value) || !int.TryParse(value, out var intValue)) + return null; + + return intValue; + } } } diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs index d5dc83e4252df..3f12c54e07f9c 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs @@ -93,12 +93,12 @@ public override bool Execute() // Internal for tests public void WriteBootJson(Stream output, string entryAssemblyName) { - var helper = new BootJsonBuilderHelper(Log); + var helper = new BootJsonBuilderHelper(Log, DebugLevel, IsPublish); var result = new BootJsonData { resources = new ResourcesData(), - startupMemoryCache = ParseOptionalBool(StartupMemoryCache), + startupMemoryCache = helper.ParseOptionalBool(StartupMemoryCache), }; if (IsTargeting80OrLater()) @@ -128,7 +128,7 @@ public void WriteBootJson(Stream output, string entryAssemblyName) result.runtimeOptions = runtimeOptions; } - bool? jiterpreter = ParseOptionalBool(Jiterpreter); + bool? jiterpreter = helper.ParseOptionalBool(Jiterpreter); if (jiterpreter != null) { var runtimeOptions = result.runtimeOptions?.ToHashSet() ?? new HashSet(3); @@ -332,7 +332,7 @@ public void WriteBootJson(Stream output, string entryAssemblyName) if (IsTargeting80OrLater()) { - int? debugLevel = ParseOptionalInt(DebugLevel); + int? debugLevel = helper.ParseOptionalInt(DebugLevel); // If user didn't give us a value, check if we have any PDB. if (debugLevel == null && result.resources?.pdb?.Count > 0) @@ -409,22 +409,6 @@ private GlobalizationMode GetGlobalizationMode() return GlobalizationMode.Sharded; } - private static bool? ParseOptionalBool(string value) - { - if (string.IsNullOrEmpty(value) || !bool.TryParse(value, out var boolValue)) - return null; - - return boolValue; - } - - private static int? ParseOptionalInt(string value) - { - if (string.IsNullOrEmpty(value) || !int.TryParse(value, out var intValue)) - return null; - - return intValue; - } - private void AddToAdditionalResources(ITaskItem resource, Dictionary additionalResources, string resourceName, string behavior) { if (!additionalResources.ContainsKey(resourceName)) diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index 9c7d8a6799134..d5e2aa53e1486 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -27,6 +27,8 @@ public class WasmAppBuilder : WasmAppBuilderBaseTask public bool WasmIncludeFullIcuData { get; set; } public string? WasmIcuDataFileName { get; set; } public string? RuntimeAssetsLocation { get; set; } + public string? DebugLevel { get; set; } + public bool IsPublish { get; set; } // // Extra json elements to add to _framework/blazor.boot.json @@ -83,7 +85,7 @@ private GlobalizationMode GetGlobalizationMode() protected override bool ExecuteInternal() { - var helper = new BootJsonBuilderHelper(Log); + var helper = new BootJsonBuilderHelper(Log, DebugLevel!, IsPublish); if (!ValidateArguments()) return false; @@ -115,6 +117,8 @@ protected override bool ExecuteInternal() if (UseWebcil) Log.LogMessage(MessageImportance.Normal, "Converting assemblies to Webcil"); + int baseDebugLevel = helper.GetDebugLevel(false); + foreach (var assembly in _assemblies) { if (UseWebcil) @@ -133,7 +137,7 @@ protected override bool ExecuteInternal() { FileCopyChecked(assembly, Path.Combine(runtimeAssetsPath, Path.GetFileName(assembly)), "Assemblies"); } - if (DebugLevel != 0) + if (baseDebugLevel != 0) { var pdb = assembly; pdb = Path.ChangeExtension(pdb, ".pdb"); @@ -191,7 +195,7 @@ protected override bool ExecuteInternal() } bootConfig.resources.assembly[Path.GetFileName(assemblyPath)] = Utils.ComputeIntegrity(bytes); - if (DebugLevel != 0) + if (baseDebugLevel != 0) { var pdb = Path.ChangeExtension(assembly, ".pdb"); if (File.Exists(pdb)) @@ -205,7 +209,7 @@ protected override bool ExecuteInternal() } } - bootConfig.debugLevel = DebugLevel; + bootConfig.debugLevel = helper.GetDebugLevel(bootConfig.resources.pdb?.Count > 0); ProcessSatelliteAssemblies(args => { From 410719a5791de845648c8360098fc96459ae38a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Sun, 2 Jun 2024 13:33:02 +0200 Subject: [PATCH 06/10] Fix wasi --- src/mono/wasi/build/WasiApp.targets | 7 ++++++- src/tasks/WasmAppBuilder/WasmAppBuilderBaseTask.cs | 1 - src/tasks/WasmAppBuilder/wasi/WasiAppBuilder.cs | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/mono/wasi/build/WasiApp.targets b/src/mono/wasi/build/WasiApp.targets index 099ba8c3ebea2..c403cd718f55c 100644 --- a/src/mono/wasi/build/WasiApp.targets +++ b/src/mono/wasi/build/WasiApp.targets @@ -368,6 +368,11 @@ + + <_WasmOutputSymbolsToAppBundle Condition="'$(CopyOutputSymbolsToPublishDirectory)' == 'true' and '$(_IsPublishing)' == 'true'">true + <_WasmOutputSymbolsToAppBundle Condition="'$(_WasmOutputSymbolsToAppBundle)' == ''">false + + diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilderBaseTask.cs b/src/tasks/WasmAppBuilder/WasmAppBuilderBaseTask.cs index c533e404c4173..feee565ad4f6c 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilderBaseTask.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilderBaseTask.cs @@ -36,7 +36,6 @@ public abstract class WasmAppBuilderBaseTask : Task // https://github.com/dotnet/icu/tree/maint/maint-67/icu-filters public string[] IcuDataFileNames { get; set; } = Array.Empty(); - public int DebugLevel { get; set; } public ITaskItem[] SatelliteAssemblies { get; set; } = Array.Empty(); public bool HybridGlobalization { get; set; } public bool InvariantGlobalization { get; set; } diff --git a/src/tasks/WasmAppBuilder/wasi/WasiAppBuilder.cs b/src/tasks/WasmAppBuilder/wasi/WasiAppBuilder.cs index 0974904e6627b..521284dd56c77 100644 --- a/src/tasks/WasmAppBuilder/wasi/WasiAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/wasi/WasiAppBuilder.cs @@ -11,6 +11,7 @@ namespace Microsoft.WebAssembly.Build.Tasks; public class WasiAppBuilder : WasmAppBuilderBaseTask { public bool IsSingleFileBundle { get; set; } + public bool OutputSymbolsToAppBundle { get; set; } protected override bool ValidateArguments() { @@ -53,7 +54,7 @@ protected override bool ExecuteInternal() { FileCopyChecked(assembly, Path.Combine(asmRootPath, Path.GetFileName(assembly)), "Assemblies"); - if (DebugLevel != 0) + if (OutputSymbolsToAppBundle) { string pdb = Path.ChangeExtension(assembly, ".pdb"); if (File.Exists(pdb)) From f4776adc39792f2973bbc941f81d0b35959e62d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 14 Jun 2024 14:39:15 +0200 Subject: [PATCH 07/10] Fix --- .../TestAppScenarios/AppTestBase.cs | 8 +-- .../TestAppScenarios/DebugLevelTestsBase.cs | 61 ++++++------------- .../TestAppScenarios/ModuleConfigTests.cs | 20 ------ .../WasmAppBuilderDebugLevelTests.cs | 7 +-- .../WasmSdkDebugLevelTests.cs | 4 +- 5 files changed, 28 insertions(+), 72 deletions(-) diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppTestBase.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppTestBase.cs index 771daff1f5d46..3b3baf581c185 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppTestBase.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/AppTestBase.cs @@ -36,15 +36,15 @@ protected void CopyTestAsset(string assetName, string generatedProjectNamePrefix _projectDir = Path.Combine(_projectDir!, "App"); } - protected void BuildProject(string configuration, params string[] extraArgs) + protected void BuildProject(string configuration, bool assertAppBundle = true, params string[] extraArgs) { - (CommandResult result, _) = BlazorBuild(new BlazorBuildOptions(Id, configuration), extraArgs); + (CommandResult result, _) = BlazorBuild(new BlazorBuildOptions(Id, configuration, AssertAppBundle: assertAppBundle), extraArgs); result.EnsureSuccessful(); } - protected void PublishProject(string configuration, params string[] extraArgs) + protected void PublishProject(string configuration, bool assertAppBundle = true, params string[] extraArgs) { - (CommandResult result, _) = BlazorPublish(new BlazorBuildOptions(Id, configuration), extraArgs); + (CommandResult result, _) = BlazorPublish(new BlazorBuildOptions(Id, configuration, AssertAppBundle: assertAppBundle), extraArgs); result.EnsureSuccessful(); } diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTestsBase.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTestsBase.cs index eb1ca09d90797..219b8b2ed6714 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTestsBase.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/DebugLevelTestsBase.cs @@ -13,14 +13,14 @@ namespace Wasm.Build.Tests.TestAppScenarios; -public class DebugLevelTestsBase : AppTestBase +public abstract class DebugLevelTestsBase : AppTestBase { public DebugLevelTestsBase(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) : base(output, buildContext) { } - private void AssertDebugLevel(RunResult result, int value) + protected void AssertDebugLevel(RunResult result, int value) { Assert.Collection( result.TestOutput, @@ -28,18 +28,19 @@ private void AssertDebugLevel(RunResult result, int value) ); } + protected abstract void SetupProject(string projectId); + protected abstract Task RunForBuild(string configuration); + protected abstract Task RunForPublish(string configuration); + [Theory] [InlineData("Debug")] [InlineData("Release")] public async Task BuildWithDefaultLevel(string configuration) { - CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_BuildWithDefaultLevel_{configuration}"); - BuildProject(configuration); + SetupProject($"DebugLevelTests_BuildWithDefaultLevel_{configuration}"); + BuildProject(configuration, assertAppBundle: false); - var result = await RunSdkStyleAppForBuild(new( - Configuration: configuration, - TestScenario: "DebugLevelTest" - )); + var result = await RunForBuild(configuration); AssertDebugLevel(result, -1); } @@ -50,13 +51,10 @@ public async Task BuildWithDefaultLevel(string configuration) [InlineData("Release", 0)] public async Task BuildWithExplicitValue(string configuration, int debugLevel) { - CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_BuildWithExplicitValue_{configuration}"); - BuildProject(configuration, $"-p:WasmDebugLevel={debugLevel}"); + SetupProject($"DebugLevelTests_BuildWithExplicitValue_{configuration}"); + BuildProject(configuration, assertAppBundle: false, extraArgs: $"-p:WasmDebugLevel={debugLevel}"); - var result = await RunSdkStyleAppForBuild(new( - Configuration: configuration, - TestScenario: "DebugLevelTest" - )); + var result = await RunForBuild(configuration); AssertDebugLevel(result, debugLevel); } @@ -65,13 +63,10 @@ public async Task BuildWithExplicitValue(string configuration, int debugLevel) [InlineData("Release")] public async Task PublishWithDefaultLevel(string configuration) { - CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_PublishWithDefaultLevel_{configuration}"); - PublishProject(configuration); + SetupProject($"DebugLevelTests_PublishWithDefaultLevel_{configuration}"); + PublishProject(configuration, assertAppBundle: false); - var result = await RunSdkStyleAppForPublish(new( - Configuration: configuration, - TestScenario: "DebugLevelTest" - )); + var result = await RunForPublish(configuration); AssertDebugLevel(result, 0); } @@ -82,28 +77,10 @@ public async Task PublishWithDefaultLevel(string configuration) [InlineData("Release", -1)] public async Task PublishWithExplicitValue(string configuration, int debugLevel) { - CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_PublishWithExplicitValue_{configuration}"); - PublishProject(configuration, $"-p:WasmDebugLevel={debugLevel}"); + SetupProject($"DebugLevelTests_PublishWithExplicitValue_{configuration}"); + PublishProject(configuration, assertAppBundle: false, extraArgs: $"-p:WasmDebugLevel={debugLevel}"); - var result = await RunSdkStyleAppForPublish(new( - Configuration: configuration, - TestScenario: "DebugLevelTest" - )); + var result = await RunForPublish(configuration); AssertDebugLevel(result, debugLevel); } - - [Theory] - [InlineData("Debug")] - [InlineData("Release")] - public async Task PublishWithDefaultLevelAndPdbs(string configuration) - { - CopyTestAsset("WasmBasicTestApp", $"DebugLevelTests_PublishWithDefaultLevelAndPdbs_{configuration}"); - PublishProject(configuration, $"-p:CopyOutputSymbolsToPublishDirectory=true"); - - var result = await RunSdkStyleAppForPublish(new( - Configuration: configuration, - TestScenario: "DebugLevelTest" - )); - AssertDebugLevel(result, -1); - } -} +} \ No newline at end of file diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/ModuleConfigTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/ModuleConfigTests.cs index 72bc5e4b04035..bc2b5631d2373 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/ModuleConfigTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/ModuleConfigTests.cs @@ -54,24 +54,4 @@ public async Task DownloadProgressFinishes(bool failAssemblyDownload) : "The download progress test did emit unexpected message about failing download" ); } - - [Fact] - public async Task OutErrOverrideWorks() - { - CopyTestAsset("WasmBasicTestApp", $"ModuleConfigTests_OutErrOverrideWorks"); - PublishProject("Debug"); - - var result = await RunSdkStyleApp(new( - Configuration: "Debug", - TestScenario: "OutErrOverrideWorks" - )); - Assert.True( - result.ConsoleOutput.Any(m => m.Contains("Emscripten out override works!")), - "Emscripten out override doesn't work" - ); - Assert.True( - result.ConsoleOutput.Any(m => m.Contains("Emscripten err override works!")), - "Emscripten err override doesn't work" - ); - } } diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmAppBuilderDebugLevelTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmAppBuilderDebugLevelTests.cs index 83809b3c2a9a3..60fb3d86f7d0e 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmAppBuilderDebugLevelTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmAppBuilderDebugLevelTests.cs @@ -23,13 +23,12 @@ public WasmAppBuilderDebugLevelTests(ITestOutputHelper output, SharedBuildPerTes protected override void SetupProject(string projectId) { Id = $"{projectId}_{GetRandomId()}"; - string projectfile = CreateWasmTemplateProject(Id, "wasmconsole"); + string projectfile = CreateWasmTemplateProject(Id, "wasmconsole", extraArgs: "-f net8.0"); string projectDir = Path.GetDirectoryName(projectfile)!; string mainJs = Path.Combine(projectDir, "main.mjs"); string mainJsContent = File.ReadAllText(mainJs); mainJsContent = mainJsContent - .Replace("import { dotnet }", "import { dotnet, exit }") - .Replace("await runMainAndExit()", "console.log('TestOutput -> WasmDebugLevel: ' + config.debugLevel); exit(0)"); + .Replace("await dotnet.run()", "console.log('TestOutput -> WasmDebugLevel: ' + config.debugLevel); exit(0)"); File.WriteAllText(mainJs, mainJsContent); } @@ -54,7 +53,7 @@ private RunResult ProcessRunOutput(CommandResult res) .ToArray(); _testOutput.WriteLine($"DEBUG: testOutput '{String.Join(", ", testOutput)}'"); - return new RunResult(res.ExitCode, testOutput, output, []); + return new RunResult(res.ExitCode, testOutput, output); } protected override Task RunForPublish(string configuration) diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmSdkDebugLevelTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmSdkDebugLevelTests.cs index b3d1a0316c49e..f88bb3c7239c9 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmSdkDebugLevelTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/WasmSdkDebugLevelTests.cs @@ -20,7 +20,7 @@ public WasmSdkDebugLevelTests(ITestOutputHelper output, SharedBuildPerTestClassF { } - protected override void SetupProject(string projectId) => CopyTestAsset("WasmBasicTestApp", projectId, "App"); + protected override void SetupProject(string projectId) => CopyTestAsset("WasmBasicTestApp", projectId); protected override Task RunForBuild(string configuration) => RunSdkStyleAppForBuild(new( Configuration: configuration, @@ -38,7 +38,7 @@ protected override Task RunForPublish(string configuration) => RunSdk public async Task PublishWithDefaultLevelAndPdbs(string configuration) { SetupProject($"DebugLevelTests_PublishWithDefaultLevelAndPdbs_{configuration}"); - PublishProject(configuration, assertAppBundle: false, extraArgs: $"-p:CopyOutputSymbolsToPublishDirectory=true"); + PublishProject(configuration, extraArgs: $"-p:CopyOutputSymbolsToPublishDirectory=true"); var result = await RunForPublish(configuration); AssertDebugLevel(result, -1); From 3c914b661afe2d2812c172099bb4992e92b12e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 21 Jun 2024 07:30:43 +0200 Subject: [PATCH 08/10] Fix flaky ness of DownloadProgressFinishes test --- .../wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js b/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js index d5d4a26f7213c..5092e4cb945b7 100644 --- a/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js +++ b/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js @@ -33,6 +33,8 @@ switch (testCase) { Math.floor(Math.random() * 5) + 5, Math.floor(Math.random() * 5) + 10 ]; + console.log(`Failing test at assembly indexes [${failAtAssemblyNumbers.join(", ")}]`); + let alreadyFailed = []; dotnet.withDiagnosticTracing(true).withResourceLoader((type, name, defaultUri, integrity, behavior) => { if (type === "dotnetjs") { // loadBootResource could return string with unqualified name of resource. @@ -45,9 +47,10 @@ switch (testCase) { } assemblyCounter++; - if (!failAtAssemblyNumbers.includes(assemblyCounter)) + if (!failAtAssemblyNumbers.includes(assemblyCounter) || alreadyFailed.includes(defaultUri)) return defaultUri; + alreadyFailed.push(defaultUri); testOutput("Throw error instead of downloading resource"); const error = new Error("Simulating a failed fetch"); error.silent = true; From f18a05274470c3d24ac77105ad8c4a8d29c53cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 12 Jul 2024 14:59:16 +0200 Subject: [PATCH 09/10] [browser] default WasmDebugLevel to 0 for tests (#104361) --- eng/testing/tests.wasm.targets | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/eng/testing/tests.wasm.targets b/eng/testing/tests.wasm.targets index 4dc6b71005449..9e0fdaf3289e6 100644 --- a/eng/testing/tests.wasm.targets +++ b/eng/testing/tests.wasm.targets @@ -15,10 +15,7 @@ But we do want to set it for Configuration=Debug . --> -1 - - reset-to-zero + 0 full true From dc6da10a8afe1095500bb937a71c0565e1095c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Sat, 13 Jul 2024 22:39:49 +0200 Subject: [PATCH 10/10] Update BuildWasmAppsJobsList.txt --- eng/testing/scenarios/BuildWasmAppsJobsList.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eng/testing/scenarios/BuildWasmAppsJobsList.txt b/eng/testing/scenarios/BuildWasmAppsJobsList.txt index 787df10795e1c..9bb06b872bb6c 100644 --- a/eng/testing/scenarios/BuildWasmAppsJobsList.txt +++ b/eng/testing/scenarios/BuildWasmAppsJobsList.txt @@ -43,4 +43,5 @@ Wasm.Build.Tests.WasmTemplateTests Wasm.Build.Tests.WorkloadTests Wasm.Build.Tests.TestAppScenarios.ModuleConfigTests Wasm.Build.Tests.MT.Blazor.SimpleMultiThreadedTests -Wasm.Build.Tests.TestAppScenarios.DebugLevelTests +Wasm.Build.Tests.TestAppScenarios.WasmSdkDebugLevelTests +Wasm.Build.Tests.TestAppScenarios.WasmAppBuilderDebugLevelTests