Search code examples
ruby-on-railsrubygemsrakebundler

Why is this gem not adding rake tasks to a Rails app?


We have a gem which runs via a Rake task. The task is defined in lib/tasks/<namespace>.rake. After reading Rake tasks inside gem not being found I confirmed that the .gemspec includes the file defining the task; there is also a railtie which should be including the tasks as suggested in including rake tasks in gems. And yet our Rails 4.1 application doesn't seem to load the Rake task.

What am I missing?


Solution

  • The problem was not with the gem, but with the way it was included in the app.

    In the Gemfile, this works and includes the rake task:

    gem 'gem_fresh'
    

    This works but doesn't include the rake task:

    group :development do
      gem 'gem_fresh'
    end
    

    This seems to be due to how Bundler requires gems in Rails apps. From the documentation:

    By default, a Rails generated app calls Bundler.require(:default, Rails.env) in your application.rb, which links the groups in your Gemfile to the Rails environment.

    If for some reason that Rails.env argument wasn't evaluating to include the :development group, which seems to be the case when I call rake -T, the gem wouldn't be Bundler.require-d, and the rake tasks wouldn't be loaded.

    The fact that it isn't including the :development group seems to be an odd Bundler "gotcha", but at least now I know that moving it to the default group solves the issue and it's not a problem with the gem.