简体   繁体   中英

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 .

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

2) there are non utf8 characters in one of your assets (check for comments in the code with characters with accents etc.).

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

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:

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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