Skip to content
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
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"sdk": {
"version": "5.0.100-preview.6.20310.4"
"version": "5.0.100-preview.7.20330.3"
},
"tools": {
"dotnet": "5.0.100-preview.6.20310.4",
"dotnet": "5.0.100-preview.7.20330.3",
"runtimes": {
"dotnet/x64": [
"2.1.18",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ private static IEnumerable<string> GetDeclaredTypeNames(string assemblyPath)
{
using (var file = File.OpenRead(assemblyPath))
{
var peReader = new PEReader(file);
using var peReader = new PEReader(file);
Copy link
Member

Choose a reason for hiding this comment

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

👍

var metadataReader = peReader.GetMetadataReader();
return metadataReader.TypeDefinitions.Where(t => !t.IsNil).Select(t =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using Microsoft.AspNetCore.Razor.Tasks;
using Microsoft.AspNetCore.Testing;
using Xunit;
using ResourceHashesByNameDictionary = System.Collections.Generic.Dictionary<string, string>;
using static Microsoft.AspNetCore.Razor.Design.IntegrationTests.ServiceWorkerAssert;
using ResourceHashesByNameDictionary = System.Collections.Generic.Dictionary<string, string>;

namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
Expand Down Expand Up @@ -60,6 +60,8 @@ public async Task Publish_WithDefaultSettings_Works()
serviceWorkerPath: Path.Combine("serviceworkers", "my-service-worker.js"),
serviceWorkerContent: "// This is the production service worker",
assetsManifestPath: "custom-service-worker-assets.js");

VerifyTypeGranularTrimming(result, blazorPublishDirectory);
}

[Fact]
Expand Down Expand Up @@ -223,6 +225,10 @@ public async Task Publish_WithTrimmingdDisabled_Works()
serviceWorkerPath: Path.Combine("serviceworkers", "my-service-worker.js"),
serviceWorkerContent: "// This is the production service worker",
assetsManifestPath: "custom-service-worker-assets.js");

// Verify assemblies are not trimmed
var loggingAssemblyPath = Path.Combine(blazorPublishDirectory, "_framework", "Microsoft.Extensions.Logging.Abstractions.dll");
Assert.AssemblyContainsType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.Abstractions.NullLogger");
}

[Fact]
Expand Down Expand Up @@ -309,6 +315,8 @@ public async Task Publish_HostedApp_DefaultSettings_Works()
serviceWorkerPath: Path.Combine("serviceworkers", "my-service-worker.js"),
serviceWorkerContent: "// This is the production service worker",
assetsManifestPath: "custom-service-worker-assets.js");

VerifyTypeGranularTrimming(result, blazorPublishDirectory);
}

[Fact]
Expand Down Expand Up @@ -693,6 +701,21 @@ static string ParseWebFormattedHash(string webFormattedHash)
}
}


private void VerifyTypeGranularTrimming(MSBuildResult result, string blazorPublishDirectory)
{
var loggingAssemblyPath = Path.Combine(blazorPublishDirectory, "_framework", "Microsoft.Extensions.Logging.Abstractions.dll");
Assert.FileExists(result, loggingAssemblyPath);

// ILogger is referenced by the app, so we expect it to be preserved
Assert.AssemblyContainsType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.ILogger");
// LogLevel is referenced by ILogger and therefore must be preserved.
Assert.AssemblyContainsType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.LogLevel");

// NullLogger is not referenced by the app, and should be trimmed.
Assert.AssemblyDoesNotContainType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.Abstractions.NullLogger");
}

