Rake error: Stack level too deep - ruby

I know this is a common question, but I just started a project with the new Rails 3.1.0 and Ruby 1.9.2 p290
On my first migrations I had already this error and I am not sure why.
Aurelien$ rake db:migrate --trace
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/version.rb:4: warning: already initialized constant MAJOR
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/version.rb:5: warning: already initialized constant MINOR
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/version.rb:6: warning: already initialized constant BUILD
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/version.rb:3: warning: already initialized constant NUMBERS
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/version.rb:9: warning: already initialized constant VERSION
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake.rb:26: warning: already initialized constant RAKEVERSION
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/early_time.rb:17: warning: already initialized constant EARLY
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/alt_system.rb:32: warning: already initialized constant WINDOWS
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/application.rb:28: warning: already initialized constant DEFAULT_RAKEFILES
WARNING: Possible conflict with Rake extension: String#ext already exists
WARNING: Possible conflict with Rake extension: String#pathmap already exists
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/task_arguments.rb:73: warning: already initialized constant EMPTY_TASK_ARGS
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/invocation_chain.rb:49: warning: already initialized constant EMPTY
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_utils.rb:10: warning: already initialized constant RUBY
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_utils.rb:84: warning: already initialized constant LN_SUPPORTED
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/dsl_definition.rb:143: warning: already initialized constant Commands
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_list.rb:44: warning: already initialized constant ARRAY_METHODS
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_list.rb:47: warning: already initialized constant MUST_DEFINE
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_list.rb:51: warning: already initialized constant MUST_NOT_DEFINE
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_list.rb:55: warning: already initialized constant SPECIAL_RETURN
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_list.rb:61: warning: already initialized constant DELEGATING_METHODS
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_list.rb:364: warning: already initialized constant DEFAULT_IGNORE_PATTERNS
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/file_list.rb:370: warning: already initialized constant DEFAULT_IGNORE_PROCS
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake.rb:64: warning: already initialized constant FileList
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake.rb:65: warning: already initialized constant RakeFileUtils
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
stack level too deep
/Users/Aurelien/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rake-0.9.2/lib/rake/task.rb:162
I also removed the gems and re-tyring with db:reset
source 'http://rubygems.org'
gem 'rails', '3.1.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
#gem "rspec-rails", :group => [:test, :development]
group :test do
#gem "factory_girl_rails"
#gem "capybara"
#gem "guard-rspec"
#gem "spork", "> 0.9.0.rc"
#gem "guard-spork"
end
The rake commands work with bundle exec, but it would be nice to avoid using it.
Thank you in advance for tips and answers.
Aurelien

You should specifically add Rake version >= 0.9.2 in your Gemfile!
There was a bug with some Rails 3 versions where you would see strange errors like this when you use an older Rake version.
In your Gemfile:
gem 'rake' , '>= 0.9.2'
I'd also recommend you create a new gemset specifically for your application, e.g.
rvm gemset create yourproject
rvm gemset use yourproject
or:
rvm gemset use yourproject --default
for the new gemset, you might have to add "gem install rake" manually, then run "bundle install"
Using a separate gemset in addition to using your Gemfile is the best way to keep your gem versions in your project stable, and decoupled from other projects.

Related

Ruby - Cannot use locally installed gem

I've written a simple PasswordGenerator gem that I have at ~/workspace/gems/password_generator and have an app at ~/workspace/rubysamples/app where I want to use it. I have a Gemfile, the content of it is this:
gem 'password_generator', path: '~/workspace/gems/password_generator'
I installed it locally, like this:
bundle install --local
Resolving dependencies...
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `~/workspace/gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
It looks like it's installed locally:
bundle info password_generator
* password_generator (0.1.0)
Summary: Simple password generator
Homepage: https://github.com/jedrekdomanski/password_generator
Path: /home/jedrek/workspace/gems/password_generator
When I try to use it
~/workspace/rubysamples/app/password_reset.rb
PasswordGenerator.generate
I get an error
uninitialized constant PasswordGenerator (NameError)
What am I doing wrong? Am I missing anything?
Here's my gem repo: https://github.com/jedrekdomanski/password_generator
I also tried pointing to my repo and branch in the Gemfile
gem 'password_generator', git: 'git#github.com:jedrekdomanski/password_generator.git', branch: 'master'
but I get the same error message uninitialized constant PasswordGenerator (NameError)
There are potentially two issues. The first is how you are starting Ruby and the second is how you are requiring your module.
First, if you are starting Ruby by running ruby password_reset.rb then you are ignoring the Gemfile. The Gemfile is only used when you're using bundler, so you want to make sure you are starting Ruby by running bundle exec ruby password_reset.rb. This causes bundler to read your Gemfile and execute Ruby in that context.
Second, you're not properly including your module in your Ruby file. Just because you've added the gem to your Gemfile and started Ruby using bundler doesn't mean that the Ruby process knows you intend to use that gem's module; it just makes the module available for use. You might wonder, "Why don't I have to do that in Rails?" Because Rails does that for you automatically via config/application.rb.
Given these two issues, the correct way to accomplish your goal is to configure your app as follows:
First, create your Gemfile:
# Gemfile
gem 'password_generator', path: '~/workspace/gems/password_generator'
Second, create your password_reset.rb file:
# password_reset.rb
# Manually require any libraries that this app will use, even if defined in Gemfile
require 'password_generator'
# Call `puts` so something is printed to the console when this app runs
puts PasswordGenerator.generate
Third, run bundle install to ensure your Gemfile is properly formatted and to generate your Gemfile.lock:
⇒ bundle install
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `../../gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Fourth, run bundle exec ruby password_reset.rb and observe the output:
⇒ bundle exec ruby password_reset.rb
kpiDfyTxtdAsKmYuZqmK
Everything works because:
Ruby is started with Bundler
Bundler reads your Gemfile and makes the gems available to Ruby
Your app requires the module from the gem before attempting to use the module

