22

After upgrading to ruby 1.9.3 one of my apps is working fine but the second one I am trying to convert fails at the "assets:precompile" stage when I try to deploy with capistrano. Here is the stacktrace:

    rake aborted!
    rake aborted!
    invalid byte sequence in US-ASCII
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/trace_output.rb:16:in `block in trace_on'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/trace_output.rb:14:in `map'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/trace_output.rb:14:in `trace_on'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/application.rb:328:in `trace'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/application.rb:183:in `display_error_message'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/application.rb:169:in `rescue in standard_exception_handling'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/application.rb:159:in `standard_exception_handling'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/application.rb:88:in `load_rakefile'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/application.rb:72:in `block in run'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@rails3211/bin/ruby_noexec_wrapper:14:in `eval'
    /Users/george/.rvm/gems/ruby-1.9.3-p392@rails3211/bin/ruby_noexec_wrapper:14:in `<main>'    

I have read numerous posts and tried several suggestions but to no avail. I tried adding the following to the top of my gemfile:

if RUBY_VERSION =~ /1.9/
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
end

But it made no difference.

I checked LANG and LC_ALL environment variables as follows

$ echo $LC_ALL
en_NZ.UTF-8

$ echo $LANG
en_NZ.UTF-8

I'm afraid I dont really understand the message at all and I dont know how to identify the file that has the problem.

I cant get any rake task to run - it gives the same error.

Note that I can run the application perfectly fine in development mode.

giorgio
  • 2,115
  • 5
  • 23
  • 39
  • After searching randomly through files in the project. I decided that because the error message did not specify a filename for the problem then it was probably something to do with rake itself. Removing a rake task that contained hard coded data for database priming fixed the problem. – giorgio Apr 11 '13 at 22:06

7 Answers7

30

Add

#encoding: utf-8 

to the first line of your Rakefile (or whatever file has the strange characters in)

George Armhold
  • 30,824
  • 50
  • 153
  • 232
fotanus
  • 19,618
  • 13
  • 77
  • 111
  • 1
    But how do I find which file that is? – giorgio Apr 11 '13 at 12:09
  • 2
    No the problem file was not the Rakefile. That would have been easy!. It was one of my rake tasks that had the problem. – giorgio Apr 23 '13 at 02:24
  • 2
    you can use the `file` command to identify the encoding: `Rakefile: UTF-8 Unicode English text vs Rakefile: ASCII text` – timofei7 Nov 30 '13 at 21:24
  • @timofei7 yes, but since ASCII text is actually a subset of UTF-8, setting UTF-8 should always work - unless, of course, the file has an odd encoding. – fotanus Sep 15 '14 at 20:14
  • I nice command to show all files with their current encoding would be `find . -exec file {} \;` – Cyclonecode Oct 06 '15 at 18:02
5

Track down the rake file(s) at fault by deleting one at a time.

ie the files under lib/tasks/delete_me.rake

Then re-rake or restart which ever was giving you the issue. Once the issue is gone do a git diff to see which file was the culprit and with your favorite editor change the encoding of the file.

ie.,

vim lib/tasks/delete_me.rake :set fileencoding=utf-8 :wq

Then re-rake and you should be back in service.

f1v
  • 153
  • 1
  • 5
2

First run

$ sudo gem install magic_encoding

Then go into the folder and run

$ magic_encoding

Ready!

SztupY
  • 10,291
  • 8
  • 64
  • 87
Daniel
  • 31
  • 1
1

Make sure you're not typing in your file in romaji (Japanese). Or the English character setting for some other non US-ASCII language.

I tried running a rake db:seed and was getting a similar error. Turns out I had been typing in my seed file in the Japanese roman characters. I forgot to change my keyboard input back to US before working on my project.

ian
  • 12,003
  • 9
  • 51
  • 107
user2031423
  • 347
  • 4
  • 7
  • 1
    @BrenoSalgado he most likely add the correct encoding in the top of his file, or removed the characters from his seed file. – fotanus Sep 20 '13 at 17:57
  • @BrenoSalgado Yea I simply deleted the characters that I had typed in Romaji, switch back to US English and retyped it. – user2031423 Dec 05 '13 at 01:16
1

I had the similar problem and the "error" was by my first name: It contains a non US-ASCII char (ö) This was printed in the setup.rb and caused the problems. I changed it to "oe" and it worked fine.

I will send an E-Mail to bitnami to let it changed somehow

MrNuss
  • 11
  • 1
0

I was getting the similar error while trying to run any rake task (I use Rails 3.2 with Ruby 1.9.3). I tried all the solutions above without any success. Later I found out that I was a specific gem that was causing the error (in my case it was the Faker gem, that was updated some days ago). I deleted the gem (it was not in use) and everything began to work! So, my advice is, if you run into this kind of issue and none of the solutions listed here work, check the version of each gem and see if any of them was been updated. Try to delete them or to set a compatible version.

ana3ela
  • 51
  • 1
  • 8
0

This error popped out on a new project I was setting its dev env for work.

It was a rake task throwing the error, but on checking its file encoding, it was UTF-8.

So, this task in question was pulling data from another file (which also had UTF-8 encoding). The error persisted.

so, instead of reading the file as is (i.e. File.read("myfile.rb"))

I changed it to:

File.read("myfile.rb", :encoding => 'utf-8'), as per instructions of this solution, and all good again.

j4v1
  • 1,457
  • 1
  • 22
  • 32