Repository navigation
a sad tale of unicode account names, shells and code pages - #2624
Merged
Merged
Conversation
shiftkey
force-pushed
the
its-just-a-series-of-bytes-they-said
branch
from
September 6, 2017 08:00
7cf6da8 to
33c2f21
Compare
Contributor
joshaber
suggested changes
Sep 7, 2017
| } | ||
|
|
||
| const activeCodePage = await getActiveCodePage() | ||
| if (activeCodePage) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| }) | ||
| } | ||
|
|
||
| if (process.platform !== 'win32') { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| const result = chcpOutputRegex.exec(output) | ||
| if (result) { | ||
| const value = result[1] | ||
| resolve(parseInt(value, 10)) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Member
Author
|
🤾♂️ |
joshaber
approved these changes
Sep 8, 2017
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Notice anything wrong with this picture?
I'll give you a hint:
I'm not quite familiar with how Git Bash is differentiating between these two, but it's unimportant to the problem at hand, which is #2585. It took a while to get to the bottom of why the Atom installation wasn't being detected for @DRSAgile, but it's related to having non-ASCII characters in their account name.
Here's my test account running this example script to read the registry:
It's those � characters that are the root cause of the issue.
If you're particularly sharp you might have noticed that directory path in the prompt is rendered correctly. Your eyes aren't deceiving you.
But it's 2017! Doesn't Windows support Unicode?
Yes, Windows supports Unicode character sets and code pages. But specific parts of the Windows operating system, for compatibility reasons, don't have this enabled by default - like the Windows Console. It turns out we already encountered this problem in a different context when we were retrieving the
PATHenvironment variable for the current user using a PowerShell script (shout out to the Atom team for finding this earlier):Why do we need to do these shenanigans?
Compatibility, mostly. If you're running the Command Prompt in Windows 10 chances are you have the same code page set as the IBM PC supported. That's right, Code Page 437, baby!
Before going any further, I should give some back story. Code pages are conceptually similar to character sets, but because of History™ there's lots of platform-specific code pages still floating around. The documented list of these is pretty entertaining read, if only to understand how messy this is.
Most code pages are a superset of ASCII, so any time you see that � or garbled text you're likely dealing with that problem. This ignores discussions around fonts, but I'm not going to deal with that here.
Anyway, Windows has an active code page set which affects how characters are rendered in a terminal. This also affects the behaviour of Win32 APIs - for example,
ShellExecuteAis the ANSI API forShellExecuteand it's behaviour is affected by the active code page.ShellExecuteWis the Unicode version and is unaffected by the active code page.Oh, and the active code page is set globally, so you need to be really, really careful 😱
Did you know that everything is terrible?
What can we do here?
The excellent people over at
node-winregpointed out that you probably need to care about the active code page for any UTF-8 data it reads, and suggested this workaround:That's the approach I've gone with here, and I'll dig more into this now that I know where I'm heading.
But doesn't Node support Unicode? Why not just use that?
By the time Node gets the chance to read these bytes into the stream, they're already invalid. We need to tell the shell which code page to use before spawning
reg.exeso we get the right characters back.getActiveCodePagemethod to retrieve the current code page