zweilove/css_splitter

undefined method `bytesize' for nil:NilClass

Opened this issue · 8 comments

Rack::Utils.bytesize blows up if the master stylesheet is empty.

NoMethodError - undefined method `bytesize' for nil:NilClass:
  rack (1.6.0) lib/rack/utils.rb:361:in `bytesize'
  sprockets (2.12.3) lib/sprockets/bundled_asset.rb:29:in `initialize'
  sprockets (2.12.3) lib/sprockets/base.rb:377:in `build_asset'
  sprockets (2.12.3) lib/sprockets/index.rb:94:in `block in build_asset'
  sprockets (2.12.3) lib/sprockets/caching.rb:58:in `cache_asset'
  sprockets (2.12.3) lib/sprockets/index.rb:93:in `build_asset'
  sprockets (2.12.3) lib/sprockets/base.rb:287:in `find_asset'
  sprockets (2.12.3) lib/sprockets/index.rb:61:in `find_asset'
  sprockets (2.12.3) lib/sprockets/environment.rb:75:in `find_asset'
  sprockets (2.12.3) lib/sprockets/base.rb:295:in `[]'
  sprockets-rails (2.2.4) lib/sprockets/rails/helper.rb:230:in `lookup_asset_for_path'
  sprockets-rails (2.2.4) lib/sprockets/rails/helper.rb:190:in `check_errors_for'
  sprockets-rails (2.2.4) lib/sprockets/rails/helper.rb:89:in `asset_path'
  actionview (4.2.0) lib/action_view/helpers/asset_url_helper.rb:267:in `stylesheet_path'
  actionview (4.2.0) lib/action_view/helpers/asset_tag_helper.rb:100:in `block in stylesheet_link_tag'
  actionview (4.2.0) lib/action_view/helpers/asset_tag_helper.rb:96:in `stylesheet_link_tag'
  sprockets-rails (2.2.4) lib/sprockets/rails/helper.rb:170:in `stylesheet_link_tag'
  css_splitter (0.4.1) app/helpers/css_splitter/application_helper.rb:18:in `block in split_stylesheet_link_tag'
  css_splitter (0.4.1) app/helpers/css_splitter/application_helper.rb:7:in `split_stylesheet_link_tag

We should try to create a failing test for this issue

While css_splitter does appear in the backtrace, the error appears to have resulted from its split_stylesheet_link_tag method calling the Sprockets-defined stylesheet_link_tag method on the empty stylesheet.

Does the error also occur if you directly call Sprockets' stylesheet_link_tag with your empty stylesheet? If so, then it's probably more likely that the bug is with Sprockets than with css_splitter.

I'm getting the same error (NoMethodError: undefined method 'bytesize' for nil:NilClass).

@Umofomia, I checked and called stylesheet_link_tag on an empty stylesheet, and it did not return any errors, so presumably the issue is with css_splitter.

@liorbrauer can you provide any additional information?

@jhilden Hi, I continued fiddling with it and got it working. The issue for me was that I needed to provide the path to the too_big_stylesheet when writing the *= require 'too_big_stylesheet' (it is placed inside a folder under stylesheets). I wrongly assumed that since the too_big_stylesheet_split2.css was on the same folder as the too_big_stylesheet there was no need for the path.

I'm glad you were able to fix it @liorbrauer

I tried to reproce the issue you were having in this branch (https://github.com/zweilove/css_splitter/compare/reproduce-issue-50) to see if we could give a better error message in this case.
But when I ran RAILS_ENV=production bundle exec rake assets:precompile inside test/dummy I got this error message instead of what you described:

Sprockets::FileNotFound: couldn't find file 'too_big_stylesheet.css' with type 'text/css'
/css_splitter/test/dummy/app/assets/stylesheets/folder/too_big_stylesheet_split2.css:2

Any tips for me what could be different? What are your rails/sprockets versions?

@jhilden Thanks, this gem has really helped us!

Regarding your question: I looked at the branch you linked. That's not what I did - I added the folder/ bit inside the too_big_stylesheet_split2.css file, like so:

/*
 *= require 'folder/too_big_stylesheet'
 */

Hope that it is more clear?

Also: Using rails 3.2.22 and sprockets 2.2.3