runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Unicode hello world not working

Open schellap opened this issue 10 years ago • 7 comments

Encountered this while fixing https://github.com/dotnet/cli/issues/2649

Cc @gkhanna79 @eerhardt @gregg-miskelly @janvorli

Repro Steps

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hélló");
        }
    }
}
dotnet new
replace contents of Program.cs with this
dotnet restore
dotnet run [or] corerun with CORE_ROOT pointing to dependencies.

Expected

Desktop behavior (Compile this using Full FX)

> "c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe" /o Program.cs /nologo
> Program.exe
Hélló

Actual

Hélló

schellap avatar Apr 25 '16 08:04 schellap

/cc @tarekgh

I tried adding

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

before calling Console.WriteLine, but that didn't seem to fix it.

eerhardt avatar Apr 25 '16 14:04 eerhardt

After further investigation, the CoreFX/CoreCLR is working as designed here.

As stated above, I needed to add:

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

to the test app. But I also needed the same change in the https://github.com/dotnet/cli code as well. The reasoning is because when you run dotnet run, more than 1 process is executed. The first process is the CLI process which ensures your app is built and up-to-date. The CLI process then spawns a new process to run the actual console app in, and then it forwards the standard out and error to the original CLI calling process. Both processes need the CodePagesEncodingProvider registered for the console output to be correct.

This bug will be fixed with https://github.com/dotnet/cli/issues/2486, which will add the CodePagesEncodingProvider to the CLI code.

The need to register the CodePagesEncodingProvider in the underlying app is a known .NET Core change from the desktop framework. See https://github.com/dotnet/corefx/issues/7935#issuecomment-212678663.

Closing.

eerhardt avatar Apr 25 '16 15:04 eerhardt

Shouldn't the runtime rather ensure the console encoding is UTF-8 by default?

miloush avatar Apr 25 '16 15:04 miloush

Shouldn't the runtime rather ensure the console encoding is UTF-8 by default?

Usually we follow the underlying system. especially in Windows the Console is not fully working as desired with UTF8 because of the lack of the fonts support especially when running on non-English platforms. Also setting UTF8 will not totally avoid the problem because the app can still decide to set the Console codepage back to any other codepage.

tarekgh avatar Apr 25 '16 15:04 tarekgh

setting UTF8 will not totally avoid the problem because the app can still decide to set the Console codepage back to any other codepage.

Well but for that you would need to get in the CodePagesEncodingProvider anyway I guess, so you are fine.

the lack of the fonts support especially when running on non-English platforms

It's true that the bitmap fonts are not doing the best job showing Unicode (unlike the other ones people can switch the Console into). But, for the cases where it goes wrong, is H?ll? really that much worse than Hélló?

It's clear that no-one is going to fix the Console in underlying system for many reasons, it's not that clear to me why this legacy of codepages should be dragged around into a brand new environment as a default behavior... so I thought I would question that. :)

miloush avatar Apr 25 '16 15:04 miloush

It's clear that no-one is going to fix the Console in underlying system for many reasons, it's not that clear to me why this legacy of codepages should be dragged around into a brand new environment as a default behavior... so I thought I would question that. :)

I am really fully agree with the idea. we have been trying for long time to get people use Unicode instead of the ansi codepages. the only concern here is I recall at some point there were some trial from Windows side to have the console uses UTF8 all the time but this for some reasons didn't work well (or needed more work to support that). this is why we have to be careful if we need to force the default different than the system.

tarekgh avatar Apr 25 '16 16:04 tarekgh

Don't you have to specifically target the .NET Core to be able to run under this new runtime? Because I can understand that breaking working applications is a serious drawback, but if there are no applications yet, that looks like this would be the only possible time to do such "change"...

miloush avatar Apr 25 '16 16:04 miloush