Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/wiki/MSBuild-Environment-Variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ Some of the env variables listed here are unsupported, meaning there is no guara
- Set this to force all tasks to run out of process (except inline tasks).
- `MSBUILDFORCEINLINETASKFACTORIESOUTOFPROC`
- Set this to force all inline tasks to run out of process. It is not compatible with custom TaskFactories.
- `CONSOLE_USE_DEFAULT_ENCODING`
- It opts out automatic console encoding UTF-8. Make Console use default encoding in the system.
5 changes: 5 additions & 0 deletions src/Framework/Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public Traits()
/// </summary>
public readonly bool ForceTaskFactoryOutOfProc = Environment.GetEnvironmentVariable("MSBUILDFORCEINLINETASKFACTORIESOUTOFPROC") == "1";

/// <summary>
/// Make Console use default encoding in the system. It opts out automatic console encoding UTF-8.
/// </summary>
public readonly bool ConsoleUseDefaultEncoding = Environment.GetEnvironmentVariable("CONSOLE_USE_DEFAULT_ENCODING") == "1";
Comment thread
YuliiaKovalova marked this conversation as resolved.
Outdated

/// <summary>
/// Variables controlling opt out at the level of not initializing telemetry infrastructure. Set to "1" or "true" to opt out.
/// mirroring
Expand Down
29 changes: 16 additions & 13 deletions src/MSBuild/XMake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,23 +1934,26 @@ internal static void SetConsoleUI()
CultureInfo.CurrentUICulture = desiredCulture;
CultureInfo.DefaultThreadCurrentUICulture = desiredCulture;

Comment thread
GangWang01 marked this conversation as resolved.
if (!Traits.Instance.ConsoleUseDefaultEncoding)
{
#if RUNTIME_TYPE_NETCORE
if (EncodingUtilities.CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding())
if (EncodingUtilities.CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding())
#else
if (EncodingUtilities.CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding()
&& !CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Equals("en", StringComparison.InvariantCultureIgnoreCase))
if (EncodingUtilities.CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding()
&& !CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Equals("en", StringComparison.InvariantCultureIgnoreCase))
#endif
{
try
{
// Setting both encodings causes a change in the CHCP, making it so we don't need to P-Invoke CHCP ourselves.
Console.OutputEncoding = Encoding.UTF8;
// If the InputEncoding is not set, the encoding will work in CMD but not in PowerShell, as the raw CHCP page won't be changed.
Console.InputEncoding = Encoding.UTF8;
}
catch (Exception ex) when (ex is IOException || ex is SecurityException)
{
// The encoding is unavailable. Do nothing.
try
{
// Setting both encodings causes a change in the CHCP, making it so we don't need to P-Invoke CHCP ourselves.
Console.OutputEncoding = Encoding.UTF8;
// If the InputEncoding is not set, the encoding will work in CMD but not in PowerShell, as the raw CHCP page won't be changed.
Console.InputEncoding = Encoding.UTF8;
}
catch (Exception ex) when (ex is IOException || ex is SecurityException)
{
// The encoding is unavailable. Do nothing.
}
}
}

Expand Down