rake command looks for Rakefile up the directory chain? - ruby

I am using Rails3 and accidentally I ran rake command from within lib directory. rake command successfully ran. I think rake command looks for Rakefile all the up the chain. Is that true?

Yes. According to http://docs.rubyrake.org/user_guide/chapter02.html you can avoid that by:
rake -N

Related

Jenkins and running rake tasks

I'm having some issues running a rake task from within Jenkins (within a docker container) as part of a build process (I have the Rake plugin installed). I am getting the error
java.io.IOException: Cannot run program "rake" (in directory "/var/jenkins/workspace/HendricksFeaturesCopy"): error=2, No such file or directory
Which i don't understand as when i pwd $ECHO before the rake task is invoked i get.
/var/jenkins/workspace/HendricksFeaturesCopy
So i'm in the correct place and rake is installed as its located here
/usr/local/rvm/rubies/ruby-2.3.0/bin/rake
My Rakefile looks like
import 'lib/tasks/yard-docs.rake'
lib/tasks/yard-docs.rake
require 'yard'
namespace :yard_docs do
desc 'Generate Yard Documentation'
task :generate do
# Generate Yard Documentation
end
end
Does anyone know how to rectify this or what I am missing?
UPDATE
After doing echo $PATH
/usr/local/rvm/gems/ruby-2.3.0/bin:/usr/local/rvm/gems/ruby-2.3.0#global/bin:/usr/local/rvm/rubies/ruby-2.3.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/rvm/bin
So this means the correct gemset is being used and when I have done rvm #global do gem list
rake (10.4.2)
So, rake is available right ?
Make sure /usr/local/rvm/rubies/ruby-2.3.0/bin/ is in your $PATH.
PATH=/usr/local/rvm/rubies/ruby-2.3.0/bin/:$PATH
Alternatively, symlink /usr/local/rvm/rubies/ruby-2.3.0/bin/rake to /usr/local/bin/
ln -s /usr/local/rvm/rubies/ruby-2.3.0/bin/rake /usr/local/bin/

Could not detect rake tasks

ensure you can run `$ bundle exec rake -P` against your app with no environment variables present
and using the production group of your Gemfile.
This may be intentional, if you expected rake tasks to be run
cancel the build (CTRL+C) and fix the error then commit the fix:
rake aborted!
I don't have any rake tasks that I'd like to run automatically. Should I just ignore this warning?
Effective December 05, 2013, Heroku added debug output to deploys using the Ruby build pack.
The error is triggered:
if the assets:precompile task does not exist or
if there is an error in the Rakefile.
Two common causes of errors in the Rakefile are referencing a dependency not available in production (such as rspec) or
expecting an environment variable to be present.
It's counter-intuitive because the app never runs without config vars, but Heroku's build process executes without config vars.
As per the error message, ensure you can run bundle exec rake -P RAILS_ENV=production with no environment variables present before pushing to Heroku (e.g. comment out your environment variables while running the aforementioned command).
Also, rest assured that rake's -P switch is harmless, so you can run it as much as you want until you fix this issue. This switch is used to display a list of all tasks and their immediate prerequisites. See Rake Command Line Usage if you want to double-check. The output may have over 200 lines, and look something like this:
rake about
environment
rake assets:clean
environment
rake assets:clobber
environment
rake assets:environment
rake assets:precompile
environment
rake db:_dump
...
rake tmp:pids:clear
rake tmp:sessions:clear
rake tmp:sockets:clear
I started getting this strange error all of a sudden yesterday. Heroku confirmed making an update to the Ruby buildpack...
It has to do with the Rakefile. Does your Rakefile require any files? Does it require your app files? If so, then the app should not raise exceptions when it is loaded without any config vars set.
It's counter-intuitive because the app never runs without the config vars set.
In my case, the Sinatra app was looking for database urls in the init file:
uri = URI.parse( ENV[ "REDISTOGO_URL" ])
This will raise an exception if there are no env vars set.
You may have the same issue with other database URL's, such as Mongo or Postgres.
So, protect against missing env vars:
if ENV[ "REDISTOGO_URL" ]
uri = URI.parse( ENV[ "REDISTOGO_URL" ])
...
You can check if it will work before pushing to Heroku by running bundle exec rake -P
Also, make sure all your tests pass after updating your init. Remove any cached init state by restarting Spork or similar.
Reference: Show Rakefile errors in Ruby deploys
you can also try enabling user-env-compile
heroku labs:enable user-env-compile
FWIW I just ran into this issue also.
It turned out that I had config.assets.css_compressor = :sass commented out, when there was a rake task referring to it in production.rb.
Very simple oversight, but that would cause rake assets:precompile to fail and thus cause this error.
Old question, but I ran into this issue just now, and there is a new fix for it. If you're using Ruby version <= 2.6.1, and Bundler 2.0.1, update Ruby to 2.6.3 ($ rvm install "ruby-2.6.3") and Bundler to 2.0.2 ($ gem install bundler '2.0.2'). Make sure to specify the new Ruby version in your Gemfile.
Unfortunately I can't tell you why this works, but it's worked for 3 other people on my team so far, so it's worth a shot.

