8
votes

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?

3

3 Answers

5
votes

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.

5
votes

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.

0
votes

in my case, the project requiring the gem had a rake file, and the gem I was requiring had the same name when I changed the name of the rake file I could see the tasks in the project.

my_gem had lib/tasks/XXXX.rake and my_proj also had lib/tasks/XXXX.rake, after I changed my_gem XXXX.rake to YYYY.rake I managed to list and use the tasks in YYYY.rake