diff --git a/src/MSBuild.UnitTests/XMake_Tests.cs b/src/MSBuild.UnitTests/XMake_Tests.cs index c6029b73765..86379f90295 100644 --- a/src/MSBuild.UnitTests/XMake_Tests.cs +++ b/src/MSBuild.UnitTests/XMake_Tests.cs @@ -642,6 +642,30 @@ public void SetConsoleUICulture() thisThread.CurrentUICulture = originalUICulture; } + /// + /// We shouldn't change the UI culture if the current UI culture is invariant. + /// In other cases, we can get an exception on CultureInfo creation when System.Globalization.Invariant enabled. + /// + + [Fact] + public void SetConsoleUICultureInInvariantCulture() + { + Thread thisThread = Thread.CurrentThread; + + // Save the current UI culture, so we can restore it at the end of this unit test. + CultureInfo originalUICulture = thisThread.CurrentUICulture; + + thisThread.CurrentUICulture = CultureInfo.InvariantCulture; + MSBuildApp.SetConsoleUI(); + + // Make sure we don't change culture. + thisThread.CurrentUICulture.ShouldBe(CultureInfo.InvariantCulture); + + // Restore the current UI culture back to the way it was at the beginning of this unit test. + thisThread.CurrentUICulture = originalUICulture; + } + + #if FEATURE_SYSTEM_CONFIGURATION /// /// Invalid configuration file should not dump stack. diff --git a/src/MSBuild/XMake.cs b/src/MSBuild/XMake.cs index 0d0fa710bd2..dc714fe60e7 100644 --- a/src/MSBuild/XMake.cs +++ b/src/MSBuild/XMake.cs @@ -1693,7 +1693,8 @@ internal static void SetConsoleUI() && codepage != thisThread.CurrentUICulture.TextInfo.OEMCodePage && - codepage != thisThread.CurrentUICulture.TextInfo.ANSICodePage) + codepage != thisThread.CurrentUICulture.TextInfo.ANSICodePage + && !Equals(CultureInfo.InvariantCulture, thisThread.CurrentUICulture)) { thisThread.CurrentUICulture = new CultureInfo("en-US"); return;