Skip to content

Commit 5ebb520

Browse files
authored
[browser] Fix fingerprinting of relinked dotnet.js - follow up (dotnet#87051)
Address comments from dotnet#86048
1 parent 2835110 commit 5ebb520

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,22 @@ Copyright (c) .NET Foundation. All rights reserved.
191191

192192
<Target Name="ResolveWasmOutputs" DependsOnTargets="_ResolveWasmOutputs" />
193193

194-
<Target Name="_ResolveWasmOutputs" DependsOnTargets="ResolveReferences;PrepareResourceNames;ComputeIntermediateSatelliteAssemblies;_ResolveWasmConfiguration;_WasmNativeForBuild">
194+
<Target Name="_GetWasmRuntimePackVersion">
195+
<PropertyGroup>
196+
<_WasmRuntimePackVersion>%(ResolvedRuntimePack.NuGetPackageVersion)</_WasmRuntimePackVersion>
197+
</PropertyGroup>
198+
199+
<Error Code="WASM0005" Message="Unnable to resolve WebAssembly runtime pack version" Condition="'$(_WasmRuntimePackVersion)' == ''" />
200+
</Target>
201+
202+
<Target Name="_ResolveWasmOutputs" DependsOnTargets="ResolveReferences;PrepareResourceNames;ComputeIntermediateSatelliteAssemblies;_ResolveWasmConfiguration;_WasmNativeForBuild;_GetWasmRuntimePackVersion">
195203
<ItemGroup>
196204
<_WasmConfigFileCandidates Include="@(StaticWebAsset)" Condition="'%(SourceType)' == 'Discovered'" />
197-
<_DotNetJsItem Include="@(ReferenceCopyLocalPaths)" Condition="('%(FileName)' == 'dotnet' or '%(FileName)' == 'dotnet.native') and '%(Extension)' == '.js'" />
198205

199206
<!-- Remove dotnet.js/wasm from runtime pack, in favor of the relinked ones in @(WasmNativeAsset) -->
200207
<ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)"
201208
Condition="@(WasmNativeAsset->Count()) > 0 and ( '%(FileName)' == 'dotnet' or '%(FileName)' == 'dotnet.native' ) and ('%(Extension)' == '.wasm' or '%(Extension)' == '.js')" />
202209
</ItemGroup>
203-
<PropertyGroup>
204-
<_DotNetJsBuildVersion>%(_DotNetJsItem.NuGetPackageVersion)</_DotNetJsBuildVersion>
205-
</PropertyGroup>
206210

207211
<ComputeWasmBuildAssets
208212
Candidates="@(ReferenceCopyLocalPaths->Distinct());@(WasmNativeAsset)"
@@ -213,7 +217,7 @@ Copyright (c) .NET Foundation. All rights reserved.
213217
ProjectSatelliteAssemblies="@(IntermediateSatelliteAssembliesWithTargetPath)"
214218
TimeZoneSupport="$(_BlazorEnableTimeZoneSupport)"
215219
InvariantGlobalization="$(_WasmInvariantGlobalization)"
216-
DotNetJsVersion="$(_DotNetJsBuildVersion)"
220+
DotNetJsVersion="$(_WasmRuntimePackVersion)"
217221
CopySymbols="$(_WasmCopyOutputSymbolsToOutputDirectory)"
218222
OutputPath="$(OutputPath)"
219223
FingerprintDotNetJs="$(WasmFingerprintDotnetJs)"
@@ -357,7 +361,7 @@ Copyright (c) .NET Foundation. All rights reserved.
357361
BeforeTargets="PrepareForPublish" />
358362

