Skip to content

Commit e27023b

Browse files
authored
Add DynamicCodeSupport Feature Switch (#29785)
Adds support in the SDK for the new RuntimeFeature.IsDynamicCodeSupported feature switch added in dotnet/runtime#80246. When PublishAot is set in a project, DynamicCodeSupport gets defaulted to false.
1 parent 5638e0f commit e27023b

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

src/Assets/TestProjects/KitchenSink/TestApp/TestApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<NullabilityInfoContextSupport>false</NullabilityInfoContextSupport>
2626
<CustomResourceTypesSupport>false</CustomResourceTypesSupport>
2727
<UseSystemResourceKeys>true</UseSystemResourceKeys>
28+
<DynamicCodeSupport>true</DynamicCodeSupport>
2829
<BuiltInComInteropSupport>false</BuiltInComInteropSupport>
2930
<_EnableConsumingManagedCodeFromNativeHosting>false</_EnableConsumingManagedCodeFromNativeHosting>
3031
<EnableCppCLIHostActivation>false</EnableCppCLIHostActivation>

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Copyright (c) .NET Foundation. All rights reserved.
1919
<PublishTrimmed Condition="'$(PublishTrimmed)' == '' And '$(PublishAot)' == 'true'">true</PublishTrimmed>
2020
<_IsTrimmingEnabled Condition="'$(_IsTrimmingEnabled)' == '' And ('$(PublishTrimmed)' == 'true' Or '$(IsTrimmable)' == 'true')">true</_IsTrimmingEnabled>
2121
<_IsTrimmingEnabled Condition="'$(_IsTrimmingEnabled)' == ''">false</_IsTrimmingEnabled>
22+
<DynamicCodeSupport Condition="'$(DynamicCodeSupport)' == '' And '$(PublishAot)' == 'true'">false</DynamicCodeSupport>
2223
</PropertyGroup>
2324

2425
<ItemDefinitionGroup>

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,11 @@ Copyright (c) .NET Foundation. All rights reserved.
474474
Value="$(UseSystemResourceKeys)"
475475
Trim="true" />
476476

477+
<RuntimeHostConfigurationOption Include="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported"
478+
Condition="'$(DynamicCodeSupport)' != ''"
479+
Value="$(DynamicCodeSupport)"
480+
Trim="true" />
481+
477482
<RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.BuiltInComInterop.IsSupported"
478483
Condition="'$(BuiltInComInteropSupport)' != ''"
479484
Value="$(BuiltInComInteropSupport)"

src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAProjectWithAllFeatures.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public void It_publishes_the_project_correctly(string targetFramework, string []
8383
""System.Reflection.NullabilityInfoContext.IsSupported"": false,
8484
""System.Resources.ResourceManager.AllowCustomResourceTypes"": false,
8585
""System.Resources.UseSystemResourceKeys"": true,
86+
""System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported"": true,
8687
""System.Runtime.InteropServices.BuiltInComInterop.IsSupported"": false,
8788
""System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting"": false,
8889
""System.Runtime.InteropServices.EnableCppCLIHostActivation"": false,

src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.NET.TestFramework.Assertions;
1818
using Microsoft.NET.TestFramework.Commands;
1919
using Microsoft.NET.TestFramework.ProjectConstruction;
20+
using Newtonsoft.Json.Linq;
2021
using Xunit;
2122
using Xunit.Abstractions;
2223
using static Microsoft.NET.Publish.Tests.PublishTestUtils;
@@ -705,6 +706,31 @@ public void It_publishes_with_implicit_rid_with_NativeAotApp(string targetFramew
705706
}
706707
}
707708

709+
[RequiresMSBuildVersionTheory("17.0.0.32901")]
710+
[InlineData(ToolsetInfo.CurrentTargetFramework)]
711+
public void It_builds_with_dynamiccodesupport_false_when_publishaot_true(string targetFramework)
712+
{
713+
var projectName = "DynamicCodeSupportFalseApp";
714+
var testProject = CreateHelloWorldTestProject(targetFramework, projectName, true);
715+
testProject.AdditionalProperties["PublishAot"] = "true";
716+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
717+
718+
var buildCommand = new BuildCommand(testAsset);
719+
buildCommand
720+
.Execute()
721+
.Should()
722+
.Pass();
723+
724+
string outputDirectory = buildCommand.GetOutputDirectory(targetFramework: targetFramework).FullName;
725+
string runtimeConfigFile = Path.Combine(outputDirectory, $"{projectName}.runtimeconfig.json");
726+
string runtimeConfigContents = File.ReadAllText(runtimeConfigFile);
727+
728+
JObject runtimeConfig = JObject.Parse(runtimeConfigContents);
729+
JToken configProperties = runtimeConfig["runtimeOptions"]["configProperties"];
730+
configProperties["System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported"].Value<bool>()
731+
.Should().BeFalse();
732+
}
733+
708734
private void CheckIlcVersions(string projectPath, string targetFramework, string rid, string expectedVersion)
709735
{
710736
// Compiler version matches expected version

0 commit comments

Comments
 (0)