Skip to content

Commit a5e74ad

Browse files
authored
Adding Microsoft.AspNetCore.EnsureJsonTrimmability host configuration (#29719)
1 parent 7a05724 commit a5e74ad

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

sdk.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.GenAPI", "
411411
EndProject
412412
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}"
413413
EndProject
414+
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}"
415+
EndProject
414416
Global
415417
GlobalSection(SolutionConfigurationPlatforms) = preSolution
416418
Debug|Any CPU = Debug|Any CPU
@@ -793,6 +795,10 @@ Global
793795
{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
794796
{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
795797
{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA}.Release|Any CPU.Build.0 = Release|Any CPU
798+
{B8A61A5C-A9A4-45C5-97E3-CB368358682F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
799+
{B8A61A5C-A9A4-45C5-97E3-CB368358682F}.Debug|Any CPU.Build.0 = Debug|Any CPU
800+
{B8A61A5C-A9A4-45C5-97E3-CB368358682F}.Release|Any CPU.ActiveCfg = Release|Any CPU
801+
{B8A61A5C-A9A4-45C5-97E3-CB368358682F}.Release|Any CPU.Build.0 = Release|Any CPU
796802
EndGlobalSection
797803
GlobalSection(SolutionProperties) = preSolution
798804
HideSolutionNode = FALSE
@@ -937,6 +943,7 @@ Global
937943
{E2BC296E-2660-4692-B471-F6FCD4C19F6E} = {580D1AE7-AA8F-4912-8B76-105594E00B3B}
938944
{5F74AD67-A4AD-4660-A63C-844DAAF354C4} = {95D8B040-FD7F-4C86-8E47-341AF630EDA9}
939945
{C419AE2D-D318-49EB-8ECA-6A5DC13FE4EA} = {95D8B040-FD7F-4C86-8E47-341AF630EDA9}
946+
{B8A61A5C-A9A4-45C5-97E3-CB368358682F} = {580D1AE7-AA8F-4912-8B76-105594E00B3B}
940947
EndGlobalSection
941948
GlobalSection(ExtensibilityGlobals) = postSolution
942949
SolutionGuid = {FB8F26CE-4DE6-433F-B32A-79183020BBD6}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<EnableDefaultItems>false</EnableDefaultItems>
5+
<OutDirName>Tests\$(MSBuildProjectName)</OutDirName>
6+
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<TargetFramework>$(SdkTargetFramework)</TargetFramework>
10+
</PropertyGroup>
11+
12+
<PropertyGroup>
13+
<OutputType>Exe</OutputType>
14+
<PackageId>testSdkWeb</PackageId>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<Compile Include="**\*.cs" Exclude="$(GlobalExclude)" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<PackageReference Include="FluentAssertions.Json" Version="$(FluentAssertionsJsonVersion)" />
23+
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="$(SystemReflectionMetadataLoadContextVersion)" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\Microsoft.NET.TestFramework\Microsoft.NET.TestFramework.csproj" />
28+
<ProjectReference Include="..\..\..\src\WebSdk\Web\Tasks\Microsoft.NET.Sdk.Web.Tasks.csproj" />
29+
</ItemGroup>
30+
31+
</Project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Text.Json.Nodes;
7+
using FluentAssertions;
8+
using Microsoft.NET.TestFramework;
9+
using Microsoft.NET.TestFramework.Assertions;
10+
using Microsoft.NET.TestFramework.Commands;
11+
using Microsoft.NET.TestFramework.ProjectConstruction;
12+
using Xunit;
13+
using Xunit.Abstractions;
14+
15+
namespace Microsoft.NET.Sdk.Web.Tests
16+
{
17+
public class PublishTests : SdkTest
18+
{
19+
public PublishTests(ITestOutputHelper log) : base(log)
20+
{
21+
}
22+
23+
[Theory()]
24+
[MemberData(nameof(SupportedTfms))]
25+
public void TrimmingOptions_Are_Defaulted_Correctly_On_Trimmed_Apps(string targetFramework)
26+
{
27+
var projectName = "HelloWorld";
28+
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);
29+
30+
var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName);
31+
testProject.AdditionalProperties["PublishTrimmed"] = "true";
32+
33+
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: projectName + targetFramework);
34+
35+
var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
36+
publishCommand.Execute($"/p:RuntimeIdentifier={rid}").Should().Pass();
37+
38+
string outputDirectory = publishCommand.GetOutputDirectory(targetFramework: targetFramework, runtimeIdentifier: rid).FullName;
39+
string runtimeConfigFile = Path.Combine(outputDirectory, $"{projectName}.runtimeconfig.json");
40+
string runtimeConfigContents = File.ReadAllText(runtimeConfigFile);
41+
42+
JsonNode runtimeConfig = JsonNode.Parse(runtimeConfigContents);
43+
JsonNode configProperties = runtimeConfig["runtimeOptions"]["configProperties"];
44+
45+
configProperties["Microsoft.AspNetCore.EnsureJsonTrimmability"].GetValue<bool>()
46+
.Should().BeTrue();
47+
}
48+
49+
[Theory()]
50+
[MemberData(nameof(SupportedTfms))]
51+
public void TrimmingOptions_Are_Defaulted_Correctly_On_Aot_Apps(string targetFramework)
52+
{
53+
var projectName = "HelloWorld";
54+
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);
55+
56+
var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName);
57+
testProject.AdditionalProperties["PublishAOT"] = "true";
58+
59+
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: projectName + targetFramework);
60+
var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
61+
publishCommand.Execute($"/p:RuntimeIdentifier={rid}").Should().Pass();
62+
63+
string outputDirectory = publishCommand.GetIntermediateDirectory(targetFramework: targetFramework, runtimeIdentifier: rid).FullName;
64+
string responseFile = Path.Combine(outputDirectory, "native", $"{projectName}.ilc.rsp");
65+
var responseFileContents = File.ReadLines(responseFile);
66+
67+
responseFileContents.Should().Contain("--feature:Microsoft.AspNetCore.EnsureJsonTrimmability=true");
68+
}
69+
70+
public static IEnumerable<object[]> SupportedTfms { get; } = new List<object[]>
71+
{
72+
#if NET8_0
73+
new object[] { ToolsetInfo.CurrentTargetFramework }
74+
#else
75+
#error If building for a newer TFM, please update the values above
76+
#endif
77+
};
78+
79+
private TestProject CreateTestProjectForILLinkTesting(
80+
string targetFramework,
81+
string projectName)
82+
{
83+
var testProject = new TestProject()
84+
{
85+
Name = projectName,
86+
TargetFrameworks = targetFramework,
87+
IsExe = true,
88+
ProjectSdk = "Microsoft.NET.Sdk.Web"
89+
};
90+
91+
testProject.SourceFiles[$"Program.cs"] = """
92+
using Microsoft.AspNetCore.Builder;
93+
using Microsoft.Extensions.Hosting;
94+
95+
var builder = WebApplication.CreateBuilder();
96+
var app = builder.Build();
97+
app.Start();
98+
""";
99+
100+
return testProject;
101+
}
102+
}
103+
}

src/WebSdk/ProjectSystem/Targets/Microsoft.NET.Sdk.Web.ProjectSystem.targets

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ Copyright (c) .NET Foundation. All rights reserved.
4646
<EnableUnsafeBinaryFormatterSerialization Condition="'$(EnableUnsafeBinaryFormatterSerialization)' == ''">false</EnableUnsafeBinaryFormatterSerialization>
4747
</PropertyGroup>
4848

49+
50+
<PropertyGroup Condition="'$(PublishTrimmed)' == 'true' Or '$(PublishAot)' == 'true'">
51+
<!-- Runtime feature defaults to trim unnecessary code -->
52+
<EnsureAspNetCoreJsonTrimmability Condition="'$(EnsureAspNetCoreJsonTrimmability)' == ''">true</EnsureAspNetCoreJsonTrimmability>
53+
</PropertyGroup>
54+
4955
<!--
5056
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
5157
exist, it falls back to the default set of values defined here.

src/WebSdk/Web/Sdk/Sdk.targets

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ Copyright (c) .NET Foundation. All rights reserved.
3030

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

33+
<!--
34+
============================================================
35+
DefaultRuntimeHostConfigurationOptions
36+
Defaults @(RuntimeHostConfigurationOption) items based on MSBuild properties.
37+
============================================================
38+
-->
39+
40+
<ItemGroup>
41+
<RuntimeHostConfigurationOption Include="Microsoft.AspNetCore.EnsureJsonTrimmability"
42+
Condition="'$(EnsureAspNetCoreJsonTrimmability)' != ''"
43+
Value="$(EnsureAspNetCoreJsonTrimmability)"
44+
Trim="true" />
45+
</ItemGroup>
46+
3347
</Project>

0 commit comments

Comments
 (0)