Skip to content

Commit

Permalink
Add support for new feature switch (#14432)
Browse files Browse the repository at this point in the history
* Add StartupHookSupport feature-switch

* Disable startup hooks on trimmed apps by default
Add test

* Fix property condition

* Add new feature switch to test

* Add feature switch to csproj

* Update default condition

* Test that .NET 5 remains unchanged
  • Loading branch information
mateoatr authored Nov 13, 2020
1 parent 1523882 commit 2fae2fa
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Assets/TestProjects/KitchenSink/TestApp/TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<TieredCompilation>true</TieredCompilation>
<TieredCompilationQuickJit>true</TieredCompilationQuickJit>
<TieredCompilationQuickJitForLoops>true</TieredCompilationQuickJitForLoops>
<StartupHookSupport>false</StartupHookSupport>
<EventSourceSupport>false</EventSourceSupport>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ Copyright (c) .NET Foundation. All rights reserved.
<_LinkSemaphore>$(IntermediateOutputPath)Link.semaphore</_LinkSemaphore>
</PropertyGroup>

<!-- We disable startup hooks for trimmed apps here so that the feature
switch can flow to the runtimeconfig.json. Startup hooks are disabled
by default since they may require assemblies, types or members that
could be removed by the linker, causing a trimmed app to crash. -->
<PropertyGroup Condition="'$(StartupHookSupport)' == '' And
'$(PublishTrimmed)' == 'true' And
'$(_TargetFrameworkVersionWithoutV)' >= '6.0'">
<StartupHookSupport>false</StartupHookSupport>
</PropertyGroup>

<!--
============================================================
ILLink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ Copyright (c) .NET Foundation. All rights reserved.
Condition="'$(TieredCompilationQuickJitForLoops)' != ''"
Value="$(TieredCompilationQuickJitForLoops)" />

<RuntimeHostConfigurationOption Include="System.StartupHookProvider.IsSupported"
Condition="'$(StartupHookSupport)' != ''"
Value="$(StartupHookSupport)"
Trim="true" />

<RuntimeHostConfigurationOption Include="System.Text.Encoding.EnableUnsafeUTF7Encoding"
Condition="'$(EnableUnsafeUTF7Encoding)' != ''"
Value="$(EnableUnsafeUTF7Encoding)"
Expand All @@ -412,7 +417,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<RuntimeHostConfigurationOption Include="System.Threading.ThreadPool.MaxThreads"
Condition="'$(ThreadPoolMaxThreads)' != ''"
Value="$(ThreadPoolMaxThreads)" />

</ItemGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void It_publishes_the_project_correctly(string targetFramework, string []
""System.Runtime.TieredCompilation"": true,
""System.Runtime.TieredCompilation.QuickJit"": true,
""System.Runtime.TieredCompilation.QuickJitForLoops"": true,
""System.StartupHookProvider.IsSupported"": false,
""System.Text.Encoding.EnableUnsafeUTF7Encoding"": false,
""System.Threading.ThreadPool.MinThreads"": 2,
""System.Threading.ThreadPool.MaxThreads"": 9,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -379,6 +380,40 @@ public void ILLink_verify_analysis_warnings_hello_world_app(string targetFramewo
Assert.True(!missingWarnings.Any() && !extraWarnings.Any(), errorMessage);
}

[Theory]
[InlineData("net5.0")]
[InlineData("net6.0")]
public void StartupHookSupport_is_false_by_default_on_trimmed_apps(string targetFramework)
{
var projectName = "HelloWorld";
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);

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

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

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


if (Version.TryParse(targetFramework.TrimStart("net".ToCharArray()), out Version parsedVersion) &&
parsedVersion.Major >= 6)
{
JObject runtimeConfig = JObject.Parse(runtimeConfigContents);
runtimeConfig["runtimeOptions"]["configProperties"]
["System.StartupHookProvider.IsSupported"].Value<bool>()
.Should().Be(false);
}
else
{
runtimeConfigContents.Should().NotContain("System.StartupHookProvider.IsSupported");
}
}

[Theory]
[InlineData("netcoreapp3.0")]
public void ILLink_accepts_root_descriptor(string targetFramework)
Expand Down

0 comments on commit 2fae2fa

Please sign in to comment.