Skip to content

Adding Microsoft.AspNetCore.EnsureJsonTrimmability host configuration #29719

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 16 commits into from
Jan 10, 2023
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
7 changes: 7 additions & 0 deletions sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.GenAPI", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.GenAPI.Task", "src\GenAPI\Microsoft.DotNet.GenAPI.Task\Microsoft.DotNet.GenAPI.Task.csproj", "{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.Web.Tests", "src\Tests\Microsoft.NET.Sdk.Web.Tests\Microsoft.NET.Sdk.Web.Tests.csproj", "{B8A61A5C-A9A4-45C5-97E3-CB368358682F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -793,6 +795,10 @@ Global
{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA}.Release|Any CPU.Build.0 = Release|Any CPU
{B8A61A5C-A9A4-45C5-97E3-CB368358682F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8A61A5C-A9A4-45C5-97E3-CB368358682F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8A61A5C-A9A4-45C5-97E3-CB368358682F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8A61A5C-A9A4-45C5-97E3-CB368358682F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -937,6 +943,7 @@ Global
{E2BC296E-2660-4692-B471-F6FCD4C19F6E} = {580D1AE7-AA8F-4912-8B76-105594E00B3B}
{5F74AD67-A4AD-4660-A63C-844DAAF354C4} = {95D8B040-FD7F-4C86-8E47-341AF630EDA9}
{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA} = {95D8B040-FD7F-4C86-8E47-341AF630EDA9}
{B8A61A5C-A9A4-45C5-97E3-CB368358682F} = {580D1AE7-AA8F-4912-8B76-105594E00B3B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FB8F26CE-4DE6-433F-B32A-79183020BBD6}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<EnableDefaultItems>false</EnableDefaultItems>
<OutDirName>Tests\$(MSBuildProjectName)</OutDirName>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>$(SdkTargetFramework)</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<PackageId>testSdkWeb</PackageId>
</PropertyGroup>

<ItemGroup>
<Compile Include="**\*.cs" Exclude="$(GlobalExclude)" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions.Json" Version="$(FluentAssertionsJsonVersion)" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="$(SystemReflectionMetadataLoadContextVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.NET.TestFramework\Microsoft.NET.TestFramework.csproj" />
<ProjectReference Include="..\..\..\src\WebSdk\Web\Tasks\Microsoft.NET.Sdk.Web.Tasks.csproj" />
</ItemGroup>

</Project>
103 changes: 103 additions & 0 deletions src/Tests/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.IO;
using System.Text.Json.Nodes;
using FluentAssertions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.NET.Sdk.Web.Tests
{
public class PublishTests : SdkTest
{
public PublishTests(ITestOutputHelper log) : base(log)
{
}

[Theory()]
[MemberData(nameof(SupportedTfms))]
public void TrimmingOptions_Are_Defaulted_Correctly_On_Trimmed_Apps(string targetFramework)
{
var projectName = "HelloWorld";
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);

var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName);
testProject.AdditionalProperties["PublishTrimmed"] = "true";

var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: projectName + targetFramework);

var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
publishCommand.Execute($"/p:RuntimeIdentifier={rid}").Should().Pass();

string outputDirectory = publishCommand.GetOutputDirectory(targetFramework: targetFramework, runtimeIdentifier: rid).FullName;
string runtimeConfigFile = Path.Combine(outputDirectory, $"{projectName}.runtimeconfig.json");
string runtimeConfigContents = File.ReadAllText(runtimeConfigFile);

JsonNode runtimeConfig = JsonNode.Parse(runtimeConfigContents);
JsonNode configProperties = runtimeConfig["runtimeOptions"]["configProperties"];

configProperties["Microsoft.AspNetCore.EnsureJsonTrimmability"].GetValue<bool>()
.Should().BeTrue();
}

[Theory()]
[MemberData(nameof(SupportedTfms))]
public void TrimmingOptions_Are_Defaulted_Correctly_On_Aot_Apps(string targetFramework)
{
var projectName = "HelloWorld";
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);

var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName);
testProject.AdditionalProperties["PublishAOT"] = "true";

var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: projectName + targetFramework);
var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
publishCommand.Execute($"/p:RuntimeIdentifier={rid}").Should().Pass();

string outputDirectory = publishCommand.GetIntermediateDirectory(targetFramework: targetFramework, runtimeIdentifier: rid).FullName;
string responseFile = Path.Combine(outputDirectory, "native", $"{projectName}.ilc.rsp");
var responseFileContents = File.ReadLines(responseFile);

responseFileContents.Should().Contain("--feature:Microsoft.AspNetCore.EnsureJsonTrimmability=true");
}

public static IEnumerable<object[]> SupportedTfms { get; } = new List<object[]>
{
#if NET8_0
new object[] { ToolsetInfo.CurrentTargetFramework }
#else
#error If building for a newer TFM, please update the values above
#endif
};

private TestProject CreateTestProjectForILLinkTesting(
string targetFramework,
string projectName)
{
var testProject = new TestProject()
{
Name = projectName,
TargetFrameworks = targetFramework,
IsExe = true,
ProjectSdk = "Microsoft.NET.Sdk.Web"
};

testProject.SourceFiles[$"Program.cs"] = """
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder();
var app = builder.Build();
app.Start();
""";

return testProject;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Copyright (c) .NET Foundation. All rights reserved.
<EnableUnsafeBinaryFormatterSerialization Condition="'$(EnableUnsafeBinaryFormatterSerialization)' == ''">false</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>


<PropertyGroup Condition="'$(PublishTrimmed)' == 'true' Or '$(PublishAot)' == 'true'">
<!-- Runtime feature defaults to trim unnecessary code -->
<EnsureAspNetCoreJsonTrimmability Condition="'$(EnsureAspNetCoreJsonTrimmability)' == ''">true</EnsureAspNetCoreJsonTrimmability>
</PropertyGroup>

<!--
Newer versions of Visual Studio ship the designtime related properties in a targets file and all future design time only elements should be added there. If that file does not
exist, it falls back to the default set of values defined here.
Expand Down
14 changes: 14 additions & 0 deletions src/WebSdk/Web/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ Copyright (c) .NET Foundation. All rights reserved.

<Import Sdk="Microsoft.NET.Sdk.Publish" Project="Sdk.targets" />

<!--
============================================================
DefaultRuntimeHostConfigurationOptions
Defaults @(RuntimeHostConfigurationOption) items based on MSBuild properties.
============================================================
-->

<ItemGroup>
<RuntimeHostConfigurationOption Include="Microsoft.AspNetCore.EnsureJsonTrimmability"
Condition="'$(EnsureAspNetCoreJsonTrimmability)' != ''"
Value="$(EnsureAspNetCoreJsonTrimmability)"
Trim="true" />
</ItemGroup>

</Project>