-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix InvariantGlobalization=false in a trimmed app (#53453)
* Fix InvariantGlobalization=false in a trimmed app. Move the LoadICU logic into a static ctor, so it runs early in the process, but not as part of querying the GlobalizationMode.Invariant property. This allows for LoadICU to still be invoked, even when the app is trimmed and GlobalizationMode.Invariant is hard-coded by the linker. While I was changing this code, I also removed the workaround in Browser builds for swallowing errors being returned from LoadICU. Fix #49073 Fix #49391 * Add trimming tests for InvariantGlobalization * Update the LoadICU message for mobile workloads. * Respond to PR feedback. - Split the substitutions of GlobalizationMode.Invariant between true and false - Add a trimming test that ensures Invariant=true trims everything it is supposed to * Add checks for all mobile platforms
- Loading branch information
Showing
7 changed files
with
131 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/libraries/System.Runtime/tests/TrimmingTests/InvariantGlobalizationFalse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Threading; | ||
|
||
/// <summary> | ||
/// Ensures setting InvariantGlobalization = false still works in a trimmed app. | ||
/// </summary> | ||
class Program | ||
{ | ||
static int Main(string[] args) | ||
{ | ||
// since we are using Invariant GlobalizationMode = false, setting the culture matters. | ||
// The app will always use the current culture, so in the Turkish culture, 'i' ToUpper will NOT be "I" | ||
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR"); | ||
if ("i".ToUpper() == "I") | ||
{ | ||
// 'i' ToUpper was "I", but shouldn't be in the Turkish culture, so fail | ||
return -1; | ||
} | ||
|
||
return 100; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/libraries/System.Runtime/tests/TrimmingTests/InvariantGlobalizationTrue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using System.Threading; | ||
|
||
/// <summary> | ||
/// Ensures setting InvariantGlobalization = true still works in a trimmed app. | ||
/// </summary> | ||
class Program | ||
{ | ||
static int Main(string[] args) | ||
{ | ||
// since we are using Invariant GlobalizationMode = true, setting the culture doesn't matter. | ||
// The app will always use Invariant mode, so even in the Turkish culture, 'i' ToUpper will be "I" | ||
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR"); | ||
if ("i".ToUpper() != "I") | ||
{ | ||
// 'i' ToUpper was not "I", so fail | ||
return -1; | ||
} | ||
|
||
// Ensure the internal GlobalizationMode class is trimmed correctly | ||
Type globalizationMode = GetCoreLibType("System.Globalization.GlobalizationMode"); | ||
const BindingFlags allStatics = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; | ||
foreach (MemberInfo member in globalizationMode.GetMembers(allStatics)) | ||
{ | ||
// properties and their backing getter methods are OK | ||
if (member is PropertyInfo || member.Name.StartsWith("get_")) | ||
{ | ||
continue; | ||
} | ||
|
||
if (OperatingSystem.IsWindows()) | ||
{ | ||
// Windows still contains a static cctor and a backing field for UseNls | ||
if (member is ConstructorInfo || (member is FieldInfo field && field.Name.Contains("UseNls"))) | ||
{ | ||
continue; | ||
} | ||
} | ||
|
||
// Some unexpected member was left on GlobalizationMode, fail | ||
Console.WriteLine($"Member '{member.Name}' was not trimmed from GlobalizationMode, but should have been."); | ||
return -2; | ||
} | ||
|
||
return 100; | ||
} | ||
|
||
private static Type GetCoreLibType(string name) => | ||
typeof(object).Assembly.GetType(name, throwOnError: true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters