Skip to content

Commit

Permalink
[wasm] Move some defaults out of the templates (#72626)
Browse files Browse the repository at this point in the history
* Move some defaults out of the template
* Fix build for template tests, and satellite assembly tests

- Running with `node` fails when relinking, or AOT'ing in Debug config
- this seems to be when linking is done with `-O0`
  - it seems to work fine with emsdk `3.1.7` though
  - For `Debug` config we automatically set `_WasmDevel=true`, which
  then sets `-O0`

  - This is not seen on CI, since we don't run non-template tests in
  `Debug`. But for local runs, we do.
  - The template tests are explicitly run for Debug, and Release, and
  they break because of this
  - So, setting `_WasmDevel=false` for now, till this gets fixed

- Also, set `OutputType=Exe` in the project file itself, so it gets set
before `Microsoft.NET.Sdk` gets imported, or early enough.

* Wasm.Build.Tests: add relinking tests for templates

* Revert unintended change

* Fix WasmTemplateTests on windows

* Wasm.Build.Tests: Explicitly allow web server port in chrome, on windows

Fixes #72436

Co-authored-by: Ankit Jain <[email protected]>
  • Loading branch information
lewing and radical authored Jul 25, 2022
1 parent 193fd53 commit ed2a5a1
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ProjectReference Include="$(RepoRoot)src\mono\wasm\host\WasmAppHost.csproj" />

<PackageFile Include="Sdk\AutoImport.props" TargetPath="Sdk" />
<PackageFile Include="Sdk\Sdk.props" TargetPath="Sdk" />
<PackageFile Include="$(RepoRoot)\src\mono\wasm\build\WasmApp.props" TargetPath="Sdk" />
<PackageFile Include="$(RepoRoot)\src\mono\wasm\build\WasmApp.targets" TargetPath="Sdk" />
<PackageFile Include="$(RepoRoot)\src\mono\wasm\build\WasmApp.Native.*" TargetPath="Sdk" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<PropertyGroup>
<WasmNativeWorkload>true</WasmNativeWorkload>
</PropertyGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>
<PropertyGroup Condition="'$(UsingMicrosoftNETSdkBlazorWebAssembly)' != 'true'">
<TargetArchitecture>wasm</TargetArchitecture>
<TargetOS>browser</TargetOS>
<UseMonoRuntime>true</UseMonoRuntime>
<OutputType>Exe</OutputType>
<PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<WasmAppBuilderTasksAssemblyPath>$(_TasksDir)WasmAppBuilder.dll</WasmAppBuilderTasksAssemblyPath>
<WasmBuildTasksAssemblyPath>$(_TasksDir)WasmBuildTasks.dll</WasmBuildTasksAssemblyPath>
<WasmAppHostDir>$([MSBuild]::NormalizeDirectory($(MSBuildThisFileDirectory), '..', 'WasmAppHost'))</WasmAppHostDir>

<!-- only for non-blazor projects -->
<WasmEmitSymbolMap Condition="'$(UsingMicrosoftNETSdkBlazorWebAssembly)' != 'true' and '$(WasmEmitSymbolMap)' == '' and '$(RunAOTCompilation)' != 'true'">true</WasmEmitSymbolMap>
</PropertyGroup>

<Import Project="$(MSBuildThisFileDirectory)\WasmApp.props" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

<ImportGroup Condition="'$(RuntimeIdentifier)' == 'browser-wasm' and '$(UsingBrowserRuntimeWorkload)' == 'true'">
<Import Project="Sdk.props" Sdk="Microsoft.NET.Runtime.MonoTargets.Sdk" />
<Import Project="Sdk.props" Sdk="Microsoft.NET.Runtime.WebAssembly.Sdk" />
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Runtime.WebAssembly.Sdk" />
<Import Project="Sdk.props" Sdk="Microsoft.NETCore.App.Runtime.AOT.Cross.browser-wasm" />
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Runtime.MonoTargets.Sdk" />
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@

<_DriverGenCNeeded Condition="'$(_DriverGenCNeeded)' == '' and '$(_WasmShouldAOT)' == 'true'">true</_DriverGenCNeeded>

<_WasmDevel Condition="'$(WasmBuildNative)' == 'true' and '$(Configuration)' == 'Debug'">true</_WasmDevel>
<_WasmDevel Condition="'$(_WasmDevel)' == '' and '$(WasmBuildNative)' == 'true' and '$(Configuration)' == 'Debug'">true</_WasmDevel>

<_EmccAssertionLevelDefault Condition="'$(_EmccAssertionLevelDefault)' == ''">0</_EmccAssertionLevelDefault>
<_EmccOptimizationFlagDefault Condition="'$(_WasmDevel)' == 'true'">-O0 -s ASSERTIONS=$(_EmccAssertionLevelDefault)</_EmccOptimizationFlagDefault>
<_EmccOptimizationFlagDefault Condition="'$(_WasmDevel)' == 'true'">-O0</_EmccOptimizationFlagDefault>
<_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == '' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">-O1</_EmccOptimizationFlagDefault>
<_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == ''">-Oz</_EmccOptimizationFlagDefault>

Expand Down Expand Up @@ -207,6 +207,7 @@

<!-- Adding optimization flag at the top, so it gets precedence -->
<_EmccCFlags Include="$(EmccCompileOptimizationFlag)" />
<_EmccCFlags Include="-s ASSERTIONS=$(_EmccAssertionLevelDefault)" Condition="'$(_WasmDevel)' == 'true'" />
<_EmccCFlags Include="@(_EmccCommonFlags)" />

<_EmccCFlags Include="-DDISABLE_PERFTRACING_LISTEN_PORTS=1" />
Expand All @@ -224,6 +225,7 @@

<!-- Adding optimization flag at the top, so it gets precedence -->
<_EmccLDFlags Include="$(EmccLinkOptimizationFlag)" />
<_EmccLDFlags Include="-s ASSERTIONS=$(_EmccAssertionLevelDefault)" Condition="'$(_WasmDevel)' == 'true'" />
<_EmccLDFlags Include="@(_EmccCommonFlags)" />
<_EmccLDFlags Include="-Wl,--allow-undefined" />
<_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" />
Expand Down
5 changes: 0 additions & 5 deletions src/mono/wasm/templates/templates/browser/browser.0.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetArchitecture>wasm</TargetArchitecture>
<TargetOS>browser</TargetOS>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<UseMonoRuntime>true</UseMonoRuntime>
<WasmMainJSPath>main.js</WasmMainJSPath>
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<WasmEmitSymbolMap Condition="'$(RunAOTCompilation)' != 'true'">true</WasmEmitSymbolMap>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
5 changes: 0 additions & 5 deletions src/mono/wasm/templates/templates/console/console.0.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetArchitecture>wasm</TargetArchitecture>
<TargetOS>browser</TargetOS>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<UseMonoRuntime>true</UseMonoRuntime>
<WasmMainJSPath>main.mjs</WasmMainJSPath>
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<WasmEmitSymbolMap Condition="'$(RunAOTCompilation)' != 'true'">true</WasmEmitSymbolMap>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
6 changes: 5 additions & 1 deletion src/tests/BuildWasmApps/Wasm.Build.Tests/BrowserRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ public async Task<IPage> RunAsync(ToolCommand cmd, string args, bool headless =
if (!urlAvailable.Task.IsCompleted)
throw new Exception("Timed out waiting for the app host url");

var url = new Uri(urlAvailable.Task.Result);
Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{
ExecutablePath = s_chromePath.Value,
Headless = headless
Headless = headless,
Args = OperatingSystem.IsWindows()
? new[] { $"--explicitly-allowed-ports={url.Port}" }
: Array.Empty<string>()
});

IPage page = await Browser.NewPageAsync();
Expand Down
11 changes: 11 additions & 0 deletions src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,17 @@ public string CreateWasmTemplateProject(string id, string template = "wasmbrowse
{
InitPaths(id);
InitProjectDir(id);

File.WriteAllText(Path.Combine(_projectDir, "Directory.Build.props"), "<Project />");
File.WriteAllText(Path.Combine(_projectDir, "Directory.Build.targets"),
"""
<Project>
<ItemGroup>
<EmscriptenEnvVars Include="FROZEN_CACHE=" Condition="'$(OS)' == 'Windows_NT'" />
</ItemGroup>
</Project>
""");

new DotNetCommand(s_buildEnv, _testOutput, useDefaultArgs: false)
.WithWorkingDirectory(_projectDir!)
.ExecuteWithCapturedOutput($"new {template}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ public void ResourcesFromMainAssembly(BuildArgs buildArgs,
// Release+publish defaults to native relinking
bool dotnetWasmFromRuntimePack = !nativeRelink && !buildArgs.AOT && buildArgs.Config != "Release";

string extraProperties = (nativeRelink ? $"<WasmBuildNative>true</WasmBuildNative>" : string.Empty)
// make ASSERTIONS=1 so that we test with it
+ $"<EmccCompileOptimizationFlag>-O0 -sASSERTIONS=1</EmccCompileOptimizationFlag>"
+ $"<EmccLinkOptimizationFlag>-O1</EmccLinkOptimizationFlag>";

buildArgs = buildArgs with { ProjectName = projectName };
buildArgs = ExpandBuildArgs(buildArgs,
projectTemplate: s_resourcesProjectTemplate,
extraProperties: (nativeRelink ? $"<WasmBuildNative>true</WasmBuildNative>" : string.Empty)
// make ASSERTIONS=1 so that we test with it
+ $"<EmccCompileOptimizationFlag>{ (buildArgs.Config == "Debug" ? "-O0 -sASSERTIONS=1" : "-O0 -sASSERTIONS=1")}</EmccCompileOptimizationFlag>"
);
extraProperties: extraProperties);

BuildProject(buildArgs,
id: id,
Expand All @@ -65,7 +67,7 @@ public void ResourcesFromMainAssembly(BuildArgs buildArgs,
args: argCulture,
host: host, id: id,
// check that downloading assets doesn't have timing race conditions
extraXHarnessMonoArgs: "--fetch-random-delay=200");
extraXHarnessMonoArgs: host is RunHost.Chrome ? "--fetch-random-delay=200" : string.Empty);
}

[Theory]
Expand All @@ -81,10 +83,15 @@ public void ResourcesFromProjectReference(BuildArgs buildArgs,
string projectName = $"SatelliteAssemblyFromProjectRef";
bool dotnetWasmFromRuntimePack = !nativeRelink && !buildArgs.AOT;

string extraProperties = $"<WasmBuildNative>{(nativeRelink ? "true" : "false")}</WasmBuildNative>"
// make ASSERTIONS=1 so that we test with it
+ $"<EmccCompileOptimizationFlag>-O0 -sASSERTIONS=1</EmccCompileOptimizationFlag>"
+ $"<EmccLinkOptimizationFlag>-O1</EmccLinkOptimizationFlag>";

buildArgs = buildArgs with { ProjectName = projectName };
buildArgs = ExpandBuildArgs(buildArgs,
projectTemplate: s_resourcesProjectTemplate,
extraProperties: $"<WasmBuildNative>{(nativeRelink ? "true" : "false")}</WasmBuildNative>",
extraProperties: extraProperties,
extraItems: $"<ProjectReference Include=\"..\\LibraryWithResources\\LibraryWithResources.csproj\" />");

BuildProject(buildArgs,
Expand All @@ -97,8 +104,18 @@ public void ResourcesFromProjectReference(BuildArgs buildArgs,
_projectDir = Path.Combine(rootDir, projectName);
Directory.CreateDirectory(_projectDir);
Utils.DirectoryCopy(Path.Combine(BuildEnvironment.TestAssetsPath, "SatelliteAssemblyFromProjectRef"), rootDir);
Utils.DirectoryCopy(Path.Combine(BuildEnvironment.TestAssetsPath, projectName), rootDir);
// D.B.* used for wasm projects should be moved next to the wasm project, so it doesn't
// affect the non-wasm library project
File.Move(Path.Combine(rootDir, "Directory.Build.props"), Path.Combine(_projectDir, "Directory.Build.props"));
File.Move(Path.Combine(rootDir, "Directory.Build.targets"), Path.Combine(_projectDir, "Directory.Build.targets"));
CreateProgramForCultureTest(_projectDir, "LibraryWithResources.resx.words", "LibraryWithResources.Class1");
// The root D.B* should be empty
File.WriteAllText(Path.Combine(rootDir, "Directory.Build.props"), "<Project />");
File.WriteAllText(Path.Combine(rootDir, "Directory.Build.targets"), "<Project />");
}));

RunAndTestWasmApp(buildArgs,
Expand Down
41 changes: 28 additions & 13 deletions src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,35 @@ public void ConsoleBuildThenPublish(string config)
}

[ConditionalTheory(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
[InlineData("Debug")]
[InlineData("Release")]
public void ConsoleBuildAndRun(string config)
[InlineData("Debug", false)]
[InlineData("Debug", true)]
[InlineData("Release", false)]
[InlineData("Release", true)]
public void ConsoleBuildAndRun(string config, bool relinking)
{
string id = $"{config}_{Path.GetRandomFileName()}";
string projectFile = CreateWasmTemplateProject(id, "wasmconsole");
string projectName = Path.GetFileNameWithoutExtension(projectFile);

updateProgramCS();
if (relinking)
AddItemsPropertiesToProject(projectFile, "<WasmBuildNative>true</WasmBuildNative>");

var buildArgs = new BuildArgs(projectName, config, false, id, null);
buildArgs = ExpandBuildArgs(buildArgs);

BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
DotnetWasmFromRuntimePack: true,
DotnetWasmFromRuntimePack: !relinking,
CreateProject: false,
HasV8Script: false,
MainJS: "main.mjs",
Publish: false,
TargetFramework: "net7.0"
));

AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: true);
AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: !relinking);

(int exitCode, string output) = RunProcess(s_buildEnv.DotNet, _testOutput, args: $"run --no-build -c {config} x y z", workingDir: _projectDir);
Assert.Equal(42, exitCode);
Expand All @@ -167,25 +171,28 @@ public void ConsoleBuildAndRun(string config)
}

[ConditionalTheory(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
[InlineData("Debug", false)]
[InlineData("Debug", true)]
[InlineData("Release", false)]
[InlineData("Release", true)]
public void ConsolePublishAndRun(string config, bool aot)
[InlineData("Debug", false, false)]
[InlineData("Debug", false, true)]
[InlineData("Debug", true, false)]
[InlineData("Release", false, false)] // Release relinks by default
[InlineData("Release", true, false)]
public void ConsolePublishAndRun(string config, bool aot, bool relinking)
{
string id = $"{config}_{Path.GetRandomFileName()}";
string projectFile = CreateWasmTemplateProject(id, "wasmconsole");
string projectName = Path.GetFileNameWithoutExtension(projectFile);

updateProgramCS();

if (aot)
AddItemsPropertiesToProject(projectFile, "<RunAOTCompilation>true</RunAOTCompilation>");
else if (relinking)
AddItemsPropertiesToProject(projectFile, "<WasmBuildNative>true</WasmBuildNative>");

var buildArgs = new BuildArgs(projectName, config, aot, id, null);
buildArgs = ExpandBuildArgs(buildArgs);

bool expectRelinking = config == "Release" || aot;
bool expectRelinking = config == "Release" || aot || relinking;
BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
Expand All @@ -197,7 +204,15 @@ public void ConsolePublishAndRun(string config, bool aot)
TargetFramework: "net7.0",
UseCache: false));

AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: !expectRelinking);
if (!aot)
{
// These are disabled for AOT explicitly
AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: !expectRelinking);
}
else
{
AssertFilesDontExist(Path.Combine(GetBinDir(config), "AppBundle"), new[] { "dotnet.js.symbols" });
}

// FIXME: pass envvars via the environment, once that is supported
string runArgs = $"run --no-build -c {config}";
Expand Down

0 comments on commit ed2a5a1

Please sign in to comment.