Skip to content

Fix Blazor WASM SDK bugs #24172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ public class WasmBuildIntegrationTest
{
private static readonly string DotNetJsFileName = $"dotnet.{BuildVariables.MicrosoftNETCoreAppRuntimeVersion}.js";

[Fact]
public async Task BuildMinimal_Works()
{
// Arrange
// Minimal has no project references, service worker etc. This is pretty close to the project template.
using var project = ProjectDirectory.Create("blazorwasm-minimal");
var result = await MSBuildProcessManager.DotnetMSBuild(project);

Assert.BuildPassed(result);

var buildOutputDirectory = project.BuildOutputDirectory;

Assert.FileExists(result, buildOutputDirectory, "wwwroot", "_framework", "blazor.boot.json");
Assert.FileExists(result, buildOutputDirectory, "wwwroot", "_framework", "blazor.webassembly.js");
Assert.FileExists(result, buildOutputDirectory, "wwwroot", "_framework", "dotnet.wasm");
Assert.FileExists(result, buildOutputDirectory, "wwwroot", "_framework", "dotnet.wasm.gz");
Assert.FileExists(result, buildOutputDirectory, "wwwroot", "_framework", DotNetJsFileName);
Assert.FileExists(result, buildOutputDirectory, "wwwroot", "_framework", "blazorwasm-minimal.dll");

var staticWebAssets = Assert.FileExists(result, buildOutputDirectory, "blazorwasm-minimal.StaticWebAssets.xml");
Assert.FileContains(result, staticWebAssets, Path.Combine(project.TargetFramework, "wwwroot"));
}

[Fact]
public async Task Build_Works()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ public class WasmPublishIntegrationTest
{
private static readonly string DotNetJsFileName = $"dotnet.{BuildVariables.MicrosoftNETCoreAppRuntimeVersion}.js";

[Fact]
public async Task Publish_MinimalApp_Works()
{
// Arrange
using var project = ProjectDirectory.Create("blazorwasm-minimal");
var result = await MSBuildProcessManager.DotnetMSBuild(project, "Publish");

Assert.BuildPassed(result);

var publishDirectory = project.PublishOutputDirectory;

var blazorPublishDirectory = Path.Combine(publishDirectory, "wwwroot");

Assert.FileExists(result, blazorPublishDirectory, "_framework", "blazor.boot.json");
Assert.FileExists(result, blazorPublishDirectory, "_framework", "blazor.webassembly.js");
Assert.FileExists(result, blazorPublishDirectory, "_framework", "dotnet.wasm");
Assert.FileExists(result, blazorPublishDirectory, "_framework", DotNetJsFileName);
Assert.FileExists(result, blazorPublishDirectory, "_framework", "blazorwasm-minimal.dll");

// Verify static assets are in the publish directory
Assert.FileExists(result, blazorPublishDirectory, "index.html");

// Verify web.config
Assert.FileExists(result, publishDirectory, "web.config");
Assert.FileCountEquals(result, 1, publishDirectory, "*", SearchOption.TopDirectoryOnly);

VerifyBootManifestHashes(result, blazorPublishDirectory);
}

[Fact]
public async Task Publish_WithDefaultSettings_Works()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Copyright (c) .NET Foundation. All rights reserved.

<PropertyGroup>
<!-- Paths to tools, tasks, and extensions are calculated relative to the BlazorWebAssemblySdkDirectoryRoot. This can be modified to test a local build. -->
<BlazorWebAssemblySdkDirectoryRoot Condition="'$(BlazorWebAssemblySdkDirectoryRoot)'==''">$(MSBuildThisFileDirectory)..\..\</BlazorWebAssemblySdkDirectoryRoot>
<BlazorWebAssemblySdkDirectoryRoot Condition="'$(BlazorWebAssemblySdkDirectoryRoot)'==''">$(MSBuildThisFileDirectory)..\</BlazorWebAssemblySdkDirectoryRoot>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our integration tests run from the build output and do not exercise this code path. Our templates will once we have a .NET SDK that includes the Blazor SDK

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our templates will once we have a .NET SDK that includes the Blazor SDK

When will we be doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to insert the Blazor SDK -> .NET SDK -> Installer -> update our global.json to use this build -> Golden!

<_BlazorWebAssemblySdkTasksTFM Condition=" '$(MSBuildRuntimeType)' == 'Core'">net5.0</_BlazorWebAssemblySdkTasksTFM>
<_BlazorWebAssemblySdkTasksTFM Condition=" '$(MSBuildRuntimeType)' != 'Core'">net46</_BlazorWebAssemblySdkTasksTFM>
<_BlazorWebAssemblySdkTasksAssembly>$(BlazorWebAssemblySdkDirectoryRoot)tasks\$(_BlazorWebAssemblySdkTasksTFM)\Microsoft.NET.Sdk.BlazorWebAssembly.Tasks.dll</_BlazorWebAssemblySdkTasksAssembly>
Expand Down Expand Up @@ -69,7 +69,7 @@ Copyright (c) .NET Foundation. All rights reserved.

</PropertyGroup>

<Import Project="Microsoft.NET.Sdk.BlazorWebAssembly.ServiceWorkerAssetsManifest.targets" />
<Import Project="Microsoft.NET.Sdk.BlazorWebAssembly.ServiceWorkerAssetsManifest.targets" Condition="'$(ServiceWorkerAssetsManifest)' != ''" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our test app always had a service worker. Importing this was causing issues in a project that did not have a service worker. I added build and publish tests for an app that's closer to our standalone template.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: what issues did it cause for projects that didn't have service workers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The targets unconditionally run when the file is imported: https://github.com/dotnet/aspnetcore/blob/master/src/Components/WebAssembly/Sdk/src/targets/Microsoft.NET.Sdk.BlazorWebAssembly.ServiceWorkerAssetsManifest.targets#L16. This results in spooky errors because it assumes a manifest file exists


<Target Name="_ScrambleDotnetJsFileName" AfterTargets="ResolveRuntimePackAssets">
<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ProjectReference Include="..\blazorhosted\blazorhosted.csproj" />
<ProjectReference Include="..\blazorhosted-rid\blazorhosted-rid.csproj" />
<ProjectReference Include="..\blazorwasm\blazorwasm.csproj" />
<ProjectReference Include="..\blazorwasm-minimal\blazorwasm-minimal.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData"/>
</Found>
<NotFound>
<p>Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace standalone
{
public class Program
{
public static void Main(string[] args)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@using Microsoft.AspNetCore.Components.Routing
@using standalone
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<Import Project="$(RepoRoot)src\Components\WebAssembly\Sdk\src\Sdk\Sdk.props" />

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<RazorSdkDirectoryRoot>$(RazorSdkArtifactsDirectory)$(Configuration)\sdk-output\</RazorSdkDirectoryRoot>
<BlazorWebAssemblySdkDirectoryRoot>$(BlazorWebAssemblySdkArtifactsDirectory)$(Configuration)\sdk-output\</BlazorWebAssemblySdkDirectoryRoot>
</PropertyGroup>

<!-- Test Placeholder -->

<PropertyGroup Condition="'$(RunningAsTest)' == ''">
<!-- We don't want to run build server when not running as tests. -->
<UseRazorBuildServer>false</UseRazorBuildServer>
</PropertyGroup>

<PropertyGroup Condition="'$(BinariesRoot)'==''">
<!-- In test scenarios $(BinariesRoot) is defined in a generated Directory.Build.props file -->
<BinariesRoot>$(RepoRoot)artifacts\bin\Microsoft.AspNetCore.Razor.Test.MvcShim.ClassLib\$(Configuration)\netstandard2.0\</BinariesRoot>
</PropertyGroup>

<!-- DO NOT add addition references here. This is meant to simulate a non-MVC library -->
<ItemGroup Condition="'$(BinariesRoot)'!=''">
<Reference Include="$(BinariesRoot)\Microsoft.AspNetCore.Razor.Test.ComponentShim.dll"/>
</ItemGroup>

<Import Project="$(RepoRoot)src\Components\WebAssembly\Sdk\src\Sdk\Sdk.targets" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.build { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>standalone</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
</head>

<body>
<app>Loading...</app>

<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.webassembly.js"></script>
</body>

</html>