简体   繁体   English

未定义方法预编译资产错误

[英]Precompile Assets Error By Undefined Method

I've been doing some work on my app and I recently ran 我一直在做我的应用程序上的一些工作,最近我跑了

rake assets:precompile:primary RAILS_ENV=production --trace

The end result is this error: 最终结果是此错误:

NoMethodError: undefined method 'bytesize' for nil:NilClass

Not informative at all and the trace does little to help me 根本没有提供信息,并且跟踪对我没有多大帮助

** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
rake aborted!
NoMethodError: undefined method `bytesize' for nil:NilClass
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rack-1.4.7/lib/rack/utils.rb:300:in `bytesize'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/processed_asset.rb:13:in `initialize'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:249:in `new'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:249:in `block in build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:270:in `circular_call_protection'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:248:in `build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:93:in `block in build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/caching.rb:19:in `cache_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:92:in `build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:169:in `find_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:60:in `find_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/processed_asset.rb:111:in `block in resolve_dependencies'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/processed_asset.rb:105:in `each'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/processed_asset.rb:105:in `resolve_dependencies'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/processed_asset.rb:97:in `build_required_assets'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/processed_asset.rb:16:in `initialize'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:249:in `new'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:249:in `block in build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:270:in `circular_call_protection'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:248:in `build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:93:in `block in build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/caching.rb:19:in `cache_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:92:in `build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:169:in `find_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:60:in `find_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/bundled_asset.rb:16:in `initialize'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:252:in `new'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:252:in `build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:93:in `block in build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/caching.rb:19:in `cache_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:92:in `build_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:169:in `find_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/index.rb:60:in `find_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/environment.rb:78:in `find_asset'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/actionpack-3.2.22.1/lib/sprockets/static_compiler.rb:19:in `block in compile'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:219:in `block in each_logical_path'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:206:in `block (2 levels) in each_file'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:196:in `each'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:196:in `each_entry'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:204:in `block in each_file'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:203:in `each'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:203:in `each_file'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/sprockets-2.2.3/lib/sprockets/base.rb:217:in `each_logical_path'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/actionpack-3.2.22.1/lib/sprockets/static_compiler.rb:18:in `compile'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/actionpack-3.2.22.1/lib/sprockets/assets.rake:56:in `internal_precompile'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/actionpack-3.2.22.1/lib/sprockets/assets.rake:70:in `block (3 levels) in <top (required)>'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/task.rb:240:in `call'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/task.rb:240:in `block in execute'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/task.rb:235:in `each'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/task.rb:235:in `execute'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/jasontanner/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/task.rb:165:in `invoke'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:150:in `invoke_task'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:106:in `each'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:106:in `block in top_level'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:115:in `run_with_threads'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:100:in `top_level'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:78:in `block in run'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/lib/rake/application.rb:75:in `run'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/gems/rake-10.5.0/bin/rake:33:in `<top (required)>'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/bin/rake:19:in `load'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/bin/rake:19:in `<main>'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `eval'
/Users/jasontanner/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => assets:precompile:primary

This is my production environment file. 这是我的生产环境文件。

Galapal::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = true

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # For angular js compression
  config.assets.js_compressor = Uglifier.new(mangle: false)

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Raise exception on mass assignment protection for Active Record models
  config.active_record.mass_assignment_sanitizer = :strict

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5

  # Do not compress assets
  config.assets.compress = true

  config.assets..serve_static_assets = true  

  # Expands the lines which load the assets
  config.assets.debug = false

  # Expands the lines which load the assets
  config.assets.debug = false

  #ActiveRecord::Base.logger = Logger.new STDOUT


end

So my question is this. 所以我的问题是这个。 Why can't I precompile my assets and how do I fix this? 为什么我不能预编译资产,以及如何解决此问题? I'm running Rails 3.2.22 . 我正在运行Rails 3.2.22

I did a bit of digging around regarding this issue and it seems like it can happen for at least two reasons: 我对此问题做了一些深入研究,看来至少有两个原因可能会发生这种情况:

1) You have empty assets 1)您有空资产

2) there are non utf8 characters in one of your assets (check for comments in the code with characters with accents etc.). 2)您的资产之一中包含非utf8字符(请检查代码中带重音等字符的注释)。

See this for reference regarding 'require' specifically the person who solved the problem by "adding an empty class to the bottom of application.css" github.com/zweilove/css_splitter/issues/12 .Also perhaps try debugging by removing all assets from your manifests and then start adding the, one by one until it breaks. 请参阅此以获取有关“需要”的参考信息,特别是通过“在application.css的底部添加空类”解决问题的人github.com/zweilove/css_splitter/issues/12。还可以尝试通过从中删除所有资产来进行调试您的清单,然后开始逐个添加,直到损坏为止。 Good luck! 祝好运!

I had this issue do to a JavaScript file that had some sort of odd formatting (from the prettier library) that Rails took issue with . 我对一个JavaScript文件有这个问题,该文件具有某种奇怪的格式(来自漂亮的库),而Rails则对此提出了质疑。

NoMethodError: undefined method `bytesize' for nil:NilClass
/Users/joel/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rack-1.6.8/lib/rack/utils.rb:380:in `bytesize'
/Users/joel/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sprockets-2.12.4/lib/sprockets/processed_asset.rb:13:in `initialize'

To figure out which file, I dug into the gem path and added some code to sprockets/processed_asset.rb to print the file: 为了弄清楚哪个文件,我挖了gem路径,并在sprockets/processed_asset.rb添加了一些代码以打印文件:

  context = environment.context_class.new(environment, logical_path, pathname)
  @source = context.evaluate(pathname)
  p pathname
  @length = Rack::Utils.bytesize(source)

The next time I compiled assets, it showed me the culprit. 下次我整理资产时,它向我显示了罪魁祸首。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM