Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 973
pjmorse Avatar asked Mar 07 '16 18:03

pjmorse


2 Answers

I just successfully tested your gem and I can see no problem with the rake tasks in it:

  • I added gem_fresh to the Gemfile and ran bundle, the gem installed
  • Immediately I could see the rake present in the list of rakes:

    $ rake -T outdated
    rake gem_fresh:outdated  # outdated
    
  • Then I updated the railtie.rb file to use the load method to load the rake defined in lib/task/metrics.rake and searched the available rakes again:

    # lib/gem_fresh/railtie.rb:
    rake_tasks do
      namespace :gem_fresh do
        desc "outdated"
        task :outdated => :environment do
          GemFresh::Reporter.new.report
        end
      end
    
      load "tasks/metrics.rake"
    end 
    
    $ rake -T outdated
    rake gem_fresh:outdated     # outdated
    rake metrics:outdated_gems  # display outdated gem version metrics
    

So, I can see no problem with your gem. Both methods in railtie (inline rake as well as using the load method) seem to work OK. The only difference I noticed is that I tested this on rails 4.2 but I rather doubt that would make a difference.

Did you put the gem into your Gemfile? If I remove it from there, I indeed see no gem_fresh rakes defined.

like image 85
Matouš Borák Avatar answered Oct 22 '22 07:10

Matouš Borák


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.

like image 22
pjmorse Avatar answered Oct 22 '22 08:10

pjmorse