Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 13 additions & 10 deletions src/Cli/Microsoft.DotNet.Cli.Utils/UILanguageOverride.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ public static void Setup()
FlowOverrideToChildProcesses(language);
}

if (
!CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Equals("en", StringComparison.InvariantCultureIgnoreCase) &&
if (Environment.GetEnvironmentVariable("CONSOLE_USE_DEFAULT_ENCODING") != "1")
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting the environment variable name and value into named constants to avoid magic strings and improve maintainability. This duplicates the same magic strings used in Program.cs.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my comment on the MSBuild side - this feels wrong. If we know the locale that we're using, and we know that the locale doesn't support UTF8 output for some reason, then we shouldn't do the UTF8 encoding change.

{
if (
!CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Equals("en", StringComparison.InvariantCultureIgnoreCase) &&
#if NET
OperatingSystemSupportsUtf8()
OperatingSystemSupportsUtf8()
#else
CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding()
CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding()
#endif
)
{
// Setting both encodings causes a change in the CHCP, making it so we don't need to P-Invoke ourselves.
Console.OutputEncoding = s_defaultMultilingualEncoding;
Console.InputEncoding = s_defaultMultilingualEncoding;
// 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.
)
{
// Setting both encodings causes a change in the CHCP, making it so we don't need to P-Invoke ourselves.
Console.OutputEncoding = s_defaultMultilingualEncoding;
Console.InputEncoding = s_defaultMultilingualEncoding;
// 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.
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/Cli/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public static int Main(string[] args)
{
using AutomaticEncodingRestorer _ = new();

// Setting output encoding is not available on those platforms
if (UILanguageOverride.OperatingSystemSupportsUtf8())
if (Environment.GetEnvironmentVariable("CONSOLE_USE_DEFAULT_ENCODING") != "1")
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting the environment variable name and value into named constants to avoid magic strings and improve maintainability. This would also ensure consistency across the codebase where this same check is performed.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any env variables that are important to the dotnet CLI need a DOTNET_CLI_ prefix, and then we'd need to 'transform' them to the MSBuild-friendly version before we call out to MSBuild in the MSBuildforwardingApp.cs logic.

{
Console.OutputEncoding = Encoding.UTF8;
// Setting output encoding is not available on those platforms
if (UILanguageOverride.OperatingSystemSupportsUtf8())
{
Console.OutputEncoding = Encoding.UTF8;
}
}

DebugHelper.HandleDebugSwitch(ref args);
Expand Down