-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Trim warnings coming from TryGetAzureRoleInstanceIdNoThrow #2449
Comments
Agreed we'd throw on this...but if we removed it wouldn't it just complain about the next 5 reflection usages (or other things?) This is reflected so we don't have a dependency on Azure SDKs, so I'm not sure what the alternative is - is there an AOT friendly way to do an equivalent thing? |
You can use a If we did this, we would probably need to suppress the warnings that come when the APIs aren't there. FYI - @vitek-karas @agocke in case they have a better idea. BTW - is |
The way I got these warnings (and the warnings in mgravell/Pipelines.Sockets.Unofficial#72) was following https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming#show-all-warnings-with-sample-application for I did it really quick and I got the following extra warnings:
However, that code isn't used by |
I could be totally wrong, but this actually looks like it might be a safe pattern with some minor changes. If I understand the code correctly, it looks like the code is searching for a well-known type in a well-known assembly and then calling properties on it. If the code instead used I'm not sure what the failure case would be if the assembly isn't present -- but presumably that's still "AOT-safe" since that code path is a valid one. So maybe it would be safe to suppress a warning if one appeared in that situation. @vitek-karas or @MichalStrehovsky can correct me if I'm missing something here. |
Yes, this could be trim safe if it's written a little bit differently. Even just replacing the var type = asm.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment"); with var type = Type.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment, Microsoft.WindowsAzure.ServiceRuntime"); Would be enough for static analysis to kick in and stop warning (we can keep the |
I would actually question the use of the foreach. Note that it will only return already loaded assemblies. So if the app does include the assembly in question, but hasn't called anything in it yet the assembly will not show up in that list. Is that actually what the behavior should be in this case? |
- DefaultOptionsProvider.TryGetAzureRoleInstanceIdNoThrow use Type.GetType to check for Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment, so the trimmer knows to preserve this type, if it is in the app - ChannelMessage use the added public API for ChannelReader CanCount and Count, but fallback to reflection on netcoreapp3.1, since those APIs are not available on that runtime. Contributes to StackExchange#2449
I've opened #2451 to address the easy 2 warnings. The 3rd set will be a lot more work (and isn't needed for |
- DefaultOptionsProvider.TryGetAzureRoleInstanceIdNoThrow use Type.GetType to check for Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment, so the trimmer knows to preserve this type, if it is in the app - ChannelMessage use the added public API for ChannelReader CanCount and Count, but fallback to reflection on netcoreapp3.1, since those APIs are not available on that runtime. Contributes to #2449 This at least removes the warnings from using `Microsoft.Extensions.Caching.StackExchangeRedis`. We could also add a trimming test app as outlined in https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming#show-all-warnings-with-sample-application, along with a unit test that publishes the app and ensures there are no new warnings. LMK if you think this is valuable. There are still these warnings left in this library: ``` /_/src/StackExchange.Redis/ScriptParameterMapper.cs(173): Trim analysis warning IL2070: StackExchange.Redis.ScriptParameterMapper.IsValidParameterHash(Type,LuaScript,String&,String&): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.PublicNestedTypes', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.PublicEvents' in call to 'System.Type.GetMember(String)'. The parameter 't' of method 'StackExchange.Redis.ScriptParameterMapper.IsValidParameterHash(Type,LuaScript,String&,String&)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\git\azure-activedirectory-identitymodel-extensions-for-dotnet\test\Microsoft.IdentityModel.AotCompatibility.TestApp\Microsoft.IdentityModel.AotCompatibility.TestApp.csproj] /_/src/StackExchange.Redis/ScriptParameterMapper.cs(227): Trim analysis warning IL2070: StackExchange.Redis.ScriptParameterMapper.GetParameterExtractor(Type,LuaScript): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.PublicNestedTypes', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.PublicEvents' in call to 'System.Type.GetMember(String)'. The parameter 't' of method 'StackExchange.Redis.ScriptParameterMapper.GetParameterExtractor(Type,LuaScript)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\git\azure-activedirectory-identitymodel-extensions-for-dotnet\test\Microsoft.IdentityModel.AotCompatibility.TestApp\Microsoft.IdentityModel.AotCompatibility.TestApp.csproj] /_/src/StackExchange.Redis/ScriptParameterMapper.cs(260): Trim analysis warning IL2026: StackExchange.Redis.ScriptParameterMapper.GetParameterExtractor(Type,LuaScript): Using member 'System.Linq.Expressions.Expression.Property(Expression,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [C:\git\azure-activedirectory-identitymodel-extensions-for-dotnet\test\Microsoft.IdentityModel.AotCompatibility.TestApp\Microsoft.IdentityModel.AotCompatibility.TestApp.csproj] /_/src/StackExchange.Redis/ScriptParameterMapper.cs(261): Trim analysis warning IL2026: StackExchange.Redis.ScriptParameterMapper.GetParameterExtractor(Type,LuaScript): Using member 'System.Linq.Expressions.Expression.Call(Expression,String,Type[],Expression[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [C:\git\azure-activedirectory-identitymodel-extensions-for-dotnet\test\Microsoft.IdentityModel.AotCompatibility.TestApp\Microsoft.IdentityModel.AotCompatibility.TestApp.csproj] ``` Fixing those will require a bit more work, as the whole LuaScript functionality looks like it needs to be marked `RequiresUnreferencedCode`. cc @mgravell @NickCraver
This version contains fixes for supporting NativeAOT. Contributes to StackExchange#2449
This version contains fixes for supporting NativeAOT. Contributes to #2449
In trying to use
Microsoft.Extensions.Caching.StackExchangeRedis
in a Native AOT app (see dotnet/aspnetcore#45910), I'm getting the following warnings from this code:StackExchange.Redis/src/StackExchange.Redis/Configuration/DefaultOptionsProvider.cs
Lines 208 to 242 in b1fddf3
I'm not sure exactly what this code is for. If it isn't necessary from a trimmed/AOT app, maybe the warnings could just be suppressed? Either way, it would be good to address these warnings so devs using Redis caching in Native AOT apps don't get the warnings.
cc @mgravell
The text was updated successfully, but these errors were encountered: