-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
RequiresUnreferencedCode not suppressing warning from explicitly implemented generic method using lambda method #98368
Comments
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsDescriptionWhen trying to annotate some Azure SDK code, I'm hitting an errant warning that shouldn't be produced. The method in question has a The original code is here: https://github.com/Azure/azure-sdk-for-net/blob/a3a24ef9cd1af7ad7ef21a82a51426bb72ba59d3/sdk/extensions/Microsoft.Extensions.Azure/src/AzureClientFactoryBuilder.cs#L33-L38 I've extracted what I think is a minimal repro using this code below. Reproduction Steps<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!--either setting will produce the errant warning-->
<!--<PublishAot>true</PublishAot>-->
<PublishTrimmed>true</PublishTrimmed>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
</ItemGroup>
</Project> using Microsoft.Extensions.Configuration;
using System.Diagnostics.CodeAnalysis;
((IAzureClientFactoryBuilderWithConfiguration<IConfiguration>)new AzureClientFactoryBuilder()).RegisterClientFactory<object, object>(null!);
public interface IAzureClientBuilder<TClient, TOptions> where TOptions : class
{
}
public abstract class TokenCredential { }
public interface IAzureClientFactoryBuilderWithCredential
{
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(Func<TOptions, TokenCredential, TClient> clientFactory, bool requiresCredential = true) where TOptions : class;
}
public interface IAzureClientFactoryBuilderWithConfiguration<in TConfiguration>
{
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TOptions>(TConfiguration configuration) where TOptions : class;
}
class AzureClientFactoryBuilder : IAzureClientFactoryBuilderWithConfiguration<IConfiguration>, IAzureClientFactoryBuilderWithCredential
{
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilderWithConfiguration<IConfiguration>.RegisterClientFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TOptions>(
IConfiguration configuration)
{
var clientBuilder = ((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>(
(options, credentials) => (TClient)ClientFactory.CreateClient(typeof(TClient), typeof(TOptions), options, configuration, credentials));
return clientBuilder;
}
// NOTE: commenting out the above explicitly implemented method, and uncommenting the below method, will make the warning go away
//[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
//[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
//public IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TOptions>(IConfiguration configuration) where TOptions : class
//{
// var clientBuilder = ((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>(
// (options, credentials) => (TClient)ClientFactory.CreateClient(typeof(TClient), typeof(TOptions), options, configuration, credentials));
// return clientBuilder;
//}
public IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(Func<TOptions, TokenCredential, TClient> clientFactory, bool requiresCredential = true) where TOptions : class
{
throw new NotImplementedException();
}
}
internal static class ClientFactory
{
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
public static object CreateClient(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type clientType,
Type optionsType,
object options,
IConfiguration configuration,
TokenCredential credential)
{
throw new NotImplementedException();
}
} Expected behaviorThe only trim/AOT warnings emitted should be coming from line 4 - the only caller of an incompatible method that doesn't have a RequiresUnreferencedCode attribute. Actual behaviorGetting a warning on line 4, as expected. But also getting a warning on line 32, which shouldn't be emitted since the method has
Regression?I see this both in net7.0 and net8.0. So I don't believe it is a regression. Known WorkaroundsNo response ConfigurationNo response Other informationcc @sbomer @agocke @MichalStrehovsky @jtschuster
|
Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas Issue DetailsDescriptionWhen trying to annotate some Azure SDK code, I'm hitting an errant warning that shouldn't be produced. The method in question has a The original code is here: https://github.com/Azure/azure-sdk-for-net/blob/a3a24ef9cd1af7ad7ef21a82a51426bb72ba59d3/sdk/extensions/Microsoft.Extensions.Azure/src/AzureClientFactoryBuilder.cs#L33-L38 I've extracted what I think is a minimal repro using this code below. Reproduction Steps<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!--either setting will produce the errant warning-->
<!--<PublishAot>true</PublishAot>-->
<PublishTrimmed>true</PublishTrimmed>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
</ItemGroup>
</Project> using Microsoft.Extensions.Configuration;
using System.Diagnostics.CodeAnalysis;
((IAzureClientFactoryBuilderWithConfiguration<IConfiguration>)new AzureClientFactoryBuilder()).RegisterClientFactory<object, object>(null!);
public interface IAzureClientBuilder<TClient, TOptions> where TOptions : class
{
}
public abstract class TokenCredential { }
public interface IAzureClientFactoryBuilderWithCredential
{
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(Func<TOptions, TokenCredential, TClient> clientFactory, bool requiresCredential = true) where TOptions : class;
}
public interface IAzureClientFactoryBuilderWithConfiguration<in TConfiguration>
{
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TOptions>(TConfiguration configuration) where TOptions : class;
}
class AzureClientFactoryBuilder : IAzureClientFactoryBuilderWithConfiguration<IConfiguration>, IAzureClientFactoryBuilderWithCredential
{
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilderWithConfiguration<IConfiguration>.RegisterClientFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TOptions>(
IConfiguration configuration)
{
var clientBuilder = ((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>(
(options, credentials) => (TClient)ClientFactory.CreateClient(typeof(TClient), typeof(TOptions), options, configuration, credentials));
return clientBuilder;
}
// NOTE: commenting out the above explicitly implemented method, and uncommenting the below method, will make the warning go away
//[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
//[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
//public IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TOptions>(IConfiguration configuration) where TOptions : class
//{
// var clientBuilder = ((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>(
// (options, credentials) => (TClient)ClientFactory.CreateClient(typeof(TClient), typeof(TOptions), options, configuration, credentials));
// return clientBuilder;
//}
public IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(Func<TOptions, TokenCredential, TClient> clientFactory, bool requiresCredential = true) where TOptions : class
{
throw new NotImplementedException();
}
}
internal static class ClientFactory
{
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
public static object CreateClient(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type clientType,
Type optionsType,
object options,
IConfiguration configuration,
TokenCredential credential)
{
throw new NotImplementedException();
}
} Expected behaviorThe only trim/AOT warnings emitted should be coming from line 4 - the only caller of an incompatible method that doesn't have a RequiresUnreferencedCode attribute. Actual behaviorGetting a warning on line 4, as expected. But also getting a warning on line 32, which shouldn't be emitted since the method has
Regression?I see this both in net7.0 and net8.0. So I don't believe it is a regression. Known WorkaroundsNo response ConfigurationNo response Other informationcc @sbomer @agocke @MichalStrehovsky @jtschuster
|
Minimal repro: using System.Diagnostics.CodeAnalysis;
((IG<object>)new C()).M();
interface IG<T> {
[RequiresUnreferencedCode("IG.M")]
void M();
}
class C : IG<object>
{
[RequiresUnreferencedCode("IG<object>.M")]
void IG<object>.M() {
var f = () => RUC();
}
[RequiresUnreferencedCode("RUC")]
static void RUC() => throw null;
} |
Description
When trying to annotate some Azure SDK code, I'm hitting an errant warning that shouldn't be produced. The method in question has a
RequiresUnreferencedCode
attribute applied to it, but there is still a trim warning sneaking through (in bothPublishTrimmed
andPublishAot
apps).The original code is here: https://github.com/Azure/azure-sdk-for-net/blob/a3a24ef9cd1af7ad7ef21a82a51426bb72ba59d3/sdk/extensions/Microsoft.Extensions.Azure/src/AzureClientFactoryBuilder.cs#L33-L38.
Azure/azure-sdk-for-net#41936 is opened to annotate this code for trimming/AOT, where I ran into this issue.
I've extracted what I think is a minimal repro using this code below.
Reproduction Steps
Expected behavior
The only trim/AOT warnings emitted should be coming from line 4 - the only caller of an incompatible method that doesn't have a RequiresUnreferencedCode attribute.
Actual behavior
Getting a warning on line 4, as expected.
But also getting a warning on line 32, which shouldn't be emitted since the method has
RequiresUnreferencedCode
on it:Regression?
I see this both in net7.0 and net8.0. So I don't believe it is a regression.
Known Workarounds
No response
Configuration
No response
Other information
cc @sbomer @agocke @MichalStrehovsky @jtschuster
The text was updated successfully, but these errors were encountered: