You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Piping Closure compiler stderr output to Python with Unicode characters on Windows problem #4159
C:\emsdk\emscripten\main>python a.py
Traceback (most recent call last):
File "C:\emsdk\emscripten\main\a.py", line 2, in <module>
subprocess.run(['npx', 'google-closure-compiler','--charset=UTF8','--js','a.js','--js_output_file','o.js'], encoding='utf-8', stderr=subprocess.PIPE, shell=True)
File "C:\Python311\Lib\subprocess.py", line 550, in run
stdout, stderr = process.communicate(input, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\subprocess.py", line 1197, in communicate
stderr = self.stderr.read()
^^^^^^^^^^^^^^^^^^
File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 135: invalid continuation byte
My impression here is that Closure has emitted the ISO-8859-1 encoding value of á to stderr, which has the hex value of 0xe1. However, the encoding='utf-8' argument in Python expects the stderr to be printed out as UTF-8.
Which encoding does Closure use for stdout/stderr printing? Is it ISO-8859-1 by intent? Or should it have been UTF-8 and Closure accidentally printed out ISO-8859-1?
I cannot tell from the example a.js file in the description whether the á character is correctly encoded as UTF-8 in the file you're actually using when you see this error.
Can you confirm that the input file, a.js is actually correct utf-8?
The exact output from closure-compiler itself. (i.e. the input that python is seeing)
The test case does not produce any JavaScript output from closure-compiler. Python attempts to capture the stderr error message from Closure process, but Python croaks internally since it cannot decode the stderr bytes that Closure is outputting, and so does not produce any output to the calling a.py file.
Is closure-compiler actually generating an invalid character sequence to stderr, or is something else going on?
One thing that could be happening is that the stderr output from closure-compiler could be getting mixed with output from either its own stdout or output from some other process that happens to share the same output stream. Due to buffering, the 2-character sequence for 'á'closure-compiler sends to stderr could be interrupted by output from somewhere else..
Thanks for providing the a.js file and your command line. We can use that to find out what the actual stderr output from the latest closure-compiler build is for this case.
If this problem is in some way actually tied to Windows, we're unlikely to fix it ourselves as none of the core team uses Windows when working on closure-compiler.
Now run the compiler with the options as described in earlier comments and save its stderr output into err.out and use xxd to check the contents of that file.
Yep. We again see "c3" and "a1" used as the 2-byte encoding in bytes at positions 0x87 and 0x88.
The Java jar executing in Linux is definitely generating stderr using UTF-8 encoding.
Probably the closure-compiler you're running has been converted from a jar file to a native Windows binary using Graal, because I think that's what the google/closure-compiler-npm code that generates the NPM release tries to make the default.
I'm not sure if the different behavior you see is the result of Windows behavior or in the behavior of Java on Windows (as emulated by Graal), or something else.
Although this change does not affect the bug, so this is not a Windows terminal/console issue, but something somewhere in the libraries in question either in Closure or somewhere else like observed.
We successfully worked around this in Emscripten code by specifying a directive encoding='iso-8859-1' if WINDOWS else 'utf-8' when invoking Closure.
Activity
Fix Closure invocation.
brad4d commentedon Mar 7, 2024
I cannot tell from the example
a.jsfile in the description whether theácharacter is correctly encoded as UTF-8 in the file you're actually using when you see this error.Can you confirm that the input file,
a.jsis actually correct utf-8?brad4d commentedon Mar 7, 2024
Actually, could you just attach 2 files to this issue?
a.jsfile.juj commentedon Mar 7, 2024
Here are the input files: a.zip
C3 A1is11000011 10100001, which is of form110xxxxx 10yyyyyy, i.e. a leading code point and a continution code point. See e.g. Wikipedia on UTF-8 Encoding. The Unicode code point in this case will bexxxxxyyyyyy=00011 100001=0xE1= https://www.compart.com/en/unicode/U+00E1.The test case does not produce any JavaScript output from closure-compiler. Python attempts to capture the stderr error message from Closure process, but Python croaks internally since it cannot decode the stderr bytes that Closure is outputting, and so does not produce any output to the calling a.py file.
Executing the following python file instead
does not throw an exception, and instead causes Python to print the stderr as expected:
Fix Closure invocation.
brad4d commentedon Mar 11, 2024
What I want to know is this:
Is closure-compiler actually generating an invalid character sequence to
stderr, or is something else going on?One thing that could be happening is that the
stderroutput from closure-compiler could be getting mixed with output from either its ownstdoutor output from some other process that happens to share the same output stream. Due to buffering, the 2-character sequence for'á'closure-compilersends tostderrcould be interrupted by output from somewhere else..Thanks for providing the
a.jsfile and your command line. We can use that to find out what the actualstderroutput from the latestclosure-compilerbuild is for this case.If this problem is in some way actually tied to Windows, we're unlikely to fix it ourselves as none of the core team uses Windows when working on closure-compiler.
brad4d commentedon Mar 12, 2024
Thank you for supplying the
a.jsfile.$ccjarFirst confirm that my terminal / OS is using UTF-8
Yep.
c3a1is the correct byte pair for this UTF-8 character as stated in a previous comment.Now confirm that the character is correct in
a.jsYep.
Now run the compiler with the options as described in earlier comments and save its
stderroutput intoerr.outand usexxdto check the contents of that file.Yep. We again see "c3" and "a1" used as the 2-byte encoding in bytes at positions 0x87 and 0x88.
The Java jar executing in Linux is definitely generating stderr using UTF-8 encoding.
Probably the closure-compiler you're running has been converted from a jar file to a native Windows binary using Graal, because I think that's what the google/closure-compiler-npm code that generates the NPM release tries to make the default.
I'm not sure if the different behavior you see is the result of Windows behavior or in the behavior of Java on Windows (as emulated by Graal), or something else.
Fix Closure invocation.
juj commentedon Mar 26, 2024
One simplification/note to the bug test case is that the original
a.pywasalthough this bug does not relate to
--charset=UTF8parameter, and the bug occurs also with shorter lineIt is expected that the issue does not occur on Linux or macOS, since those OSes default to UTF-8 widely.
In my Windows shell I have changed my active codepage to UTF-8, i.e.
See chcp 65001.
Although this change does not affect the bug, so this is not a Windows terminal/console issue, but something somewhere in the libraries in question either in Closure or somewhere else like observed.
We successfully worked around this in Emscripten code by specifying a directive
encoding='iso-8859-1' if WINDOWS else 'utf-8'when invoking Closure.