2012-07-11 105 views
4

在我的webserver運行rails 3.2和ruby 1.9.3p125的任何rake命令中,我都得到了一個奇怪的錯誤,無論使用哪種rake任務,堆棧跟蹤都是一樣的。除了Rakefile和lib/tasks中的ascii,什麼都沒有。UTF-8中rake無效的字節序列

堆棧跟蹤:

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

有問題的方法是

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 

由於堆棧跟蹤是沒有好處的給我,我在塊的開頭插入一個看跌期權"#{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>' 

rakefile只是默認的一個軌道生成

# 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 

中的lib /任務的唯一任務文件

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 

我不知道接下來要去哪裏,任何幫助是極大的讚賞。

+0

您的應用程序的路徑是否包含奇怪的東西? – 2012-07-12 12:51:33

+0

不,它只是/ home/test/my_app – david 2012-07-12 19:04:17

+0

任何人都解決了這一個呢? – 2012-09-30 10:03:26

回答

1

嘗試在UTF-8 WITH BOM中保存違規文件(可能是rake嘗試的任何內容)。

7

好的,我的問題與您的問題稍有不同,但我會發布如何解決它以防萬一它有助於未來的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 

所以,我打開了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 

到:

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 

運行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恢復到原來的形狀!)

+0

凹凸! @GeorgeMillo - 夢幻般的vim提示,爲什麼可以是皮塔餅!有義務的.. :) – sensadrome 2016-07-19 02:05:39

0

基於GeorgeMillo的想法,但無需調試,可以這樣做:

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 

錯誤將被忽略,並與有問題的文件痕跡將被打印出來。