Debugging asset compilation errors

Using bundle show to debug undefined method 'directory?' for nil:NilClass while compiling rails assets

Thomas V.

2 minute read

While trying to deploy a new rails app to production I came across this undefined method ‘directory?’ error during asset compilation:

** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
rake aborted!
undefined method `directory?' for nil:NilClass
/srv/www/rails/arthouse/shared/bundle/ruby/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:189:in `block in each_entry'
/srv/www/rails/arthouse/shared/bundle/ruby/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:185:in `each'

This problem was only showing up in production, and there were a number of symlinks I thought may have been the culprit, unfortunately the error output was less than helpful.

To figure out where this problem was getting thrown I logged into the production shell and ran bundle show sprockets which returned the base gem path to Sprockets.

app@www:/srv/www/rails/arthouse/releases/20130213180106$ bundle show sprockets
/srv/www/rails/arthouse/shared/bundle/ruby/1.9.1/gems/sprockets-2.2.2

I opened up the file that was throwing the error , scrolled to line 189, caught the error and output the location of the path like this:

begin
  if stat(path).directory?
    each_entry(path) do |subpath|
      paths << subpath
    end
  end
rescue
  raise "Problem with path: #{path}"
end

This gave me the path to the invalid symlink in the assets directory and I was able to figure out exactly why it was failing. The symbolic link pointed to a git submodule we had added in development.

It turns out Capistrano has an easy setting for automatically taking care of git submodules, in config/deploy.rb add:

set :git_enable_submodules, 1
comments powered by Disqus