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.
…saving the C++ code from receiving a Node-style encoding name that it doesn’t recognize.
Our
TextBuffer
object acts as a bridge betweenTextEditor
and lower-level APIs likepathwatcher
andsuperstring
. Andsuperstring
, in turn, presents a JavaScript API but implements lots of its internals in C++ for speed.But that’s where a bug can slip in, because JavaScript and C++ use different conventional names for common text encodings… including UTF-8, the commonest of all (in our particular domain). Node likes to call it
utf8
, but thesuperstring
C++ code insists upon the more formalUTF-8
. There’s a utility function callednormalizeEncoding
within thesuperstring
JavaScript that converts Node-style encoding names to C++-style encoding names.I’m not going to be able to demonstrate exactly how this bug happens, but take a look at
superstring/index.js
and search for the stringencoding
. You’ll see that, aside from the aforementionednormalizeEncoding
utility, three different methods take anencoding
parameter in some form:TextBuffer#load
,TextBuffer#save
, andTextBuffer#baseTextMatchesFile
.You might also notice that the first two of those methods pass their
encoding
parameter through thenormalizeEncoding
function almost immediately — butbaseTextMatchesFile
doesn’t. After lots of analysis and reflection, I can’t come up with a single reason why it shouldn’t normalize the encoding; it would seem to save me from this exception I kept getting today while working on a new package:I got to this method from
text-buffer
code — specificallysubscribeToFile
, which my new package apparently called in the course of adding anonDidSave
callback to aTextEditor
.It feels like this code path should be hit quite often, and I’m sure I’ve gotten this exact error before, but not for ages; so I definitely don’t have reproduction steps here. But I can assert that hot-fixing this in a debugger (via a conditional breakpoint that simply does
encoding = normalizeEncoding(encoding)
on the appropriate line) prevents this error, and it makes logical sense why.