在UTF-8中rake无效的字节序列

dav*_*vid 4 ruby rake ruby-on-rails-3

出乎意料的是,我在运行rails 3.2和Ruby 1.9.3p125的网络服务器上的任何rake命令都会遇到一个奇怪的错误,无论什么样的rake任务,堆栈跟踪都是一样的.除了Rakefile和lib/tasks中的ascii之外什么都没有.

堆栈跟踪:

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>'
Run Code Online (Sandbox Code Playgroud)

有问题的方法是

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
Run Code Online (Sandbox Code Playgroud)

由于堆栈跟踪对我没有帮助,我"#{fn} #{File::FNM_CASEFOLD}"在块的开头插入了一个puts 并得到了:

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>'
Run Code Online (Sandbox Code Playgroud)

rakefile只是rails生成的默认文件

# 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
Run Code Online (Sandbox Code Playgroud)

lib/tasks中唯一的任务文件是

 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
Run Code Online (Sandbox Code Playgroud)

我不知道下一步该去哪里,非常感谢任何帮助.

GMA*_*GMA 9

好吧,我的问题与你的问题略有不同,但我会发布如何解决它,以防它对未来的Google员工有所帮助.

我的问题是每次我尝试运行时都会收到以下错误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
Run Code Online (Sandbox Code Playgroud)

所以我打开了code_statistics_calculator.rb(堆栈跟踪顶部的文件并更改了:

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
Run Code Online (Sandbox Code Playgroud)

至:

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
Run Code Online (Sandbox Code Playgroud)

rake stats再次运行,我进入调试器,此时我可以看到file_path此时指向的是一个app/models无法解析为utf-8 的特定文件.

果然,我在vim中打开了那个文件,当我输入:set fileencoding?它时它返回了latin-1.所以我把它设置为utf-8(set fileencoding=utf-8然后保存文件),果然,rake stats再次工作!瞧.

(请注意,在您的情况下,可能有多个文件不在utf-8中.此外,当您完成后,请确保您不要忘记更改code_statistics_calculator.rb回原始格式!)