359363
<!-- Wasm's Nested publish is run just to build the native bits. We don't need to run blazor targets for that -->
360-
<Target Name="ProcessPublishFilesForWasm" DependsOnTargets="_ResolveWasmConfiguration;LoadStaticWebAssetsBuildManifest" AfterTargets="ILLink" Condition="'$(WasmBuildingForNestedPublish)' != 'true'">
364+
<Target Name="ProcessPublishFilesForWasm" DependsOnTargets="_ResolveWasmConfiguration;LoadStaticWebAssetsBuildManifest;_GetWasmRuntimePackVersion" AfterTargets="ILLink" Condition="'$(WasmBuildingForNestedPublish)' != 'true'">
361365
<!-- The list of static web assets already contains all the assets from the build. We want to correct certain assets that might
362366
have changed as part of the publish process. We are going to do so as follows:
363367
* We will update Blazor runtime asset dlls if we are running PublishTrimmed
@@ -371,14 +375,6 @@ Copyright (c) .NET Foundation. All rights reserved.
371375
Condition="'%(StaticWebAsset.AssetTraitName)' == 'WasmResource' or '%(StaticWebAsset.AssetTraitName)' == 'Culture' or '%(AssetRole)' == 'Alternative'" />
372376
</ItemGroup>
373377

374-
<ItemGroup>
375-
<_DotNetJsItem Include="@(ResolvedFileToPublish)" Condition="'%(ResolvedFileToPublish.DestinationSubPath)' == 'dotnet.js' AND '%(ResolvedFileToPublish.AssetType)' == 'native'" />
376-
</ItemGroup>
377-
378-
<PropertyGroup>
379-
<_DotNetJsVersion>%(_DotNetJsItem.NuGetPackageVersion)</_DotNetJsVersion>
380-
</PropertyGroup>
381-
382378
<ComputeWasmPublishAssets
383379
ResolvedFilesToPublish="@(ResolvedFileToPublish)"
384380
CustomIcuCandidate="$(_BlazorIcuDataFileName)"
@@ -388,7 +384,7 @@ Copyright (c) .NET Foundation. All rights reserved.
388384
InvariantGlobalization="$(_WasmInvariantGlobalization)"
389385
CopySymbols="$(CopyOutputSymbolsToPublishDirectory)"
390386
ExistingAssets="@(_WasmPublishPrefilteredAssets)"
391-
DotNetJsVersion="$(_DotNetJsVersion)"
387+
DotNetJsVersion="$(_WasmRuntimePackVersion)"
392388
FingerprintDotNetJs="$(WasmFingerprintDotnetJs)"
393389
EnableThreads="$(_WasmEnableThreads)"
394390
IsWebCilEnabled="$(_WasmEnableWebcil)"

src/mono/wasm/Wasm.Build.Tests/Blazor/BuildPublishTests.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.IO;
56
using System.Linq;
67
using System.Text.Json;
@@ -65,15 +66,21 @@ public void DefaultTemplate_NoAOT_WithWorkload(string config)
6566
}
6667

6768
[Theory]
68-
[InlineData("Debug")]
69-
[InlineData("Release")]
70-
public void DefaultTemplate_BuildNative_WithWorkload(string config)
69+
[InlineData("Debug", false)]
70+
[InlineData("Release", false)]
71+
[InlineData("Debug", true)]
72+
[InlineData("Release", true)]
73+
public void DefaultTemplate_CheckFingerprinting(string config, bool expectFingerprintOnDotnetJs)
7174
{
72-
string id = $"blz_buildandbuildnative_{config}_{Path.GetRandomFileName()}";
75+
string id = $"blz_checkfingerprinting_{config}_{Path.GetRandomFileName()}";
7376

7477
CreateBlazorWasmTemplateProject(id);
7578

76-
BlazorBuild(new BlazorBuildOptions(id, config, NativeFilesType.Relinked), "/p:WasmBuildNative=true");
79+
var options = new BlazorBuildOptions(id, config, NativeFilesType.Relinked, ExpectRelinkDirWhenPublishing: true, ExpectFingerprintOnDotnetJs: expectFingerprintOnDotnetJs);
80+
var finterprintingArg = expectFingerprintOnDotnetJs ? "/p:WasmFingerprintDotnetJs=true" : string.Empty;
81+
82+
BlazorBuild(options, "/p:WasmBuildNative=true", finterprintingArg);
83+
BlazorPublish(options, "/p:WasmBuildNative=true", finterprintingArg);
7784
}
7885

