Skip to content

Commit

Permalink
Couple fixes for UseSystemResourceKeys (#103463)
Browse files Browse the repository at this point in the history
Fixes #102303.

* Set a default value for the feature switch
* Make it possible to preinitialize the static constructor
* Fix generation of substitutions XML
* Update SR.vb to match SR.cs
* Fix resources in System.Diagnostics.FileVersionInfo.csproj

@dotnet/illink
  • Loading branch information
MichalStrehovsky committed Jun 17, 2024
1 parent ac996f3 commit a0b8890
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
8 changes: 7 additions & 1 deletion eng/ILLink.Substitutions.Resources.template
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<linker>
<!-- System.Resources.UseSystemResourceKeys removes resource strings and instead uses the resource key as the exception message -->
<assembly fullname="{AssemblyName}" feature="System.Resources.UseSystemResourceKeys" featurevalue="true">
<!-- System.Resources.UseSystemResourceKeys removes resource strings and instead uses the resource key as the exception message -->
<resource name="{StringResourcesName}.resources" action="remove" />
<type fullname="System.SR">
<method signature="System.Boolean UsingResourceKeys()" body="stub" value="true" />
<method signature="System.Boolean GetUsingResourceKeysSwitchValue()" body="stub" value="true" />
</type>
</assembly>
<assembly fullname="{AssemblyName}" feature="System.Resources.UseSystemResourceKeys" featurevalue="false">
<type fullname="System.SR">
<method signature="System.Boolean UsingResourceKeys()" body="stub" value="false" />
<method signature="System.Boolean GetUsingResourceKeysSwitchValue()" body="stub" value="false" />
</type>
</assembly>
</linker>
2 changes: 1 addition & 1 deletion eng/illink.targets
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<ILLinkRewritePDBs Condition="'$(ILLinkRewritePDBs)' == ''">true</ILLinkRewritePDBs>

<ILLinkResourcesSubstitutionIntermediatePath>$(IntermediateOutputPath)ILLink.Resources.Substitutions.xml</ILLinkResourcesSubstitutionIntermediatePath>
<GenerateResourcesSubstitutions Condition="'$(GenerateResourcesSubstitutions)' == '' and '$(StringResourcesPath)' != ''">true</GenerateResourcesSubstitutions>
<GenerateResourcesSubstitutions Condition="'$(GenerateResourcesSubstitutions)' == '' and '$(StringResourcesPath)' != '' and '$(OmitResources)' != 'true'">true</GenerateResourcesSubstitutions>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion src/libraries/Common/src/System/SR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ namespace System
{
internal static partial class SR
{
private static readonly bool s_usingResourceKeys = AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", out bool usingResourceKeys) ? usingResourceKeys : false;
private static readonly bool s_usingResourceKeys = GetUsingResourceKeysSwitchValue();

// This method is a target of ILLink substitution.
private static bool GetUsingResourceKeysSwitchValue() => AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", out bool usingResourceKeys) ? usingResourceKeys : false;

// This method is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
// by default it returns the value of System.Resources.UseSystemResourceKeys AppContext switch or false if not specified.
Expand Down
22 changes: 15 additions & 7 deletions src/libraries/Common/src/System/SR.vb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ Imports System.Resources
Namespace System

Friend NotInheritable Class SR
' This method is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
' by default it returns false.
Private Shared ReadOnly s_usingResourceKeys As Boolean = GetUsingResourceKeysSwitchValue()

Private Shared Function GetUsingResourceKeysSwitchValue() As Boolean
Dim usingResourceKeys As Boolean
If (AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", usingResourceKeys)) Then
Return usingResourceKeys
End If

Return False
End Function

' This method Is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
' by default it returns the value of System.Resources.UseSystemResourceKeys AppContext switch Or false if Not specified.
' Native code generators can replace the value this returns based on user input at the time of native code generation.
' Marked as NoInlining because if this is used in an AoT compiled app that is not compiled into a single file, the user
' could compile each module with a different setting for this. We want to make sure there's a consistent behavior
' that doesn't depend on which native module this method got inlined into.
<Global.System.Runtime.CompilerServices.MethodImpl(Global.System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>
' The trimming tools are also capable of replacing the value of this method when the application Is being trimmed.
Public Shared Function UsingResourceKeys() As Boolean
Return False
Return s_usingResourceKeys
End Function

Friend Shared Function GetResourceString(ByVal resourceKey As String, Optional ByVal defaultString As String = Nothing) As String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' == ''">SR.DiagnosticsFileVersionInfo_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' == ''">SR.DiagnosticsFileVersionInfo_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
<OmitResources Condition="'$(TargetPlatformIdentifier)' == 'windows'">true</OmitResources>
</PropertyGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
<OutputType>Exe</OutputType>
<CLRTestPriority>0</CLRTestPriority>
<DefineConstants>$(DefineConstants);RESOURCE_KEYS</DefineConstants>
<UseSystemResourceKeys>true</UseSystemResourceKeys>

<!-- Requires the framework to also be compiled with UseSystemResourceKeys -->
<CLRTestTargetUnsupported Condition="'$(IlcMultiModule)' == 'true'">true</CLRTestTargetUnsupported>
<!-- Test infra issue on apple devices: https://github.com/dotnet/runtime/issues/89917 -->
<CLRTestTargetUnsupported Condition="'$(TargetsAppleMobile)' == 'true'">true</CLRTestTargetUnsupported>
</PropertyGroup>

<ItemGroup>
<IlcArg Include="--feature:System.Resources.UseSystemResourceKeys=true" />
</ItemGroup>

<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<_ComObjectDescriptorSupport Condition="'$(_ComObjectDescriptorSupport)' == ''">false</_ComObjectDescriptorSupport>
<_DesignerHostSupport Condition="'$(_DesignerHostSupport)' == ''">false</_DesignerHostSupport>
<_DefaultValueAttributeSupport Condition="'$(_DefaultValueAttributeSupport)' == ''">false</_DefaultValueAttributeSupport>
<UseSystemResourceKeys Condition="'$(UseSystemResourceKeys)' == ''">false</UseSystemResourceKeys>
</PropertyGroup>


Expand Down

0 comments on commit a0b8890

Please sign in to comment.