ruby/irb

IRB fails to colorize code other than utf-8

Opened this issue · 0 comments

Description

Create a ruby script with File.write("a.rb", "# coding: sjis\nbinding.irb\n__END__\nあ".encode("sjis"))
Run ruby a.rb
IRB fails to colorize code around binding.irb

ruby -e 'File.write("a.rb", "# coding: sjis\nbinding.irb\n__END__\nあ".encode("sjis"))'
ruby a.rb
/Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.9/lib/reline/unicode.rb:52:in `ord': invalid byte sequence in UTF-8 (ArgumentError)
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.9/lib/reline/unicode.rb:52:in `block in escape_for_print'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.9/lib/reline/unicode.rb:45:in `map!'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.9/lib/reline/unicode.rb:45:in `escape_for_print'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/color.rb:139:in `block in colorize_code'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/color.rb:192:in `block (2 levels) in scan'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/color.rb:204:in `block (2 levels) in scan'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/color.rb:202:in `each'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/color.rb:202:in `block in scan'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/ruby-lex.rb:57:in `rescue in compile_with_errors_suppressed'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/ruby-lex.rb:46:in `compile_with_errors_suppressed'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/color.rb:179:in `scan'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/color.rb:136:in `colorize_code'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb/workspace.rb:161:in `code_around_binding'
	from /Users/tomoya.ishida/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.2/lib/irb.rb:1584:in `irb'
	from <internal:prelude>:5:in `irb'
	from a.rb:1:in `<main>'

Combination

# encoding: utf-8 and file: sjis
File.write("a.rb", "binding.irb\n__END__\nあ".encode("sjis")) # error

# encoding: sjis and file: sjis
File.write("b.rb", "# coding: sjis\ns='あ'\nbinding.irb".encode("sjis")) # no error
File.write("c.rb", "# coding: sjis\nbinding.irb\n__END__\nあ".encode("sjis")) # error

# encoding: sjis and file: utf-8
File.write("d.rb", "# coding: sjis\nbinding.irb\n__END__\nあ") # no error