Skip to content

Commit

Permalink
[Static web assets] Fixes issues we discovered while integrating the …
Browse files Browse the repository at this point in the history
…static web assets improvements on to the ASP.NET Core repo (#19080)

* Filter doc XML files from blazor static web assets
* Fallback to Identity metadata when we try to match project references
  * This is not technically necessary because regular projects have the right metadata, however, ASP.NET Core uses some
special project types and its easy/cheap for us to be more lenient here and fallback.
* Correct empty entry for manifest pattern on generation
* The dotnet.js manifest file was not being passed on to GenerateBlazorBootJson manifest.
* When generating the manifest we use for development we were including empty entries "" as well as the base path for assets in the current project, which is not necessary.
* Only include discovery patterns when the folder exists on disk
  * For example, avoid creating a pattern when the wwwroot folder doesn't exist, since we can't map a non existing folder at runtime.
* Correctly compute the asset path for assets that are copied to the output folder.
* Fix how we deal with transitive assets
  • Loading branch information
javiercn committed Jul 24, 2021
1 parent 2714ab9 commit 27baeae
Show file tree
Hide file tree
Showing 40 changed files with 1,791 additions and 1,836 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<ItemGroup Condition="'@(_DotNetJsItem->Count())' != '0'">
<ReferenceCopyLocalPaths Remove="@(_DotNetJsItem)" />
<_DotnetJsStaticWebAsset Include="@(_DotnetJsStaticWebAssetCandidate->'%(ContentRoot)_framework\dotnet.js')" />
<_BlazorStaticWebAsset Include="@(_DotnetJsStaticWebAsset)" />
</ItemGroup>
</Target>

Expand Down Expand Up @@ -314,7 +315,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<Target Name="_AddBlazorWasmStaticWebAssets" DependsOnTargets="_ResolveBlazorWasmOutputs">
<ItemGroup>
<StaticWebAsset Include="@(_BlazorStaticWebAsset)" />
<StaticWebAsset Include="@(_DotnetJsStaticWebAsset)" />
<StaticWebAsset Include="@(_BlazorGzipStaticWebAsset)" />
<StaticWebAsset Include="@(_BuildBlazorBootJsonStaticWebAsset)" />
</ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions src/BlazorWasmSdk/Tasks/ComputeBlazorBuildAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,14 @@ public static bool ShouldFilterCandidate(
".a" => "extension is .a is not supported.",
".c" => "extension is .c is not supported.",
".h" => "extension is .h is not supported.",
".xml" => "it is a documentation file",
".rsp" => "extension is .rsp is not supported.",
".props" => "extension is .props is not supported.",
".blat" when !timezoneSupport => "timezone support is not enabled.",
".dat" when invariantGlobalization && fileName.StartsWith("icudt") => "invariant globalization is enabled",
".js" when fileName == "dotnet" => "dotnet js is already processed by Blazor",
".js" when assetType == "native" => "dotnet js is already processed by Blazor",
".json" when fileName == "emcc-props" => $"{fileName}{extension} is not used by Blazor",
".js" when fileName == "dotnet" => "dotnet.js is already processed by Blazor",
".js" when assetType == "native" => $"{fileName}{extension} is not used by Blazor",
".pdb" when !copySymbols => "copying symbols is disabled",
_ => null
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ Copyright (c) .NET Foundation. All rights reserved.
</ItemGroup>
</Target>

<Target Name="_EnsureContentRootFolders" AfterTargets="_RestoreStaticWebAssetsBuildAssets">
<ItemGroup>
<_StaticWebAssetContentRootFolder Include="@(StaticWebAsset->'%(ContentRoot)')" Condition="'%(SourceType)' == 'Computed'" />
</ItemGroup>
<MakeDir Directories="@(_StaticWebAssetContentRootFolder)" />
</Target>

<Target Name="CopyStaticWebAssetsToOutputFolder" DependsOnTargets="$(CopyStaticWebAssetsToOutputFolderDependsOn)" />

<Target Name="_SplitStaticWebAssetsByCopyOptions" AfterTargets="CopyStaticWebAssetsToOutputFolder">
Expand Down Expand Up @@ -429,6 +436,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<GenerateStaticWebAssetsDevelopmentManifest
DiscoveryPatterns="@(StaticWebAssetDiscoveryPattern)"
Assets="@(StaticWebAsset)"
BasePath="$(StaticWebAssetBasePath)"
ManifestPath="$(StaticWebAssetDevelopmentManifestPath)">
</GenerateStaticWebAssetsDevelopmentManifest>

Expand Down Expand Up @@ -467,7 +475,7 @@ Copyright (c) .NET Foundation. All rights reserved.

<ItemGroup>

<StaticWebAssetDiscoveryPattern Include="$(PackageId)\wwwroot">
<StaticWebAssetDiscoveryPattern Include="$(PackageId)\wwwroot" Condition="Exists('$(MSBuildProjectDirectory)\wwwroot')">
<ContentRoot>$(MSBuildProjectDirectory)\wwwroot\</ContentRoot>
<BasePath>$(StaticWebAssetBasePath)</BasePath>
<Pattern>**</Pattern>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void MergeStaticWebAssets(
switch (manifest.Mode)
{
case StaticWebAssetsManifest.ManifestModes.Default:
if (asset.IsForReferencedProjectsOnly())
if (asset.IsForCurrentProjectOnly())
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class GenerateStaticWebAssetsDevelopmentManifest : Task
WriteIndented = true
};

[Required]
public string BasePath { get; set; }

[Required]
public ITaskItem[] DiscoveryPatterns { get; set; }

Expand Down Expand Up @@ -64,8 +67,8 @@ public StaticWebAssetsDevelopmentManifest ComputeDevelopmentManifest(

var discoveryPatternsByBasePath = DiscoveryPatterns
.Select(ComputeDiscoveryPattern)
.GroupBy(p => p.BasePath,
(key, values) => (key.Split('/'), values));
.GroupBy(p => p.BasePath == BasePath ? "" : p.BasePath,
(key, values) => (key.Split(new[] { '/' }, options: StringSplitOptions.RemoveEmptyEntries), values));

var manifest = CreateManifest(assetsWithPathSegments, discoveryPatternsByBasePath);
return manifest;
Expand Down Expand Up @@ -117,7 +120,9 @@ private StaticWebAssetsDevelopmentManifest CreateManifest(
}
var matchingAsset = new StaticWebAssetMatch
{
SubPath = asset.Identity.Substring(asset.ContentRoot.Length),
SubPath = asset.Identity.StartsWith(asset.ContentRoot) ?
asset.Identity.Substring(asset.ContentRoot.Length) :
asset.RelativePath,
ContentRootIndex = index
};
currentNode.Children ??= new Dictionary<string, StaticWebAssetNode>();
Expand Down Expand Up @@ -290,7 +295,7 @@ private static (string[], StaticWebAsset) ChooseAsset(string key, IEnumerable<St
}
}

return (key.Split('/'), buildSpecificAsset ?? buildAndPublishAsset);
return (key.Split(new[] { '/' }, options: StringSplitOptions.RemoveEmptyEntries), buildSpecificAsset ?? buildAndPublishAsset);
}

private StaticWebAssetsManifest.DiscoveryPattern ComputeDiscoveryPattern(ITaskItem pattern)
Expand Down Expand Up @@ -338,27 +343,5 @@ private StaticWebAssetsManifest.ManifestReference ComputeManifestReference(ITask

return result;
}

private void PersistManifest(StaticWebAssetsManifest manifest)
{
var data = JsonSerializer.SerializeToUtf8Bytes(manifest, ManifestSerializationOptions);
var fileExists = File.Exists(ManifestPath);
var existingManifestHash = fileExists ? StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(ManifestPath)).Hash : "";

if (!fileExists)
{
Log.LogMessage($"Creating manifest because manifest file '{ManifestPath}' does not exist.");
File.WriteAllBytes(ManifestPath, data);
}
else if (!string.Equals(manifest.Hash, existingManifestHash, StringComparison.Ordinal))
{
Log.LogMessage($"Updating manifest because manifest version '{manifest.Hash}' is different from existing manifest hash '{existingManifestHash}'.");
File.WriteAllBytes(ManifestPath, data);
}
else
{
Log.LogMessage($"Skipping manifest updated because manifest version '{manifest.Hash}' has not changed.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ private ITaskItem FindMatchingProject(ITaskItem manifest)
for (var j = 0; j < ProjectReferences.Length; j++)
{
var projectReference = ProjectReferences[j];
var referenceMetadata = projectReference.GetMetadata("MSBuildSourceProjectFile");
// All project references should define MSBuildSourceProjectFile but in the ASP.NET Core some special (malformed) references do not.
// We can be more lenient here and fallback to the project reference ItemSpec if not present.
referenceMetadata = !string.IsNullOrEmpty(referenceMetadata) ? referenceMetadata : projectReference.ItemSpec;
var matchPath = string.Equals(
Path.GetFullPath(manifest.GetMetadata("ProjectFile")),
Path.GetFullPath(projectReference.GetMetadata("MSBuildSourceProjectFile")),
Path.GetFullPath(referenceMetadata),
StringComparison.Ordinal);

if (matchPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ private string TemplatizeCompressedAssets(StaticWebAsset asset, string originalV
return null;
}

return Path.Combine(Path.GetDirectoryName(asset.Identity), "{" + asset.RelativePath + "}");
if (originalValue.Contains("[["))
{
return null;
}

return Path.Combine(Path.GetDirectoryName(asset.Identity), "[[" + asset.RelativePath + "]]");
}

protected override string EmbeddedResourcePrefix => string.Join('.', "Microsoft.NET.Sdk.BlazorWebAssembly.Tests", "StaticWebAssetsBaselines");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public BlazorWasmStaticWebAssetsIntegrationTest(ITestOutputHelper log) : base(lo
{
}

[Fact(Skip = "https://github.com/dotnet/runtime/issues/55779")]
[SkipOnCI("This is a baseline test that needs to be further `templatized` https://github.com/dotnet/runtime/issues/55779")]
[Fact]
public void StaticWebAssets_BuildMinimal_Works()
{
// Arrange
Expand Down Expand Up @@ -52,8 +51,7 @@ public void StaticWebAssets_BuildMinimal_Works()
intermediateOutputPath);
}

[Fact(Skip = "https://github.com/dotnet/runtime/issues/55779")]
[SkipOnCI("This is a baseline test that needs to be further `templatized` https://github.com/dotnet/runtime/issues/55779")]
[Fact]
public void StaticWebAssets_PublishMinimal_Works()
{
// Arrange
Expand Down Expand Up @@ -83,8 +81,7 @@ public void StaticWebAssets_PublishMinimal_Works()
intermediateOutputPath);
}

[Fact(Skip = "https://github.com/dotnet/runtime/issues/55779")]
[SkipOnCI("This is a baseline test that needs to be further `templatized` https://github.com/dotnet/runtime/issues/55779")]
[Fact]
public void StaticWebAssets_Build_Hosted_Works()
{
// Arrange
Expand Down Expand Up @@ -115,8 +112,7 @@ public void StaticWebAssets_Build_Hosted_Works()
intermediateOutputPath);
}

[Fact(Skip = "https://github.com/dotnet/runtime/issues/55779")]
[SkipOnCI("This is a baseline test that needs to be further `templatized` https://github.com/dotnet/runtime/issues/55779")]
[Fact]
public void StaticWebAssets_Publish_Hosted_Works()
{
// Arrange
Expand Down
Loading

0 comments on commit 27baeae

Please sign in to comment.