Capistrano: run_locally in separate project

I'm using capistrano for deployment. I'm trying to define a task which will:
cd other-rails-app && bundle exec rake sometask
But it's complaining about Rake not being part of the bundle (which is obviously not true in the project where I am trying to run this task). I think it must be altering the environment somehow before executing the command and assuming bundler is already loaded (which is true in the capistrano task, but it's the wrong Gemfile).
How do I run a command locally with capistrano with a clean environment?
I figured out the problem. When you run capistrano, the BUNDLE_GEMFILE environment variable gets set to the Gemfile of the current project. So when you run bundle exec rake, Bundler looks first to the Gemfile set in the environment. The solution is to "reset" this Gemfile variable. I created a script for running my rake task in the root of my other project.
So instead of doing:
run_locally "cd other-rails-app && bundle exec rake mytask"
I do this:
run_locally "cd other-rails-app && ./mytask.rb"
In the mytask.rb file I do this:
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] = File.expand_path('Gemfile', File.dirname(__FILE__))
require 'bundler/setup'
%x{rake mytask}

How can I run rake with --trace within capistrano?

I want capistrano to invoke rake with --trace so I can figure out why it's failing. How do I do this? set :rake 'rake --trace' doesn't work.
The best way I found is:
set :rake, "#{rake} --trace"
This way you don't overwrite the rake variable.
For example if you use bundler this is set before to:
"bundle exec rake"
and after to:
"bundle exec rake --trace"
The chances are your custom tasks aren't using the rake variables, but instead hard-coding rake, here's an example:
run("rake sass:compile")
This is hard-coded, and won't care about your setting set :rake, 'rake --trace', here's the correct way:
run("#{fetch(:rake)} sass:compile")
Or, shorthand:
run("#{rake} sass:compile")
You can see this in practice in the Capistrano source code, in the one place that the default recipes actually invoke rake, in the migrations task: https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy.rb#L387

deploy rake task as if it were a common script

I like the rake task structure and rake utilities.. I want to create a script that can do many things, and rake seems the logical choice.
I want to know how can I make it look like a regular script:
./myscript.rb cake:bake
as opposed to
rake -f myscript.rb cake:bake
any idea? Of course, rake must still be installed etc.. simply to make it easier to use...
myscript.rb:
#!/usr/bin/ruby
require 'rubygems'
require 'rake'
namespace :cake do
task :bake do
puts "Baking cake..."
end
end
Rake::Task[ARGV.first].execute
Then on the command line:
chmod +x myscript.rb
./myscript.rb cake:bake
I found this for cygwin / windows
http://errtheblog.com/posts/60-sake-bomb
removes the dependency on rails and let's you have rake tasks installed and available globally

Resources