出乎意料的是,我在运行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>'
违规方法是
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
由于堆栈跟踪对我没用,我在块的开头插入了一个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>'
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
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
我不知道下一步该去哪里,非常感谢任何帮助。
答案 0 :(得分: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
所以我打开了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
中无法解析的:set fileencoding?
中的特定文件as utf-8。
果然,我在vim中打开了该文件,当我输入latin-1
时,它返回set fileencoding=utf-8
。所以我将它设置为utf-8(rake stats
然后保存文件),果然,code_statistics_calculator.rb
再次工作!瞧。
(请注意,在您的情况下,可能有多个文件不在utf-8中。此外,当您完成后请确保您不要忘记更改{{1回到原来的形式!)
答案 1 :(得分:1)
尝试在带有BOM的UTF-8中保存违规文件(可能是rake正在尝试的任何内容)。
答案 2 :(得分: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
错误将被忽略,并且将打印带有违规文件的跟踪。