private static BootJsonData ReadBootJsonData(MSBuildResult result, string path)
{
return JsonSerializer.Deserialize<BootJsonData>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Build.Framework;
Expand All @@ -21,16 +23,27 @@ public class CreateBlazorTrimmerRootDescriptorFile : Task

public override bool Execute()
{
using var fileStream = File.Create(TrimmerFile.ItemSpec);
var rootDescriptor = CreateRootDescriptorContents();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding the extra package file uncovered an issue with publish incrementalism. We were always writing a new trimmer descriptor which would cause the linker to be non-incremental. The task was updated to use the same pattern as WriteLinesToFile.WriteOnlyWhenDifferent

if (File.Exists(TrimmerFile.ItemSpec))
{
var existing = File.ReadAllText(TrimmerFile.ItemSpec);

if (string.Equals(rootDescriptor, existing, StringComparison.Ordinal))
{
Log.LogMessage(MessageImportance.Low, "Skipping write to file {0} because contents would not change.", TrimmerFile.ItemSpec);
// Avoid writing if the file contents are identical. This is required for build incrementalism.
return !Log.HasLoggedErrors;
}
}

WriteRootDescriptor(fileStream);
return true;
File.WriteAllText(TrimmerFile.ItemSpec, rootDescriptor);
return !Log.HasLoggedErrors;
}

internal void WriteRootDescriptor(Stream stream)
internal string CreateRootDescriptorContents()
{
var roots = new XElement("linker");
foreach (var assembly in Assemblies)
foreach (var assembly in Assemblies.OrderBy(a => a.ItemSpec))
{
// NOTE: Descriptor files don't include the file extension
// in the assemblyName.
Expand Down Expand Up @@ -60,10 +73,7 @@ internal void WriteRootDescriptor(Stream stream)
OmitXmlDeclaration = true
};

using var writer = XmlWriter.Create(stream, xmlWriterSettings);
var xDocument = new XDocument(roots);

xDocument.Save(writer);
return new XDocument(roots).Root.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ Copyright (c) .NET Foundation. All rights reserved.
<UsingTask TaskName="Microsoft.AspNetCore.Razor.Tasks.CreateBlazorTrimmerRootDescriptorFile" AssemblyFile="$(RazorSdkBuildTasksAssembly)" />

<PropertyGroup>
<PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed>
<SelfContained>true</SelfContained>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

<!-- Trimmer defaults -->
<PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed>
<TrimMode Condition="'$(TrimMode)' == ''">link</TrimMode>

<StaticWebAssetBasePath Condition="'$(StaticWebAssetBasePath)' == ''">/</StaticWebAssetBasePath>
<BlazorCacheBootResources Condition="'$(BlazorCacheBootResources)' == ''">true</BlazorCacheBootResources>

Expand All @@ -46,14 +49,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<KnownFrameworkReference Remove="Microsoft.AspNetCore.App" />
</ItemGroup>

<!-- Temporary workaround until ILLink.targets are updated -->
<PropertyGroup Condition=" '$(PublishTrimmed)' == 'true' ">
<IntermediateLinkDir Condition=" '$(IntermediateLinkDir)' == '' ">$(IntermediateOutputPath)linked\</IntermediateLinkDir>
<IntermediateLinkDir Condition=" !HasTrailingSlash('$(IntermediateLinkDir)') ">$(IntermediateLinkDir)\</IntermediateLinkDir>
<!-- Used to enable incremental build for the linker target. -->
<_LinkSemaphore>$(IntermediateOutputPath)Link.semaphore</_LinkSemaphore>
</PropertyGroup>

<Import Project="Microsoft.NET.Sdk.Razor.Components.ServiceWorkerAssetsManifest.targets" Condition="'$(ServiceWorkerAssetsManifest)' != ''" />

<Target Name="_ScrambleDotnetJsFileName" AfterTargets="ResolveRuntimePackAssets">
Expand Down Expand Up @@ -305,18 +300,22 @@ Copyright (c) .NET Foundation. All rights reserved.
</Copy>
</Target>

<Target Name="_CreateBlazorTrimmerRootDescriptorFiles" BeforeTargets="ILLink" AfterTargets="ComputeResolvedFilesToPublishList">
<Target Name="_BlazorWasmPrepareForLink" BeforeTargets="PrepareForILLink">
<PropertyGroup>
<_BlazorTypeGranularTrimmerDescriptorFile>$(IntermediateOutputPath)typegranularity.trimmerdescriptor.xml</_BlazorTypeGranularTrimmerDescriptorFile>
</PropertyGroup>

<ItemGroup>
<_BlazorTypeGranularAssembly
Include="@(ResolvedFileToPublish)"
Include="@(ManagedAssemblyToLink)"
Condition="'%(Extension)' == '.dll' AND ($([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.')) or $([System.String]::Copy('%(Filename)').StartsWith('Microsoft.Extensions.')))">
<Required>false</Required>
<Preserve>all</Preserve>
</_BlazorTypeGranularAssembly>

<ManagedAssemblyToLink
IsTrimmable="true"
Condition="'%(Extension)' == '.dll' AND ($([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.')) or $([System.String]::Copy('%(Filename)').StartsWith('Microsoft.Extensions.')))" />
Copy link
Member

Choose a reason for hiding this comment

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

(minor) I think there is a way to not duplicate this condition, and instead just use @(_BlazorTypeGranularAssembly) to update these ManagedAssemblyToLink items.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that by removing and adding it? ItemGroup Update doesn't work inside of a target.

</ItemGroup>

<CreateBlazorTrimmerRootDescriptorFile
Expand Down
1 change: 1 addition & 0 deletions src/Razor/test/testassets/blazorwasm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static void Main(string[] args)
{
GC.KeepAlive(typeof(System.Text.Json.JsonSerializer));
GC.KeepAlive(typeof(RazorClassLibrary.Class1));
GC.KeepAlive(typeof(Microsoft.Extensions.Logging.ILogger));
#if REFERENCE_classlibrarywithsatelliteassemblies
GC.KeepAlive(typeof(classlibrarywithsatelliteassemblies.Class1));
#endif
Expand Down
8 changes: 8 additions & 0 deletions src/Razor/test/testassets/blazorwasm/blazorwasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
<ProjectReference Include="..\razorclasslibrary\RazorClassLibrary.csproj" />
</ItemGroup>

<ItemGroup>
<!--
We need a Microsoft.* package to verify type granular trimming. We'll pick a fixed version to we can bake in some details about
the contents of the package in our tests.
-->
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<!-- These assets should be treated as static web assets for publish purposes -->
<Content Include="..\LinkBaseToWebRoot\**\*.js">
Expand Down