pry-rails icon indicating copy to clipboard operation
pry-rails copied to clipboard

Encoding::UndefinedConversionError

Open lcowell opened this issue 9 years ago • 18 comments

When I paste "ñ" (with quotes) in to the console with pry-rails (0.3.3) enabled I will get an Encoding::UndefinedConversionError error.

$ bundle exec rails console
main:0>"��"
/Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/history.rb:106:in `write': "\xC3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/history.rb:106:in `puts'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/history.rb:106:in `save_to_file'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/history.rb:50:in `call'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/history.rb:50:in `push'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:267:in `handle_line'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/repl.rb:77:in `block in repl'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/repl.rb:67:in `loop'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/repl.rb:67:in `repl'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/repl.rb:38:in `block in start'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/input_lock.rb:61:in `call'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/input_lock.rb:61:in `__with_ownership'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/input_lock.rb:79:in `with_ownership'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/repl.rb:38:in `start'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/repl.rb:15:in `start'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/pry-0.10.1/lib/pry/pry_class.rb:169:in `start'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands/console.rb:47:in `start'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands/console.rb:8:in `start'
    from /Users/lukec/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'

If I disable pry-rails in my Gemfile I get the expected output.

$ bundle exec rails console
Loading development environment (Rails 3.2.21)
irb(main):001:0> "ñ"
=> "ñ"

I'd love to get to the bottom of this, so please let me know if there's any additional information I can provide.

lcowell avatar Mar 19 '15 20:03 lcowell

Got this, too. Looks like a bug to me.

momolog avatar Apr 21 '15 09:04 momolog

+1

› rails c
Loading development environment (Rails 4.1.10)
>> "ø"
=> "ø"
>> exit
/Users/Magne/.rvm/scripts/irbrc.rb:33:in `write': "\xC3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
    from /Users/Magne/.rvm/scripts/irbrc.rb:33:in `puts'
    from /Users/Magne/.rvm/scripts/irbrc.rb:33:in `block (2 levels) in <top (required)>'
    from /Users/Magne/.rvm/scripts/irbrc.rb:33:in `open'
    from /Users/Magne/.rvm/scripts/irbrc.rb:33:in `block in <top (required)>'
/Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb/ext/save-history.rb:115:in `write': "\xC3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb/ext/save-history.rb:115:in `puts'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb/ext/save-history.rb:115:in `block in save_history'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/open-uri.rb:36:in `open'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/open-uri.rb:36:in `open'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb/ext/save-history.rb:113:in `save_history'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb/ext/save-history.rb:79:in `block in extended'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb.rb:407:in `call'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb.rb:407:in `block in irb_at_exit'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb.rb:407:in `each'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb.rb:407:in `irb_at_exit'
    from /Users/Magne/.rvm/rubies/ruby-2.0.0-head/lib/ruby/2.0.0/irb.rb:400:in `start'
    from /Users/Magne/.rvm/gems/ruby-2.0.0-head@bloggery/gems/railties-4.1.10/lib/rails/commands/console.rb:90:in `start'
    from /Users/Magne/.rvm/gems/ruby-2.0.0-head@bloggery/gems/railties-4.1.10/lib/rails/commands/console.rb:9:in `start'
    from /Users/Magne/.rvm/gems/ruby-2.0.0-head@bloggery/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/Magne/.rvm/gems/ruby-2.0.0-head@bloggery/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/Magne/.rvm/gems/ruby-2.0.0-head@bloggery/gems/railties-4.1.10/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

redbar0n avatar Jun 11 '15 14:06 redbar0n

Some other guy also documented the problem: https://gist.github.com/atton-/5facfe29cf251ef9323f

redbar0n avatar Jun 11 '15 14:06 redbar0n

:+1:

pgolm avatar Sep 23 '15 13:09 pgolm

https://github.com/pry/pry/issues/1429#issuecomment-148339406

definitely an issue with pry-rails

rubyconvict avatar Oct 15 '15 11:10 rubyconvict

Could someone try running the Rails console without pry-rails, opening Pry by typing binding.pry into that Rails console session, and then reproing?

rf- avatar Oct 15 '15 19:10 rf-

Sorry, pry-rails is innocent, it's pry vs. rb-readline war.

1.9.3-p429 :002 > 'Ą'
 => "Ą" 
1.9.3-p429 :003 > binding.pry
[1] pry(main)> '��'
Encoding::UndefinedConversionError: "\xC4" from ASCII-8BIT to UTF-8
    from /.../vendor/bundle/ruby/1.9.1/gems/pry-0.10.2/lib/pry/history.rb:106:in `write'
    from /.../vendor/bundle/ruby/1.9.1/gems/pry-0.10.2/lib/pry/history.rb:106:in `puts'
    from /.../vendor/bundle/ruby/1.9.1/gems/pry-0.10.2/lib/pry/history.rb:106:in `save_to_file'
    from /.../vendor/bundle/ruby/1.9.1/gems/pry-0.10.2/lib/pry/history.rb:50:in `call'
    from /.../vendor/bundle/ruby/1.9.1/gems/pry-0.10.2/lib/pry/history.rb:50:in `push'
    from /.../vendor/bundle/ruby/1.9.1/gems/pry-0.10.2/lib/pry/pry_instance.rb:267:in `handle_line'

rubyconvict avatar Oct 19 '15 09:10 rubyconvict

+1

yoonsy avatar Nov 14 '15 03:11 yoonsy

+1

mgrachev avatar Nov 28 '15 13:11 mgrachev

+1

milesbissky avatar Jan 05 '16 01:01 milesbissky

Just remove the 'rb-readline' gem if you are on a linux like system. This may be the current solution for this problem.

lingceng avatar Jan 15 '16 07:01 lingceng

Removing rb-readline solved the issue for me.

lcowell avatar Jan 15 '16 18:01 lcowell

However, if you remove rb-readline, we won't be able to type non-ascii character on the console

poc7667 avatar May 05 '16 04:05 poc7667

I've solved this. refer to pry/pry#230420454

jsnick avatar Jul 05 '16 08:07 jsnick

http://altarf.net/computer/rails/3222

add ~/.pryrc with content

Pry.config.history.should_save = false

and it works for me.

chunlea avatar Feb 13 '17 06:02 chunlea

+1

rutgerg avatar Aug 24 '17 12:08 rutgerg

Had same error on Linux in Docker image. Problem was in system locale settings (it was set to POSIX).

Fixed by setting locale to Unicode-enabled one:

FROM ruby:2.6.2
RUN apt-get update && apt-get install -y locales && update-locale LANG=C.UTF-8 LC_ALL=C.UTF-8
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

In your local system check output of locale command and add export LANG=C.UTF-8 LC_ALL=C.UTF-8 to your ~/.bashrc or ~/.zshrc if necessary

Envek avatar Mar 21 '19 11:03 Envek

I was able to "fix" this by adding this line in:

/Users/<username>/.rvm/gems/ruby-2.7.5/gems/pry-0.13.1/lib/pry/history.rb

def save_to_file(line)
  history_file.puts line.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?') if history_file
end

Of course the issue is still there, so I opened an issue in RbReadline (see https://github.com/ConnorAtherton/rb-readline/issues/163)

Overload119 avatar Nov 04 '22 23:11 Overload119