Skip to content

[std] Fix unicode passed through stdout/stderr #917

Merged
yuxiaomao merged 2 commits intoHaxeFoundation:masterfrom
tobil4sk:fix/stdout-stderr-unicode
Apr 3, 2026
Merged

[std] Fix unicode passed through stdout/stderr #917
yuxiaomao merged 2 commits intoHaxeFoundation:masterfrom
tobil4sk:fix/stdout-stderr-unicode

Conversation

@tobil4sk
Copy link
Copy Markdown
Member

These patches are needed to restore ipc sys tests in the haxe repo.

The windows builtin w-string functions don't produce actual utf-8, so
it's better to use hashlink's implementations. This allows us to print
directly without windows converting the buffer contents.

Setting the locale interferes with printf which breaks unicode IPC, so
we also need to remove that.
This _setmode method used to be used for print, but it was disabled by
default due to threading issues:
9d21b13

Using fwrite avoids the utf8 bytes being converted to a different
encoding by windows.
@ncannasse
Copy link
Copy Markdown
Member

Does the unit tests actually test writing on stdout/stderr with sys.io.File.stdout().write ? Because the code you are removing is intended for that. Also does it work with a not Unicode terminal like cmd ? You should be able to do hl app.hl >file.txt and get proper unicode this way.

@tobil4sk
Copy link
Copy Markdown
Member Author

Does the unit tests actually test writing on stdout/stderr with sys.io.File.stdout().write ?

Yes, it is tested in these cases:

https://github.com/HaxeFoundation/haxe/blob/d55a4dd07b15b2c00ddb5d7a62e378ed7369cb01/tests/sys/src/TestUnicode.hx#L297-L299

https://github.com/HaxeFoundation/haxe/blob/d55a4dd07b15b2c00ddb5d7a62e378ed7369cb01/tests/sys/src/UtilityProcess.hx#L184

Without this file_write change, it writes garbled text to stderr/stdout.

Because the code you are removing is intended for that.

Note, hl_sys_print used to use a similar thing but it caused issues so it got disabled already: 9d21b13

Also does it work with a not Unicode terminal like cmd

I don't think this fixes how unicode is displayed on the console, it only makes sure that the correct utf-8 bytes are put into the buffer (so piping to a file or another process should work). For displaying it in cmd or powershell, that is a different issue and will probably require a fix like hxcpp: HaxeFoundation/hxcpp#1307

You should be able to do hl app.hl >file.txt and get proper unicode this way.

This should work, I will double check this.

@tobil4sk
Copy link
Copy Markdown
Member Author

function main() {
    trace("Hello world!");
    Sys.println("éá🙄🦉");
    Sys.stdout().writeString("éá🙄🦉");
}

Without this patch hl hello.hl > file.txt produces:

HelloWorld.hx:3: Hello world!
��????
éá🙄🦀͍͍ﯮﯮﯝݝ

With this patch hl hello.hl > file.txt produces:

HelloWorld.hx:3: Hello world!
éá🙄🦉
éá🙄🦉

@yuxiaomao
Copy link
Copy Markdown
Collaborator

I need this fix, and I confirm that it fix the described bug. Utf-8 text are displayed correcly in git-bash (not in cwd, as expected).

@yuxiaomao yuxiaomao merged commit 4bfc945 into HaxeFoundation:master Apr 3, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants