Skip to content

Commit

Permalink
[wasm] Allow selecting which target to run before, for nested publish
Browse files Browse the repository at this point in the history
Part of fix for dotnet#60479 .
The other part of the fix will be in `dotnet/sdk`.

- Currently for the nested publish, we run `Publish`, and then our
  native targets. But that can be an overkill, and can cause issues like
  the one being fixed here.
- Instead, allow blazor to configure which target to run instead, which
  would provide us with the correct set of inputs.
  - In future, this will be cleaned up, and implemented differently.

- Also, a fixed up copy of
  `Microsoft.NET.Sdk.BlazorWebAssembly.6_0.targets` is included here, so
  we can run the corresponding test. And it can be removed once the SDK
  part of the fix gets merged.
  • Loading branch information
radical committed Oct 18, 2021
1 parent c6fb0e8 commit 3bca81d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/libraries/workloads-testing.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>

<Copy SourceFiles="@(_SourceFiles)" DestinationFolder="$(SdkWithWorkloadForTestingPath)\%(_SourceFiles.RecursiveDir)" />
<Copy SourceFiles="$(MonoProjectRoot)\wasm\BlazorOverwrite.targets" DestinationFiles="$(SdkWithWorkloadForTestingPath)\sdk\$(SdkVersionForWorkloadTesting)\Sdks\Microsoft.NET.Sdk.BlazorWebAssembly\targets\Microsoft.NET.Sdk.BlazorWebAssembly.6_0.targets" />

<WriteLinesToFile File="$(SdkWithWorkloadStampPath)" Lines="" Overwrite="true" />
</Target>
Expand All @@ -40,6 +41,7 @@
<Exec Condition="$([MSBuild]::IsOSPlatform('windows'))"
Command='powershell -ExecutionPolicy ByPass -NoProfile -command "&amp; $(_DotNetInstallScriptPath) -InstallDir $(SdkWithNoWorkloadForTestingPath) -Version $(SdkVersionForWorkloadTesting)"' />

<Copy SourceFiles="$(MonoProjectRoot)\wasm\BlazorOverwrite.targets" DestinationFiles="$(SdkWithNoWorkloadForTestingPath)\sdk\$(SdkVersionForWorkloadTesting)\Sdks\Microsoft.NET.Sdk.BlazorWebAssembly\targets\Microsoft.NET.Sdk.BlazorWebAssembly.6_0.targets" />
<WriteLinesToFile File="$(SdkWithNoWorkloadStampPath)" Lines="" Overwrite="true" />
</Target>

Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<WasmBuildAppAfterThisTarget Condition="'$(WasmBuildAppAfterThisTarget)' == '' and '$(DisableAutoWasmBuildApp)' != 'true'">Build</WasmBuildAppAfterThisTarget>

<WasmTriggerPublishAppAfterThisTarget Condition="'$(DisableAutoWasmPublishApp)' != 'true' and '$(WasmBuildingForNestedPublish)' != 'true'">Publish</WasmTriggerPublishAppAfterThisTarget>
<_WasmNestedPublishAppPreTarget Condition="'$(DisableAutoWasmPublishApp)' != 'true'">Publish</_WasmNestedPublishAppPreTarget>
<WasmNestedPublishAppPreTarget Condition="'$(WasmNestedPublishAppPreTarget)' == '' and '$(DisableAutoWasmPublishApp)' != 'true'">Publish</WasmNestedPublishAppPreTarget>

<EnableDefaultWasmAssembliesToBundle Condition="'$(EnableDefaultWasmAssembliesToBundle)' == ''">true</EnableDefaultWasmAssembliesToBundle>
<WasmBuildOnlyAfterPublish Condition="'$(WasmBuildOnlyAfterPublish)' == '' and '$(DeployOnBuild)' == 'true'">true</WasmBuildOnlyAfterPublish>
Expand Down Expand Up @@ -123,7 +123,7 @@

<!-- Public target. Do not depend on this target, as it is meant to be run by a msbuild task -->
<Target Name="WasmNestedPublishApp"
DependsOnTargets="ResolveRuntimePackAssets;$(_WasmNestedPublishAppPreTarget);$(WasmNestedPublishAppDependsOn)"
DependsOnTargets="ResolveRuntimePackAssets;$(WasmNestedPublishAppPreTarget);$(WasmNestedPublishAppDependsOn)"
Condition="'$(WasmBuildingForNestedPublish)' == 'true'"
Returns="@(WasmNativeAsset);@(WasmAssembliesFinal);@(FileWrites)">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

#nullable enable

Expand Down Expand Up @@ -225,6 +227,57 @@ void CheckNativeFileLinked(bool forPublish)
}
}

[ConditionalFact(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
public void BugRegression_60479_WithRazorClassLib()
{
string id = "blz_razor_lib_top";

InitBlazorWasmProjectDir(id);

string wasmProjectDir = Path.Combine(_projectDir!, "wasm");
string wasmProjectFile = Path.Combine(wasmProjectDir, "wasm.csproj");
Directory.CreateDirectory(wasmProjectDir);
new DotNetCommand(s_buildEnv, useDefaultArgs: false)
.WithWorkingDirectory(wasmProjectDir)
.ExecuteWithCapturedOutput("new blazorwasm")
.EnsureSuccessful();


string razorProjectDir = Path.Combine(_projectDir!, "RazorClassLibrary");
Directory.CreateDirectory(razorProjectDir);
new DotNetCommand(s_buildEnv, useDefaultArgs: false)
.WithWorkingDirectory(razorProjectDir)
.ExecuteWithCapturedOutput("new razorclasslib")
.EnsureSuccessful();

AddItemsPropertiesToProject(wasmProjectFile, extraItems:@"
<ProjectReference Include=""..\RazorClassLibrary\RazorClassLibrary.csproj"" />
<BlazorWebAssemblyLazyLoad Include=""RazorClassLibrary.dll"" />
");

_projectDir = wasmProjectDir;
string config = "Release";
// No relinking, no AOT
BlazorBuild(id, config, NativeFilesType.FromRuntimePack);

// will relink
BlazorPublish(id, config, NativeFilesType.Relinked);

// publish/wwwroot/_framework/blazor.boot.json
string frameworkDir = FindBlazorBinFrameworkDir(config, forPublish: true);
string bootJson = Path.Combine(frameworkDir, "blazor.boot.json");

Assert.True(File.Exists(bootJson), $"Could not find {bootJson}");
var jdoc = JsonDocument.Parse(File.ReadAllText(bootJson));
if (!jdoc.RootElement.TryGetProperty("resources", out JsonElement resValue) ||
!resValue.TryGetProperty("lazyAssembly", out JsonElement lazyVal))
{
throw new XunitException($"Could not find resources.lazyAssembly object in {bootJson}");
}

Assert.Contains("RazorClassLibrary.dll", lazyVal.EnumerateObject().Select(jp => jp.Name));
}

private string CreateProjectWithNativeReference(string id)
{
CreateBlazorWasmTemplateProject(id);
Expand Down

0 comments on commit 3bca81d

Please sign in to comment.