Why is this gem not adding rake tasks to a Rails app? - ruby-on-rails

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?

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.

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.

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

Related

rails rake task with globally installed gem

I'm trying to write a rake file to import data from csv, and I want to use smarter_csv gem. I have the gem installed globally (I don't want to add it to my Gemfile because it's a one-off task).
In my rake file I require 'smarter_csv' but when I run the task I get the following error:
rake aborted!
LoadError: cannot load such file -- smarter_csv
Every rake example I can find tells you to just require 'foo'. I can run the code manually in irb after requiring smarter_csv.
What am I missing?
(If it matters, I'm using rbenv on macOS Catalina)
If you do want to load a gem without having it in your Gemfile you can do the following in your rake task:
$: << '/Users/<user>/.rvm/rubies/ruby-
x.x.x/lib/ruby/gems/x.x.x/gems/smarter_csv-x.x.x/lib'
require 'smarter_csv'
I think the option to have the gem in your Gemfile with the require: false option is more suitable however. The gem won't be loaded until you specifically require it.
Rails uses bundler, and loads the gems mentioned in gemfile into memory. Doesn't look like you've any other option.
I had a similar use case, I tried putting it into a development group like so,
group :development do
gem 'smarter_csv'
end

Bundler is deprecating bundle console in favor of bin/console. Can anyone provide more clarity as to how bin/console should work?

I have a custom ruby gem that relies heavily on bundle console. Nothing special or fancy, just an interactive console with the set of gems defined by the Gemfile included. We use the console a lot during development.
Currently when I run the command, I receive the following deprecation message:
[DEPRECATED] bundle console will be replaced by bin/console generated by bundle gem <name>
Digging around in the bundler docs I found the following explanation:
The bundle console will be removed and replaced with bin/console.
Over time we found bundle console hard to maintain because every
user would want to add her own specific tweaks to it. In order to
ease maintenance and reduce bikeshedding discussions, we're removing
the bundle console command in favor of a bin/console script
created by bundle gem on gem generation that users can tweak to
their needs.
Can anyone with knowledge provide a more detailed explanation? This gem currently does not have a bin directory. I'm happy to make one, I'm just not sure what should be in the file. Running bundle gem as described in the note above raises an error (as expected).
This is the file that is generated at bin/console:
#!/usr/bin/env ruby
require "bundler/setup"
require "(your gem name here)"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require "irb"
IRB.start(__FILE__)
You can see the template in the rubygems GitHub repo.

Rake tasks inside gem not being found

I have implemented what edebill suggested in his answer to this question.
If I point to the gem in the usual way, with it installed in my environment
gem 'activerecord_datawarehouse'
rake -T does not show my rake tasks, but if I point directly to the gem source code, like
gem 'activerecord_datawarehouse', :path => "/home/acras/code/activerecord_datawarehouse"
It shows and rake tasks work perfectly.
What could I be missing here? I did double checked and the installed gem is the same that I have in the source code.
Fixed it on my end. In the gemspec, you need to include the rake tasks files as well, not just the lib files:
Instead of:
s.files = Dir['lib/**/*.rb']
Use:
s.files = Dir['lib/**/*.rb'] + Dir['tasks/*.rake']

Net::SFTP keeps throwing errors in resque

I am running rails 3.1rc4 and keep running into this error message in my resque queues
uninitialized constant Net::SFTP With the exception "NameError"
In my gemfile I have included:
gem 'net-sftp'
gem 'net-ssh'
gem 'net-scp'
I included net-scp, just in case it was a dependency. I have tested my resque task externally of rails and redis with straight ruby, in which I required 'net/ssh' and 'net/sftp'. The whole thing worked perfectly. However the same resque task continually throws the uninitialized constant Net::SFTP. When I comment out the sftp section that comes before the Net::SSH section, I end up getting a similar error.
uninitialized constant Net::SSH
Do I need to change something in my gemfile or require these gems in my resque class? Why do I have to use 'net/ssh' in ruby while I have to use 'net-ssh' in my gemfile in rails?
Thanks in advance!
Have you loaded the environment in your rake task?
task "resque:setup" => :environment do
# you can leave this blank
end
Resque, by default, runs standalone. If you need it to access things that are loaded by the app bundle, you need to load the environment.

'sass' in a non thread-safe way

I'm getting these warnings when trying to use sass in Rails 3.1 rc1.
WARN: tilt autoloading 'sass' in a non thread-safe way; explicit require 'sass' suggested.
WARN: tilt autoloading 'sass/plugin' in a non thread-safe way; explicit require 'sass/plugin' suggested.
This is my Gemfile.
gem "rails", "~> 3.1.0.rc1"
gem "haml"
gem "sass"
I've tried to create a file called sass.rb inside the config/initializers containing this code.
require "sass"
Changing the Gemfile to this.
gem "rails", "~> 3.1.0.rc1"
gem "haml"
gem "sass", require: false
But the warnings remains.
Anyone knows how to solve it?
I found the code that is printing the warnings, if that is to any help.
have you tried doing this in Gemfile?
gem "sass", :require => 'sass'
this is an explicit call, without using initializers. by the way consider that you're using a rc1 release.
I had the same problem, and was able to solve it by compiling assets locally before pushing to Heroku as mentioned in the article Rails 3.1+ Asset Pipeline on Heroku Cedar
RAILS_ENV=production bundle exec rake assets:precompile
I also tried Itecedors suggestion which also worked:
While precompiling assets, in Rails 3.1.1 and up, you can prevent initializing
your application and connecting to the database by ensuring that the following
line is in your > config/application.rb:
config.assets.initialize_on_precompile = false
On Heroku I was getting this same error and googling did not help me find the problem, so I thought I would add what I found to this questions since it comes up first when searching.
The problem was NOT this error, it was a smaller error while pushing the code up to Heroku. After the gems are listed these lines got me on the path to the answer:
Running: rake assets:precompile
rake aborted!
Tasks: TOP => environment
(See full trace by running task with --trace)
Precompiling assets failed, enabling runtime asset compilation
Injecting rails31_enable_runtime_asset_compilation
Please see this article for troubleshooting help:
http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting
I had just been configuring Redis on Heroku so I knew that the problem had to be something related to those changes. At that URL I found this:
While precompiling assets, in Rails 3.1.1 and up, you can prevent initializing your
application and connecting to the database by ensuring that the following line is in your > config/application.rb:
config.assets.initialize_on_precompile = false
Adding the on_precompile = false line fixed all the errors, including the original one in this question.

Resources