how do I require 'rake' to be able to use FileList in .gemspec?

I am using :path => '/path/to/gem' functionality of bundler to build and use a modified upstream gem, which uses Rake::FileList in its .gemspec.
At this stage, the bundle I'm installing is not yet activated, or maybe the order things are installed forbids bundler from using rake.
I am using ruby 1.8.7.
My Gemfile:
source 'http://rubygems.org'
gem "rake"
gem "foreign_gem", :path => '/home/user/src/foreign_gem'
The error I get:
$ bundle install
Unfortunately, a fatal error has occurred. Please see the Bundler
troubleshooting documentation at http://bit.ly/bundler-issues. Thanks!
/home/ilya/src/foreign_gem/foreign_gem.gemspec:11: uninitialized constant FileList (NameError)
from /home/user/.rbenv/versions/1.8.7-p358/lib/ruby/site_ruby/1.8/rubygems/specification.rb:426:in 'initialize'
from /home/user/src/foreign_gem/foreign_gem.gemspec:1:in 'new'
from /home/user/src/foreign_gem/foreign_gem.gemspec:1
You should be able to add require 'rake' at the top of your foreign_gem.gemspec file in order to use FileList.
I don't know if this is a best practice, but it should work.

uninitialized constant StoreController::Yajl Rails app

I've included the yajl gem in my Gemfile via gem 'yajl-ruby', '~> 1.1.0' however upon calling parser = Yajl::Parser.new in my store controller I receive the error uninitialized constant StoreController::Yajl
If I execute require 'yajl' at the beginning of the controller all is well but I was under the impression that the gem should be available to controllers within the application simply by including in the gem file.
You have to indicate to Bundler the main file of the gem with the require option:
gem 'yajl-ruby', '~> 1.1.0', require: 'yajl'
This is necessary for gems whose name is different that their main file name.
See the Gemfile manual.

Arel Deprecation Warning when running rake db:create

I'm trying to create an app in Rails 3.1 with mysql2 v 0.2.6. When running rake db:create, I get the following error:
DEPRECATION WARNING: Arel::Visitors::VISITORS is deprecated and will be removed.
Database adatpers should define a visitor_for method which returns the appropriate
visitor for the database. For example, MysqlAdapter.visitor_for(pool) returns
Arel::Visitors::MySQL.new(pool). (called from mon_synchronize at
c:/Ruby192/lib/ruby/1.9.1/monitor.rb:201)
I can access the MySQL monitor, so the gem appears to be installed correctly. What else could be going on here?
Thanks!
Try updating your msql gem.
In ./Gemfile
gem require 'mysql2', '~> 0.3.6'
bundle update mysql2
use the below specified version in gemfile.
gem 'mysql2', '~>0.3.10'
then run
bundle install

Ruby 1.9.2-p290 through rbenv produces warning from psych-1.2.1: VERSION and LIBYAML_VERSION

When I running ruby I get the following warnings:
~/.gem/gems/psych-1.2.1/lib/psych.rb:93: warning: already initialized constant VERSION
~/.gem/gems/psych-1.2.1/lib/psych.rb:96: warning: already initialized constant LIBYAML_VERSION
I've tried googling around but can seem to find a solution which makes me think it's specific to my system. Is there a way to fix this?
This problem seems to originate in psych being called by bundler in Bundler.setup. The work-around I used:
gem 'psych'
In my code before
Bundler.setup
There are two ways.
->First
add gem 'psych'
in your Gemfile file and then run
bundle install
-> Second
gem uninstall psych
at console

Resources