Repository navigation
Conversation
This branch has not been deployed
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.
Fixes #976
Summary
IRB::Color.colorize_codeandIRB::WorkSpace#code_around_bindingcould raiseArgumentError(invalid byte sequence in UTF-8) orEncoding::CompatibilityErrorwhen colorizing files or strings containing non-UTF-8 encodings, magic encoding comments (e.g.# coding: sjis), or invalid byte sequences (such as non-UTF-8 data after__END__):Prism.parse_lex(code, scopes: ...)previously omittedencoding: code.encoding(defaulting to UTF-8 for non-UTF-8 strings without magic comments) and did not updatecodeor location slices to the parsed source encoding when a magic comment changed it (including underPrism::Serialize/ FFI backends such as TruffleRuby). As a result, tokens parsed asWindows-31Jwere concatenated withcode.linesstill tagged asUTF-8(e.g. lines following__END__), or raisedEncoding::CompatibilityError/ArgumentErrorduring token matching and slicing.Reline::Unicode.escape_for_printexecutes regexes against its argument, raisingArgumentError: invalid byte sequencewhen unparsed data (such as arbitrary bytes after__END__or syntax error fragments) is not valid in the active encoding. Similarly,error_tokens.reject!andColorizeVisitor#visit_call_nodecould run regexes against slices with invalid encodings.Changes
encoding: orig_encodingtoPrism.parse_lex, resolve the parsed token/source encoding across both C-extension andPrism::Serializebackends, aligncode, location slices, and the output buffer with that encoding, and re-encode the colorized result back to the caller'sorig_encodingwith replacement fallback.IRB::Color.escape_for_printbefore passing text toReline::Unicode.escape_for_print, and guard regex checks on potentially invalid token/message slices.colorized,fmt, andfileencodings are compatible inIRB::WorkSpace#code_around_bindingbefore formatting lines (e.g. whenSCRIPT_LINES__or source files use non-UTF-8 encodings alongside a UTF-8 file path).Testing
test/irb/test_color.rbandtest/irb/test_workspace.rbcovering all reproduction cases from IRB fails to colorize code other than utf-8 #976, non-UTF-8 inputs without magic comments,ASCII-8BIT/ invalid UTF-8 fragments, non-ASCII-compatible encodings, andSCRIPT_LINES__.bundle exec ruby -Ilib -Itest test/irb/test_color.rb(8 tests, 906 assertions, 0 failures, 0 errors)bundle exec ruby -Ilib -Itest test/irb/test_workspace.rb(6 tests, 12 assertions, 0 failures, 0 errors)bundle exec rubocop lib/irb/color.rb lib/irb/workspace.rb test/irb/test_color.rb test/irb/test_workspace.rb(0 offenses)AI was used to write this code.