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

Ruby UTF-8中的rake无效字节序列,ruby,ruby-on-rails-3,rake,Ruby,Ruby On Rails 3,Rake,出乎意料的是,在运行rails 3.2和Ruby 1.9.3p125的Web服务器上,任何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/

出乎意料的是,在运行rails 3.2和Ruby 1.9.3p125的Web服务器上,任何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>'
由于堆栈跟踪对我没有帮助,我在块的开头插入了一个puts
“#{fn}{File::FNM_CASEFOLD}”
,得到如下结果:

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>'
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

我不知道下一步该去哪里,非常感谢您的帮助。

尝试用BOM将有问题的文件(可能是rake正在尝试的任何文件)保存在UTF-8中。

好的,我的问题与您的略有不同,但我将发布我如何解决它,以防它对未来的谷歌用户有所帮助

我的问题是,每次尝试运行
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
因此,我打开了
code\u statistics\u calculator.rb
(stacktrace顶部的文件,并更改了:

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
再次运行
rake stats
,我进入了debugger,这时我可以看到
file\u path
此时指向
app/models
中的一个特定文件,该文件无法解析为utf-8

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


(请注意,在您的情况下,可能有多个文件不是utf-8格式。此外,完成后,请确保不要忘记将
code\u statistics\u calculator.rb
更改回其原始格式!)

基于Georgeillo的想法,但无需调试,您可以执行以下操作:

  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

错误将被忽略,并将打印带有违规文件的跟踪。

应用程序的路径是否包含奇怪的内容?不,只需/home/test/my_appCheck查看rails项目中是否有名称中带有奇怪编码的文件,或者检查rails项目路径是否与UTF-8编码兼容。Bump!@GeorgeMillo-fantastic vim提示,用于什么可以是一个总皮塔
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
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
  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