initialized constant RAILS_ROOT - ruby-on-rails

Whilst following these instructions to install wizardly-examples, I get the following error:
[wizardly-examples]$ rake gems:install
(in /Users/jason/Rails/wizardly-examples)
(eval):1: warning: already initialized constant RAILS_ROOT
rake aborted!
wrong number of arguments (1 for 0)
/Users/jason/Rails/wizardly-examples/Rakefile:4:in `<top (required)>'
Can anyone advise how to resolve the issue? Thank you

This could be an out of date gem that's causing trouble. rake gems:install for one never worked properly, and has been eliminated in favor of:
bundle install
Rails 3 has switched to the bundler system by default. Older projects may still have the old task but it shouldn't be used. Since the Rakefile was often dependent on the very gems it was trying to install, you can see how it was a bad idea from the start.
In general if you're having trouble with rake tasks be sure to engage the --trace option to get a better idea of where it's going wrong.

Related

SSHKit error during Capistrano deployment of Rails app

After installing a new gem, Slackistrano to already succesfully deployed app with Capistrano, our deployments started failing with the following error during rake assets:precompile or rake db:migrate tasks
cap aborted!
SSHKit::Command::Failed: rake stdout: Nothing written
rake stderr: Nothing written
/Users/pete/.rvm/gems/ruby-2.2.2/gems/sshkit-1.3.0/lib/sshkit/command.rb:94:in `exit_status='
This error is obviously not very helpful.
If I ran the capistrano task cap staging assets:precompile, it would succeed.
So what's going on here?
There are a few other solutions to SSHKit failures during cap deployments, but most of them were for first time deployments and didn't work for me.
Here's how I figured out the solution(after much hair pulling).
I knew that it was probably the new gem that was causing the problem. So I went into the source code for sshkit/command.rb and outputted the command it was trying to execute on the remote server.
I then logged into the remote server and ran it manually and got the following output:
$ cd /home/web/sites/site.com/releases/20160106224143 && ( RAILS_ENV=production ~/.rvm/bin/rvm default do bundle exec rake db:migrate )
rake aborted!
NoMethodError: undefined method `each_pair' for "slack:deploy:updating":String
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1602:in `compile!'
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1344:in `add_filter'
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1331:in `before'
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1989:in `block (2 levels) in delegate'
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/slackistrano-1.0.0/lib/slackistrano/tasks/slack.rake:121:in `<top (required)>'
So, the problem started with Slackistrano, but then, what? why is Sinatra there? I don't use Sinatra in my app.
Well, I discovered that there was a gem in my Gemfile that was for development only(mailcatcher) but was in the production section of the Gemfile. I removed this and redeployed successfully.
However, there is obviously a deeper issue because there seems to be some sort of name collision on the "before" method of sinatra and capistrano that caused this in the first place.
I'm posting here to help others debug possible SSHKit errors during Cap deployments and also perhaps to guide others on weird name collisions between Sinatra and Capistrano.
For me this issue can arise if you're accidentally on an incompatible version of Ruby whilst running Capistrano. I had to add a .ruby-version to clamp it for me to 2.7.5 whilst using gem 'capistrano', '3.11.0'.
So I'm not sure if stating Ruby version 2 or higher is strictly true.

Rake says "Don't know how to build task X", but "rake -T" lists it anyway - why?

I have a new OS X El Capitan setup with Ruby 2.2 (Macports), Rails 4.2.4 and Bundler for an existing app. Among a lot of other gems, this app uses parallel_tests to make good use of my 8-core machine. rake -T lists all the parallel test and spec tasks fine and in the past I could execute them. But when I do this now, after the upgrade, I just get
$ /opt/local/bin/rake parallel:prepare --trace
rake aborted!
Don't know how to build task 'parallel:prepare'
/opt/local/lib/ruby2.2/2.2.0/rake/task_manager.rb:62:in `[]'
/opt/local/lib/ruby2.2/2.2.0/rake/application.rb:149:in `invoke_task'
What can cause a task to be listed in rake -T, but not be found when I try to execute it?
In this case, the reason was that I had put the gem in question inside a group :test in my Gemfile. Apparently this caused the problem, removing it from the group and making it available globally resolved the problem (after calling bundle again).
I don't know whether this is a generic solution, however.

Rails: Could not find rake-10.1.1 in any of the sources

