Skip to content

Commit a0b8890

Browse files
Couple fixes for UseSystemResourceKeys (#103463)
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
1 parent ac996f3 commit a0b8890

File tree

7 files changed

+31
-15
lines changed

7 files changed

+31
-15
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<linker>
22
<!-- System.Resources.UseSystemResourceKeys removes resource strings and instead uses the resource key as the exception message -->
33
<assembly fullname="{AssemblyName}" feature="System.Resources.UseSystemResourceKeys" featurevalue="true">
4-
<!-- System.Resources.UseSystemResourceKeys removes resource strings and instead uses the resource key as the exception message -->
54
<resource name="{StringResourcesName}.resources" action="remove" />
65
<type fullname="System.SR">
76
<method signature="System.Boolean UsingResourceKeys()" body="stub" value="true" />
7+
<method signature="System.Boolean GetUsingResourceKeysSwitchValue()" body="stub" value="true" />
8+
</type>
9+
</assembly>
10+
<assembly fullname="{AssemblyName}" feature="System.Resources.UseSystemResourceKeys" featurevalue="false">
11+
<type fullname="System.SR">
12+
<method signature="System.Boolean UsingResourceKeys()" body="stub" value="false" />
13+
<method signature="System.Boolean GetUsingResourceKeysSwitchValue()" body="stub" value="false" />
814
</type>
915
</assembly>
1016
</linker>

eng/illink.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<ILLinkRewritePDBs Condition="'$(ILLinkRewritePDBs)' == ''">true</ILLinkRewritePDBs>
3838

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

4343
<ItemGroup>

src/libraries/Common/src/System/SR.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ namespace System
77
{
88
internal static partial class SR
99
{
10-
private static readonly bool s_usingResourceKeys = AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", out bool usingResourceKeys) ? usingResourceKeys : false;
10+
private static readonly bool s_usingResourceKeys = GetUsingResourceKeysSwitchValue();
11+
12+
// This method is a target of ILLink substitution.
13+
private static bool GetUsingResourceKeysSwitchValue() => AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", out bool usingResourceKeys) ? usingResourceKeys : false;
1114

1215
// This method is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
1316
// by default it returns the value of System.Resources.UseSystemResourceKeys AppContext switch or false if not specified.

src/libraries/Common/src/System/SR.vb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@ Imports System.Resources
1111
Namespace System
1212

1313
Friend NotInheritable Class SR
14-
' This method is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
15-
' by default it returns false.
14+
Private Shared ReadOnly s_usingResourceKeys As Boolean = GetUsingResourceKeysSwitchValue()
15+
16+
Private Shared Function GetUsingResourceKeysSwitchValue() As Boolean
17+
Dim usingResourceKeys As Boolean
18+
If (AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", usingResourceKeys)) Then
19+
Return usingResourceKeys
20+
End If
21+
22+
Return False
23+
End Function
24+
25+
' This method Is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
26+
' by default it returns the value of System.Resources.UseSystemResourceKeys AppContext switch Or false if Not specified.
1627
' Native code generators can replace the value this returns based on user input at the time of native code generation.
17-
' Marked as NoInlining because if this is used in an AoT compiled app that is not compiled into a single file, the user
18-
' could compile each module with a different setting for this. We want to make sure there's a consistent behavior
19-
' that doesn't depend on which native module this method got inlined into.
20-
<Global.System.Runtime.CompilerServices.MethodImpl(Global.System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>
28+
' The trimming tools are also capable of replacing the value of this method when the application Is being trimmed.
2129
Public Shared Function UsingResourceKeys() As Boolean
22-
Return False
30+
Return s_usingResourceKeys
2331
End Function
2432

2533
Friend Shared Function GetResourceString(ByVal resourceKey As String, Optional ByVal defaultString As String = Nothing) As String

src/libraries/System.Diagnostics.FileVersionInfo/src/System.Diagnostics.FileVersionInfo.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
1010
<PropertyGroup>
1111
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
12-
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' == ''">SR.DiagnosticsFileVersionInfo_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
12+
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' == ''">SR.DiagnosticsFileVersionInfo_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
13+
<OmitResources Condition="'$(TargetPlatformIdentifier)' == 'windows'">true</OmitResources>
1314
</PropertyGroup>
1415

1516
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">

src/tests/nativeaot/SmokeTests/FrameworkStrings/UseSystemResourceKeys.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
<OutputType>Exe</OutputType>
44
<CLRTestPriority>0</CLRTestPriority>
55
<DefineConstants>$(DefineConstants);RESOURCE_KEYS</DefineConstants>
6+
<UseSystemResourceKeys>true</UseSystemResourceKeys>
67

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

13-
<ItemGroup>
14-
<IlcArg Include="--feature:System.Resources.UseSystemResourceKeys=true" />
15-
</ItemGroup>
16-
1714
<ItemGroup>
1815
<Compile Include="Program.cs" />
1916
</ItemGroup>

src/tools/illink/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Copyright (c) .NET Foundation. All rights reserved.
5555
<_ComObjectDescriptorSupport Condition="'$(_ComObjectDescriptorSupport)' == ''">false</_ComObjectDescriptorSupport>
5656
<_DesignerHostSupport Condition="'$(_DesignerHostSupport)' == ''">false</_DesignerHostSupport>
5757
<_DefaultValueAttributeSupport Condition="'$(_DefaultValueAttributeSupport)' == ''">false</_DefaultValueAttributeSupport>
58+
<UseSystemResourceKeys Condition="'$(UseSystemResourceKeys)' == ''">false</UseSystemResourceKeys>
5859
</PropertyGroup>
5960

6061

0 commit comments

Comments
 (0)