7986
// Disabling for now - publish folder can have more than one dotnet*hash*js, and not sure
@@ -83,18 +90,18 @@ public void DefaultTemplate_BuildNative_WithWorkload(string config)
8390
//[InlineData("Release")]
8491
//public void DefaultTemplate_AOT_OnlyWithPublishCommandLine_Then_PublishNoAOT(string config)
8592
//{
86-
//string id = $"blz_aot_pub_{config}";
87-
//CreateBlazorWasmTemplateProject(id);
93+
//string id = $"blz_aot_pub_{config}";
94+
//CreateBlazorWasmTemplateProject(id);
8895

89-
//// No relinking, no AOT
90-
//BlazorBuild(new BlazorBuildOptions(id, config, NativeFilesType.FromRuntimePack);
96+
//// No relinking, no AOT
97+
//BlazorBuild(new BlazorBuildOptions(id, config, NativeFilesType.FromRuntimePack);
9198

92-
//// AOT=true only for the publish command line, similar to what
93-
//// would happen when setting it in Publish dialog for VS
94-
//BlazorPublish(new BlazorBuildOptions(id, config, expectedFileType: NativeFilesType.AOT, "-p:RunAOTCompilation=true");
99+
//// AOT=true only for the publish command line, similar to what
100+
//// would happen when setting it in Publish dialog for VS
101+
//BlazorPublish(new BlazorBuildOptions(id, config, expectedFileType: NativeFilesType.AOT, "-p:RunAOTCompilation=true");
95102

96-
//// publish again, no AOT
97-
//BlazorPublish(new BlazorBuildOptions(id, config, NativeFilesType.Relinked);
103+
//// publish again, no AOT
104+
//BlazorPublish(new BlazorBuildOptions(id, config, NativeFilesType.Relinked);
98105
//}
99106

100107
[Theory]
@@ -212,7 +219,7 @@ public void BugRegression_60479_WithRazorClassLib()
212219
string razorClassLibraryFileName = UseWebcil ? $"RazorClassLibrary{WebcilInWasmExtension}" : "RazorClassLibrary.dll";
213220
AddItemsPropertiesToProject(wasmProjectFile, extraItems: @$"
214221
<ProjectReference Include=""..\\RazorClassLibrary\\RazorClassLibrary.csproj"" />
215-
<BlazorWebAssemblyLazyLoad Include=""{ razorClassLibraryFileName }"" />
222+
<BlazorWebAssemblyLazyLoad Include=""{razorClassLibraryFileName}"" />
216223
");
217224

218225
_projectDir = wasmProjectDir;

src/mono/wasm/Wasm.Build.Tests/BuildTestBase.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,8 @@ public string CreateBlazorWasmTemplateProject(string id)
553553
AssertBlazorBundle(options.Config,
554554
isPublish: false,
555555
dotnetWasmFromRuntimePack: options.ExpectedFileType == NativeFilesType.FromRuntimePack,
556-
targetFramework: options.TargetFramework);
556+
targetFramework: options.TargetFramework,
557+
expectFingerprintOnDotnetJs: options.ExpectFingerprintOnDotnetJs);
557558

558559
return res;
559560
}
@@ -565,7 +566,8 @@ public string CreateBlazorWasmTemplateProject(string id)
565566
AssertBlazorBundle(options.Config,
566567
isPublish: true,
567568
dotnetWasmFromRuntimePack: options.ExpectedFileType == NativeFilesType.FromRuntimePack,
568-
targetFramework: options.TargetFramework);
569+
targetFramework: options.TargetFramework,
570+
expectFingerprintOnDotnetJs: options.ExpectFingerprintOnDotnetJs);
569571

570572
if (options.ExpectedFileType == NativeFilesType.AOT)
571573
{
@@ -581,7 +583,7 @@ public string CreateBlazorWasmTemplateProject(string id)
581583
string objBuildDir = Path.Combine(_projectDir!, "obj", options.Config, options.TargetFramework, "wasm", "for-build");
582584
// Check that we linked only for publish
583585
if (options.ExpectRelinkDirWhenPublishing)
584-
Assert.True(Directory.Exists(objBuildDir), $"Could not find expected {objBuildDir}, which gets created when relinking during Build. This is liokely a test authoring error");
586+
Assert.True(Directory.Exists(objBuildDir), $"Could not find expected {objBuildDir}, which gets created when relinking during Build. This is likely a test authoring error");
585587
else
586588
Assert.False(Directory.Exists(objBuildDir), $"Found unexpected {objBuildDir}, which gets created when relinking during Build");
587589

@@ -829,7 +831,7 @@ protected static void AssertFile(string file0, string file1, string? label = nul
829831
return result;
830832
}
831833

832-
protected void AssertBlazorBundle(string config, bool isPublish, bool dotnetWasmFromRuntimePack, string targetFramework = DefaultTargetFrameworkForBlazor, string? binFrameworkDir = null, bool expectFingerprinting = false)
834+
protected void AssertBlazorBundle(string config, bool isPublish, bool dotnetWasmFromRuntimePack, string targetFramework = DefaultTargetFrameworkForBlazor, string? binFrameworkDir = null, bool expectFingerprintOnDotnetJs = false)
833835
{
834836
binFrameworkDir ??= FindBlazorBinFrameworkDir(config, isPublish, targetFramework);
835837

@@ -861,12 +863,21 @@ void AssertFileExists(string fileName)
861863
Assert.True(File.Exists(absolutePath), $"Expected to find '{absolutePath}'");
862864
}
863865

864-
string versionHashRegex = @"\d.0.\d?(-[a-z]+(\.\d\.\d+\.\d)?)?\.([a-zA-Z0-9])+";
866+
string versionHashRegex = @"\.(?<version>.+)\.(?<hash>[a-zA-Z0-9]+)\.";
867+
865868
Assert.Collection(
866869
dotnetJsEntries.OrderBy(f => f),
867-
item => { Assert.Equal(expectFingerprinting ? $"dotnet\\.{versionHashRegex}\\.js" : "dotnet.js", item); AssertFileExists(item); },
868-
item => { Assert.Matches($"dotnet\\.native\\.{versionHashRegex}\\.js", item); AssertFileExists(item); },
869-
item => { Assert.Matches($"dotnet\\.runtime\\.{versionHashRegex}\\.js", item); AssertFileExists(item); }
870+
item =>
871+
{
872+
if (expectFingerprintOnDotnetJs)
873+
Assert.Matches($"dotnet{versionHashRegex}js", item);
874+
else
875+
Assert.Equal("dotnet.js", item);
876+
877+
AssertFileExists(item);
878+
},
879+
item => { Assert.Matches($"dotnet\\.native{versionHashRegex}js", item); AssertFileExists(item); },
880+
item => { Assert.Matches($"dotnet\\.runtime{versionHashRegex}js", item); AssertFileExists(item); }
870881
);
871882
}
872883
}
@@ -1295,7 +1306,8 @@ public record BlazorBuildOptions
12951306
NativeFilesType ExpectedFileType,
12961307
string TargetFramework = BuildTestBase.DefaultTargetFrameworkForBlazor,
12971308
bool WarnAsError = true,
1298-
bool ExpectRelinkDirWhenPublishing = false
1309+
bool ExpectRelinkDirWhenPublishing = false,
1310+
bool ExpectFingerprintOnDotnetJs = false
12991311
);
13001312

13011313
public enum GlobalizationMode

0 commit comments

Comments
 (0)