Invalid byte sequence in UTF-8

Suddenly I get a weird error for any rake command on my web server working with rails 3.2 and Ruby 1.9.3p125, and the stack trace is the same no matter what the rake task is. There is nothing besides what exists in ascii in Rakefile and lib / tasks.

Stack trace:

rake --trace rake aborted! invalid byte sequence in UTF-8 /usr/local/lib/ruby/1.9.1/rake/application.rb:183:in `glob' /usr/local/lib/ruby/1.9.1/rake/application.rb:183:in `block in have_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:181:in `each' /usr/local/lib/ruby/1.9.1/rake/application.rb:181:in `have_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:468:in `find_rakefile_location' /usr/local/lib/ruby/1.9.1/rake/application.rb:486:in `raw_load_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:82:in `block in load_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling' /usr/local/lib/ruby/1.9.1/rake/application.rb:81:in `load_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:65:in `block in run' /usr/local/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling' /usr/local/lib/ruby/1.9.1/rake/application.rb:63:in `run' /usr/local/bin/rake:32:in `<main>' 

Insult method

 def have_rakefile @rakefiles.each do |fn| if File.exist?(fn) others = Dir.glob(fn, File::FNM_CASEFOLD) return others.size == 1 ? others.first : fn elsif fn == '' return fn end end return nil end 

Since the stack trace was useless to me, I inserted puts "#{fn} #{File::FNM_CASEFOLD}" at the beginning of the block and got this:

 rakefile 8 Rakefile 8 rake aborted! invalid byte sequence in UTF-8 /usr/local/lib/ruby/1.9.1/rake/application.rb:184:in `glob' /usr/local/lib/ruby/1.9.1/rake/application.rb:184:in `block in have_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:181:in `each' /usr/local/lib/ruby/1.9.1/rake/application.rb:181:in `have_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:469:in `find_rakefile_location' /usr/local/lib/ruby/1.9.1/rake/application.rb:487:in `raw_load_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:82:in `block in load_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling' /usr/local/lib/ruby/1.9.1/rake/application.rb:81:in `load_rakefile' /usr/local/lib/ruby/1.9.1/rake/application.rb:65:in `block in run' /usr/local/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling' /usr/local/lib/ruby/1.9.1/rake/application.rb:63:in `run' /usr/local/bin/rake:32:in `<main>' 

Rake file only by default rails generate

 # Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. require File.expand_path('../config/application', __FILE__) require 'rake/dsl_definition' require 'rake' MyApp::Application.load_tasks 

The only task file in lib / tasks is

  desc "Resets the help files in the db by deleting all existing and rereading the yaml files" task :help_reset => :environment do HelpSystem.delete_all HelpSystem.seed_help end 

I have no idea where to go next, any help is appreciated.

+4
source share
3 answers

Try saving the damaging file (maybe everything that rake does) to UTF-8 WITH BOM.

+1
source

Well, my problem was a little different from yours, but I will post how I solved it if it helps the future Googler.

My problem was that I was getting the following error every time I tried to run rake stats :

 rake aborted! ArgumentError: invalid byte sequence in UTF-8 /Users/george/.rvm/gems/ruby-2.1.5/gems/railties-4.1.6/lib/rails/code_statistics_calculator.rb:61:in `=~' /Users/george/.rvm/gems/ruby-2.1.5/gems/railties-4.1.6/lib/rails/code_statistics_calculator.rb:61:in `add_by_io' /Users/george/.rvm/gems/ruby-2.1.5/gems/railties-4.1.6/lib/rails/code_statistics_calculator.rb:43:in `block in add_by_file_path' ... # more stacktrace 

So, I opened code_statistics_calculator.rb (file at the top of stacktrace and changed:

 def add_by_file_path(file_path) File.open(file_path) do |f| self.add_by_io(f, file_type(file_path)) # <- this line is raising the error end end 

in

 def add_by_file_path(file_path) File.open(file_path) do |f| begin self.add_by_io(f, file_type(file_path)) rescue ArgumentError debugger puts # An extra statement is needed between 'debugger' and 'end' or debugger screws up. end end end 

Running rake stats again, I entered the debugger, and at that moment I could see that file_path was currently pointing to a specific file in app/models , which it could not parse as utf-8.

Of course, I opened this file in vim, and when I typed :set fileencoding? he returned latin-1 . So I set it to utf-8 ( set fileencoding=utf-8 , then saved the file) and of course rake stats works again! Voila.

(Please note that in your case there may be more than one file that is not in utf-8. Also, when you are done, make sure you remember to change code_statistics_calculator.rb back to your original form!)

+7
source

Based on the idea of ​​GeorgeMillo, but without the need for debugging, you can do:

  def add_by_file_path(file_path) File.open(file_path) do |f| self.add_by_io(f, file_type(file_path)) end rescue Exception => e puts "Exception raised while processing: #{file_path}: #{e.message}" end 

Errors will be ignored and a trace will be printed with the corrupted file.

0
source

All Articles