I am experiencing an error with running the Rake command. When I try to Rake in my Rails project, I get an error that says Could not find rake-10.1.1 in any of the sources.
I put Rake 10.1.1 in my Gemfile, but it appears to do nothing when I bundle install.
I've tried uninstalling then installing the Rake gem in my computer's Ruby, RVM Ruby-2.1.0, the RVM Ruby-2.1.0#global, and the RVM gemset that I created for the specific project. I've also tried removing the Gemfile.lock and then bundle installing.
I've tried to manually run rake out of the terminal in multiple different Ruby/gems files including RVM. It gives this error:
/Users/me/.rvm/gems/ruby-2.1.0/gems/rake-10.1.1/bin/rake ; exit;
~ me$ /Users/me/.rvm/gems/ruby-2.1.0/gems/rake-10.1.1/bin/rake ; exit;
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
I have a Rakefile in my project and have tried renaming it as well (Rakefile.rb, rakefile).
When I run Rake commands such as rake db:migrate, I get this error:
rake aborted!
undefined local variable or method config' for main:Object
/Users/me/RailsProject/config/environment.rb:4:in'
/Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in require'
/Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:inblock in require'
/Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in load_dependency'
/Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:inrequire'
/Users/me/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/application.rb:189:in require_environment!'
/Users/me/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/application.rb:250:inblock in run_tasks_blocks'
/Users/me/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in eval'
/Users/me/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
I've looked everywhere for the solution before coming here, but I could not find it. I would be appreciative if someone could give me some advice on this issue. Thank you in advance.
Looks like it it having trouble finding dependencies. Have you installed rake on your system with gem install rake? If not you will need to use bundle exec rake.
I don't know what the problem was, but I solved it by creating a new gemset, installing Rake to it, and using it. I also switched from Rubymine to Sublime. It may have been Rubymine trying to use a different gemset. Beware of those IDE settings.

Consistent Testing error (Rspec-core 2.14.5)

I'm going through Mhartl's tutorial and I'm sporadically getting this error when I run tests in the sublime text 2 console. If I use bundle exec rspec/rspec in the the terminal it works fine, but it's not the case when call shortcut commands in sublime text to run tests. I don't know if it makes a difference but I'm also running guard
Users/User/.rvm/gems/ruby-2.0.0-p247#global/gems/bundler-1.3.5/lib/bundler/runtime.rb:33:in `block in setup': You have already activated rspec-core 2.14.5, but your Gemfile requires rspec-core 2.13.1. Using bundle exec may solve this. (Gem::LoadError)
I tried searching stack overflow for solutions and tried bundle exec rake db:migrate(I normally don't specify bundle exec) as well as bundle exec rake test:prepare. Neither of which work.
I'd appreciate not only an answer to the problem but perhaps a more in-depth answer of when to use rake db:migrate and rake test:prepare. I know their main uses, but there are perhaps some less obvious times where i would need to run it. SPecifically, for this problem I would have thought rake test:prepare would be relevant and I'm still not sure what effect rake db:migrate has on the testing suite/rspec part of the application
To answer part of your question, you can delete unused gems through bundle clean, although when I've had multiple versions of gems, I've found that I had to use bundle clean --force which may mean that I had "system gems" coming into play, per https://stackoverflow.com/a/10190608/1008891.

rake db:migrate returns no such file to load -- rake/dsl_definition (I've searched and tried adding require 'rake/dsl_definition')

My hair is all gone and I'm about to throw my new macbook through the window.
I am trying to execute the famous db:migrate command and I get the following:
rake aborted!
no such file to load -- rake/dsl_definition
I am using rake 0.8.7...it's installed and included in my gemfile. I didn't have require 'rake/dsl_definition' in my rake file and I was getting the famous uninitialized-constant error. Now that I have added require 'rake/dsl_definition' to my rake file I get the error above. Anyyyyy ideas...I have searched and tried a million things with no luck... My migrations worked 2 days ago, now suddenly they don't. I have uninstalled rake 0.9.2 and also did a few other thing suggested in the link below, but nothing worked.r
How to fix the uninitialized constant Rake::DSL problem on Heroku?
heroku rake require 'rake/dsl_definition' fix not working + breaking local rake
How to fix the uninitialized constant Rake::DSL problem on Heroku?
Having rolled back your rake to 0.8.7, you need to remove the require rake/dsl-definition line from your rakefile. You only get the uninitialised constant error with later rakes.

Resources