Skip to content
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

Enable AOT compatibility for all libraries #4625

Merged
merged 3 commits into from
Nov 28, 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
6 changes: 0 additions & 6 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@
<MsbuildTaskMicrosoftCodeAnalysisCSharpVersion>$(MicrosoftCodeAnalysisCSharpVersion)</MsbuildTaskMicrosoftCodeAnalysisCSharpVersion>
</PropertyGroup>

<Target Name="DisableBrokenAnalyzers" BeforeTargets="CoreCompile">
<ItemGroup>
<Analyzer Remove="@(Analyzer)" Condition="'%(Filename)' == 'Microsoft.Extensions.Configuration.Binder.SourceGeneration'"/>
</ItemGroup>
</Target>

<!-- Test configuration -->
<PropertyGroup>
<!-- Disable building Integration Test projects in LUT. -->
Expand Down
1 change: 1 addition & 0 deletions src/Libraries/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreTargetFrameworks)$(ConditionalNet462)</TargetFrameworks>
<IsTrimmable Condition="'$(IsTrimmable)' == '' and $([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>

<InjectDiagnosticAttributesOnLegacy Condition="'$(InjectDiagnosticAttributesOnLegacy)' == ''">true</InjectDiagnosticAttributesOnLegacy>
<InjectIsExternalInitOnLegacy Condition="'$(InjectIsExternalInitOnLegacy)' == ''">true</InjectIsExternalInitOnLegacy>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Diagnostics.Latency;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -64,11 +63,6 @@ public static IServiceCollection AddRequestLatencyTelemetry(this IServiceCollect
/// <param name="section">Configuration of <see cref="RequestLatencyTelemetryOptions"/>.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException">Either <paramref name="services"/> or <paramref name="section"/> is <see langword="null" />.</exception>
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, type: typeof(RequestLatencyTelemetryOptions))]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed with [DynamicDependency]")]
public static IServiceCollection AddRequestLatencyTelemetry(this IServiceCollection services, IConfigurationSection section)
=> Throw.IfNull(services)
.Configure<RequestLatencyTelemetryOptions>(Throw.IfNull(section))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static IServiceCollection AddHttpLoggingRedaction(this IServiceCollection
{
_ = Throw.IfNull(section);

return services.AddHttpLoggingRedaction(section.Bind);
return services.AddHttpLoggingRedaction(o => section.Bind(o));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<PropertyGroup>
<TargetFrameworks>$(NetCoreTargetFrameworks)</TargetFrameworks>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<UseLoggingGenerator>true</UseLoggingGenerator>
<InjectSharedThrow>false</InjectSharedThrow>
<InjectTrimAttributesOnLegacy>false</InjectTrimAttributesOnLegacy>
Expand All @@ -22,7 +23,7 @@

<PropertyGroup>
<Stage>normal</Stage>
<MinCodeCoverage>100</MinCodeCoverage>
<MinCodeCoverage>99</MinCodeCoverage>
<MinMutationScore>85</MinMutationScore>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<PropertyGroup>
<TargetFrameworks>$(NetCoreTargetFrameworks)</TargetFrameworks>
<!-- blocked by https://github.com/dotnet/runtime/issues/94547 -->
<IsAotCompatible>false</IsAotCompatible>
<UseLoggingGenerator>true</UseLoggingGenerator>
<UseMetricsGenerator>true</UseMetricsGenerator>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.AmbientMetadata;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Shared.Diagnostics;
Expand All @@ -25,11 +23,6 @@ public static class ApplicationMetadataHostBuilderExtensions
/// <returns>The value of <paramref name="builder"/>>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="sectionName"/> is either <see langword="null"/>, empty or whitespace.</exception>
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(ApplicationMetadata))]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed with [DynamicDependency]")]
public static IHostBuilder UseApplicationMetadata(this IHostBuilder builder, string sectionName = DefaultSectionName)
{
_ = Throw.IfNull(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.AmbientMetadata;
using Microsoft.Extensions.Configuration;
using Microsoft.Shared.Diagnostics;
Expand All @@ -21,11 +20,6 @@ public static class ApplicationMetadataServiceCollectionExtensions
/// <param name="section">The configuration section to bind.</param>
/// <returns>The value of <paramref name="services"/>>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="section"/> or <paramref name="section"/> is <see langword="null"/>.</exception>
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(ApplicationMetadata))]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed with [DynamicDependency]")]
public static IServiceCollection AddApplicationMetadata(this IServiceCollection services, IConfigurationSection section)
{
_ = Throw.IfNull(services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<Workstream>Fundamentals</Workstream>
</PropertyGroup>

<PropertyGroup>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
Copy link
Member

Choose a reason for hiding this comment

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

Is this something we could configure at the global level as opposed to doing it in each individual project?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wouldn't recommend it for now. This source generator uses the experimental "Interceptors" feature in C# 12. That's why it isn't enabled by default everywhere. Plus we are only using it in 10 of the ~35 projects. So I don't think it makes sense to turn it on for all. Just like we don't turn on the Logging and Metrics source generators globally.

</PropertyGroup>

<PropertyGroup>
<Stage>normal</Stage>
<MinCodeCoverage>100</MinCodeCoverage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<InjectStringSyntaxAttributeOnLegacy>true</InjectStringSyntaxAttributeOnLegacy>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
<InjectSharedDiagnosticIds>true</InjectSharedDiagnosticIds>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public static IRedactionBuilder SetHmacRedactor(this IRedactionBuilder builder,
/// <returns>The value of <paramref name="builder" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder"/>, <paramref name="section" /> or <paramref name="classifications" /> are <see langword="null"/>.</exception>
[Experimental(diagnosticId: DiagnosticIds.Experiments.Compliance, UrlFormat = DiagnosticIds.UrlFormat)]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(HmacRedactorOptions))]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed with [DynamicDependency]")]
public static IRedactionBuilder SetHmacRedactor(this IRedactionBuilder builder, IConfigurationSection section, params DataClassificationSet[] classifications)
{
_ = Throw.IfNull(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Compliance.Classification;
using Microsoft.Extensions.Compliance.Redaction;
using Microsoft.Extensions.Compliance.Testing;
Expand Down Expand Up @@ -65,9 +64,6 @@ public static IRedactionBuilder SetFakeRedactor(this IRedactionBuilder builder,
/// <param name="classifications">The data classifications for which the redactor type should be used.</param>
/// <returns>The value of <paramref name="builder" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="section"/> is <see langword="null"/>.</exception>
[UnconditionalSuppressMessage("Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "The type is FakeRedactorOptions and we know it.")]
public static IRedactionBuilder SetFakeRedactor(this IRedactionBuilder builder, IConfigurationSection section, params DataClassificationSet[] classifications)
{
_ = Throw.IfNull(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<PropertyGroup>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<UseLoggingGenerator>true</UseLoggingGenerator>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
<InjectStringSyntaxAttributeOnLegacy>true</InjectStringSyntaxAttributeOnLegacy>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@ public static IServiceCollection ActivateKeyedSingleton<TService>(
/// <param name="serviceType">The type of the service to activate.</param>
/// <param name="serviceKey">An object used to uniquely identify the specific service.</param>
/// <returns>The value of <paramref name="services" />.</returns>
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed with [DynamicallyAccessedMembers]")]
public static IServiceCollection ActivateKeyedSingleton(
this IServiceCollection services,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type serviceType,
Type serviceType,
object? serviceKey)
{
_ = Throw.IfNull(services);
Expand All @@ -72,7 +68,7 @@ public static IServiceCollection ActivateKeyedSingleton(
.AddOptions<AutoActivatorOptions>()
.Configure(ao =>
{
var constructed = typeof(IEnumerable<>).MakeGenericType(serviceType);
var constructed = GetEnumerableServiceType(serviceType);
if (ao.KeyedAutoActivators.Contains((constructed, serviceKey)))
{
return;
Expand All @@ -85,6 +81,10 @@ public static IServiceCollection ActivateKeyedSingleton(
}

_ = ao.KeyedAutoActivators.Add((serviceType, serviceKey));

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "When IsDynamicCodeSupported is not supported, DependencyInjection ensures IEnumerable service types are not a ValueType.")]
static Type GetEnumerableServiceType(Type serviceType) => typeof(IEnumerable<>).MakeGenericType(serviceType);
});

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ public static IServiceCollection ActivateSingleton<TService>(this IServiceCollec
/// <param name="services">The service collection containing the service.</param>
/// <param name="serviceType">The type of the service to activate.</param>
/// <returns>The value of <paramref name="services" />.</returns>
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed with [DynamicallyAccessedMembers]")]
public static IServiceCollection ActivateSingleton(this IServiceCollection services, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type serviceType)
public static IServiceCollection ActivateSingleton(this IServiceCollection services, Type serviceType)
{
_ = Throw.IfNull(services);
_ = Throw.IfNull(serviceType);
Expand All @@ -69,7 +65,7 @@ public static IServiceCollection ActivateSingleton(this IServiceCollection servi
.AddOptions<AutoActivatorOptions>()
.Configure(ao =>
{
var constructed = typeof(IEnumerable<>).MakeGenericType(serviceType);
var constructed = GetEnumerableServiceType(serviceType);
if (ao.AutoActivators.Contains(constructed))
{
return;
Expand All @@ -82,6 +78,10 @@ public static IServiceCollection ActivateSingleton(this IServiceCollection servi
}

_ = ao.AutoActivators.Add(serviceType);

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "When IsDynamicCodeSupported is not supported, DependencyInjection ensures IEnumerable service types are not a ValueType.")]
static Type GetEnumerableServiceType(Type serviceType) => typeof(IEnumerable<>).MakeGenericType(serviceType);
});

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,29 @@ static HttpExceptionSummaryProvider()
};

var socketErrors = new Dictionary<SocketError, int>();
#if NET5_0_OR_GREATER
foreach (var socketError in Enum.GetValues<SocketError>())
{
#else
foreach (var v in Enum.GetValues(typeof(SocketError)))
{
var socketError = (SocketError)v!;
#endif
var name = socketError.ToString();

socketErrors[socketError] = descriptions.Count;
descriptions.Add(name);
}

var webStatuses = new Dictionary<WebExceptionStatus, int>();
#if NET5_0_OR_GREATER
foreach (var status in Enum.GetValues<WebExceptionStatus>())
{
#else
foreach (var v in Enum.GetValues(typeof(WebExceptionStatus)))
{
var status = (WebExceptionStatus)v!;
#endif
var name = status.ToString();

webStatuses[status] = descriptions.Count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<PropertyGroup>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<UseLoggingGenerator>true</UseLoggingGenerator>
<UseMetricsGenerator>true</UseMetricsGenerator>
<InjectSharedPools>true</InjectSharedPools>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<PropertyGroup>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<InjectSharedDataValidation>true</InjectSharedDataValidation>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Shared.Diagnostics;
Expand Down Expand Up @@ -56,11 +55,6 @@ public static IHealthChecksBuilder AddResourceUtilizationHealthCheck(this IHealt
/// <param name="section">Configuration for <see cref="ResourceUtilizationHealthCheckOptions"/>.</param>
/// <returns>The value of <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder" /> or <paramref name="section"/> is <see langword="null" />.</exception>
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(ResourceUtilizationHealthCheckOptions))]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed by [DynamicDependency]")]
public static IHealthChecksBuilder AddResourceUtilizationHealthCheck(
this IHealthChecksBuilder builder,
IConfigurationSection section)
Expand All @@ -80,11 +74,6 @@ public static IHealthChecksBuilder AddResourceUtilizationHealthCheck(
/// <param name="tags">A list of tags that can be used to filter health checks.</param>
/// <returns>The value of <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder" />, <paramref name="section"/> or <paramref name="tags"/> is <see langword="null" />.</exception>
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(ResourceUtilizationHealthCheckOptions))]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed by [DynamicDependency]")]
public static IHealthChecksBuilder AddResourceUtilizationHealthCheck(
this IHealthChecksBuilder builder,
IConfigurationSection section,
Expand All @@ -106,11 +95,6 @@ public static IHealthChecksBuilder AddResourceUtilizationHealthCheck(
/// <param name="tags">A list of tags that can be used to filter health checks.</param>
/// <returns>The value of <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder" />, <paramref name="section"/> or <paramref name="tags"/> is <see langword="null" />.</exception>
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(ResourceUtilizationHealthCheckOptions))]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed by [DynamicDependency]")]
public static IHealthChecksBuilder AddResourceUtilizationHealthCheck(
this IHealthChecksBuilder builder,
IConfigurationSection section,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static IServiceCollection AddKubernetesProbes(this IServiceCollection ser
_ = Throw.IfNull(services);
_ = Throw.IfNull(section);

return services.AddKubernetesProbes(section.Bind);
return services.AddKubernetesProbes(o => section.Bind(o));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<PropertyGroup>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<UseLoggingGenerator>true</UseLoggingGenerator>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
<InjectSharedDataValidation>true</InjectSharedDataValidation>
Expand All @@ -23,6 +24,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
<PackageReference Include="Microsoft.Bcl.TimeProvider" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="'$(TargetFramework)' == 'net462'" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<UseLoggingGenerator>true</UseLoggingGenerator>
<InjectSharedDataValidation>true</InjectSharedDataValidation>
<InjectSharedRentedSpan>true</InjectSharedRentedSpan>
Expand Down
Loading