Bundler doesn't load gem in production rails app - ruby-on-rails

I'm having a problem where a single gem is not being loaded by bundler in a rails app, but only in production - there's no problem using the gem in development mode.
The gem is country_select, and we've tried it with version 1.1.3 and the github repository. The app is using rails 3.2.11.
Proof that the gem exists on the server
The gem is successfully installed by bundler in the production environment:
$ RAILS_ENV=production bundle install
...
Using coffee-rails (3.2.2)
Using country_select (1.1.3)
Using daemons (1.1.9)
...
Your bundle is complete! It was installed into /home/deploy/rails-app/shared/bundle
The gem appears in the shared gem directory that bundler installs to:
$ ls -al /home/deploy/rails-app/shared/bundle/ruby/1.9.1/gems/ | grep country
drwxrwxr-x 3 deploy deploy 4096 Apr 3 08:49 country_select-1.1.3
And the gem is loaded when starting rails from the console:
$ RAILS_ENV=production script/rails c
Loading production environment (Rails 3.2.11)
irb(main):002:0> Gem.loaded_specs.keys.grep /country/
=> ["country_select"]
However...
That same check for the country_select gem in the rails app fails. For example:
class ApplicationController < ActionController::Base
before_filter do
raise Gem.loaded_specs.keys.grep(/country/).inspect
end
#...
end
Spits out [] - i.e. the gem is not loaded. Plus, parts of the application that rely on the gem fail as a result of it not existing.
And finally, the question
Simply adding a require 'country_select' somewhere in the startup means that the gem gets loaded, and the app works. But why isn't bundler doing it with that particular gem, despite the fact that it installs it and adds it to the app's gem directory?
More info about the production environment:
$ rvm -v
rvm 1.18.18 (stable)...
$ ruby -v
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
$ gem -v
1.8.25
$ bundle -v
Bundler version 1.3.2
Update: 5th April 2013
The problem seems to have "gone away". After a couple of redeployments, it appears to be working. This is in spite of the fact that the country_select gem hasn't been changed, which is very odd. I'm still looking into exactly what has changed in that time, but I'm puzzled.

try running
RAILS_ENV=production bundle exec script/rails c

I have an app using the same versions and I don't need to require the gem in dev or production. I also tried with your before_filter and got ["country_select"] as expected. So the problem isn't with the gem.
I think you're either monkeying around with the loaded gems in initialization somewhere (unlikely) or there's something going wrong with your setup around the server. Are you starting the server with RAILS_ENV=production bundle exec script/rails server? Can you check the gem path being used by the server includes the shared gem path you mentioned in your question?

Related

LoadError when using a Minitest extension from one vendored gem in another vendored gem

I have a problem with two vendored gems of a Rails 3.2 app. One gem is a Minitest extension, the other a piece of code which should use the Minitest extension in it's test suite. Since both are under development, both are vendored and not yet pushed to Rubygems.
To reproduce the issue, I've bootstrapped a bare Rails 3.2 app and added two gems to vendor/gems: minitest-great_expectations (a working Minitest extension cloned from Github) and mygem which only reports and tests it's VERSION.
First let's try with the minitest-great_expectations gem from path:
git clone https://github.com/svoop/minitest_sandbox.git
cd minitest_sandbox/vendor/gems/mygem/
cat Gemfile # note "path:"
bundle install # note "Using minitest-great_expectations (0.0.5) from source at ../minitest-great_expectations"
ruby test/lib/mygem/version_test.rb # => test_helper.rb:6 - cannot load such file -- minitest/great_expectations (LoadError)
And now the same with the minitest-great_expectations gem from Rubygems. Edit the Gemfile and use the line without the "path:".
$EDITOR Gemfile # remove the "path: ..."
bundle install # note "Installing minitest-great_expectations (0.0.5)"
ruby test/lib/mygem/version_test.rb # => PASS
Reverting back will now work since the gem is installed. However, it doesn't matter whether the Gemfile contains the "path:" or not. Explicitly uninstall the gem with gem uninstall minitest-great_exceptions and you're back to LoadError.
Any idea what's missing here?
You should be running bundle exec ruby test/lib/mygem/version_test.rb.
Path gems are a Bundler-specific extension to Rubygems, so bundle exec is necessary to set up the load path correctly.

Bundle is switchng my rails version but I have no gem file. How to stop this?

I create a gemset with
$ rvm gemset create r3
$ rvm gemset use r3
$ gem install rails -v3.2.13
At this point
$ rails -v
now shows
$ Rails 3.2.13
but every time I do bundle with a project I've forked, I find that
rails -v
shows Rails 4.0.1 - which then gives issue with the project in question when running tests
(4.0.1 conflicts with 3.2.13).
My question is - if my Gemfile only has:
$ cat Gemfile
source 'https://rubygems.org'
gemspec
how is this happening? How can I make my command line ruby version stay at 3.2.13 and not switch to 4.0.1 Once it switches to 4.0.1 I seem to be stuck with that for that gemset and to create a 3.2.13 gemset I have to start over again.
Bundle works by finding the most up-to-date version of the gems that are compatible with the restrictions from the Gemfile. In this case, those restrictions are coming from the gemspec file, which presumably allows versions of Rails greater than 3.2.13. So it's picking the most up-to-date version of Rails allowed - which is 4.0.1. Your RVM configuration is not relevant here.
To lock your particular fork to Rails 3.2.13, just add the following:
gem 'rails', '3.2.13'
to the Gemfile in your fork. This will lock the local version to 3.2.13 when you run bundle.
if you wish to explicitly use different version of rails then the one that would be calculated from Gemfile then you need to use:
NOEXEC_DISABLE=1 rails ...
you can make it permanent for single shell session with:
export NOEXEC_DISABLE=1
rails ...
and to disable loading gems in versions calculated via Gemfile put it in you shell initialization file (like ~/.bash_profile or ~/.zlogin):
export NOEXEC_DISABLE=1
this happens because RVM installs gem rubygems-bundler which automatically analyzes your Gemfile when you run gem binaries and if a version specified via this file is available then it is loaded (even if it is specified only as dependency of your gems).

Why it's so slow to use `rails new`?

The version is:
bash-3.2$ rails -v
Rails 3.2.9
And my ruby is the built-in ruby on MacOSX:
bash-3.2$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
bash-3.2$ which ruby
/usr/bin/ruby
My problem is that in the last step of rails new ( run bundle install), it seems that rails need to connect the internet to download some packages. And usually I need 5 minutes just wait for rails to create any project using rails new.
I was wondering whether somethings is not configured well for rails on my computer.
Another strange thing is that after I finished rails new PROJECT_NAME, it shows:
Using sqlite3 (1.3.6)
Using uglifier (1.3.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
It seems that sqlite3 has already been installed, however, when I try:
bash-3.2$ bundle show sqlite3
Could not locate Gemfile
It still complains that sqlite3is not installed. Maybe that's the reason that rails new is slow (Even sqlite3 has been installed once. It still needs to download the bundle)
Before bundle install, rails asks for my system password:
Enter your password to install the bundled RubyGems to your system
But I'm still not sure whether these packages have been installed into system, as bundle show sqlite3 failed.
Does anyone have ideas about why rails new is so slow on my computer? Thanks!
Another solution is to use RVM. It is similar to rbenv. I personally use RVM. I don't and would not recommend using the gemsets. Bundler does the separation of gem versions anyway.
Check what is up with your bundle install i.e. the last step in the rails new sequence. Run this instead:
rails new appname -B
The -B flag tells rails to "skip bundle" (reference). Then go into the app folder to run:
bundle install --verbose
Check for potential errors in the output.
Using different bundler versions can give you different results. So try other versions.
gem uninstall bundler -v your-version-number
gem install bundler -v new-version-number
In 2016, we got bundler v1.12.x which is causing some users to experience slow bundle install issues. In this case, using 1.11.2 is the faster option until they fix it.

Trouble removing rake 0.9

I am having some trouble removing the rake gem version 0.9. I managed to run gem uninstall rake and I believed this to have deleted it. However, going back to gem list it still showed that it still exists.
Running gem env:
GEM PATHS:
/home/sean/.rvm/gems/ruby-1.9.2-p180
/home/sean/.rvm/gems/ruby-1.9.2-p180#global
I went into the #global directory and manually deleted it there, yet still it remains somewhere. I can see it when I run gem list. The 0.9 rake gem is causing problems for my Rails 3.0.7 application.
I ran gem list -d rake:
rake (0.9.0, 0.8.7)
Installed at
(0.9.0): /home/sean/.rvm/gems/ruby-1.9.2-p180#global
(0.8.7): /home/sean/.rvm/gems/ruby-1.9.2-p180
I don't see anything in the gems directory for ruby-1.9.2-p180#global
You should run
rvm use #global && gem uninstall rake -v 0.9.0
rvm use # && gem uninstall rake -v 0.9.0
to correctly remove rake 0.9.0 from rvm
In terminal type in: gem uninstall rake, then select version 0.9.0 when prompted.
Then modify your Gemfile within your Rails application:
gem 'rake', '0.8.7'
Then in terminal run:
bundle install
These steps should fix the problems you are having. Upgrade to rake again when you upgrade to rails 3.1.
If you're using rvm you get rake 0.9 installed "for free" in the global gemset which makes it hard (impossible?) to uninstall properly. gem uninstall rake is successful but doesn't actually remove rake 0.9. I had to do it manually for both REE and 1.9.2.
I edited the rake executable script (find with which rake) and added a:
puts "Gem.bin_path: #{Gem.bin_path('rake', 'rake', version)}
...before the last line. This prints (on my system):
Gem.bin_path: ~/.rvm/gems/ree-1.8.7-2011.03#global/gems/rake-0.9.0/bin/rake
To remove 0.9 cd into ~/.rvm/gems/ree-1.8.7-2011.03 and manually delete all rake-0.9 files (gems, specifications are the ones that count I believe).
I ran into this same problem (needed rake 0.8.7 but got 0.9.0 "for free" with rvm). Initially gem list -d rake gave the following output:
rake (0.9.0, 0.8.7)
Installed at (0.9.0): ~/.rvm/gems/ruby-1.9.2-p136#global
(0.8.7): ~/.rvm/gems/ruby-1.9.2-p136#my_gemset
Switching to the global gemset with rvm use #global gave me this output:
rake (0.9.0)
Installed at: ~/.rvm/gems/ruby-1.9.2-p136#global
So I just did gem uninstall rake. Now, switching back to my gemset with rvm use #my_gemset the correct version of rake (0.8.7) is being used.
If you're using bundler, you should read this excellent article by Yehuda Katz. Following his instructions means you don't have to uninstall rake 0.9.0. Try following command:
bundle exec rake --version
Prepending bundle exec will make sure that only the correct version of the rake is loaded, regardless of others you have installed.
Did you try using the 'gem cleanup' script ?

Getting App Crashed error when deploying Rails application to Heroku

I followed all the guidelines given on the heroku blog for deploying a Rails app. I also my app got pushed successfully and created an app in my account. But when I try to run my site it gives me the following error:
App crashed
This application is temporarily offline.
If you're the administrator of this app, please check your heroku logs for the
backtrace.
I tried to check the logs and here's what I got
C:\Users\raw\Desktop\html\rohit>heroku logs
Missing the Rails 2.3.8 gem. Please `gem install -v=2.3.8 rails`, update your RAILS_GEM_
VERSION setting in config/environment.rb for the Rails version you do have installed, or
comment out RAILS_GEM_VERSION to use the latest version installed.
==> dyno-3674485.log (crash) <==
Missing the Rails 2.3.8 gem. Please `gem install -v=2.3.8 rails`, update your RAILS_GEM_
VERSION setting in config/environment.rb for the Rails version you do have installed, or
comment out RAILS_GEM_VERSION to use the latest version installed.
-----> Rails can't find the expected version.
Check to ensure you have specified the correct version of Rails in your
Gemfile or .gems. See http://docs.heroku.com/gems for details.
You may also be affected by a gem dependency issue with rack.
See http://docs.heroku.com/gem-dependency for details.
Examine the backtrace above this message to debug.
II have made a .gems file in my Rails_App directory:
rails -v '2.3.8'
pg
rack -v '1.1.0'
haml -v '3.0.13'
formtastic -v '0.9.8'
authlogic -v '2.1.5'
subdomain-fu -v '0.5.4'
compass -v '0.10.2'
compass-colors -v '0.3.1'
paperclip -v '2.3.3'
activemerchant -v '1.9.0'
icalendar
What can I do to get this to work?
Rails 2.3.8 is not compatible with rack 1.2.1 You can't define you .gems like that you have a "gem dependency issue with rack" like define.
Try with rake -v=1.1.0 the dependency of ActionPack 2.3.8 ( http://rubygems.org/gems/actionpack/versions/2.3.8)
My Heroku app, running on bamboo-ree-1.8.7 stack, (run heroku info to determine what stack you are running), I have the following configuration:
first line of my .gems file
rails --version 2.3.8
You may also need to add the following to your config/environment.rb file
config.gem 'rails', :version => '2.3.8'
You will need to do another git push to heroku for these changes to take effect.
I added the command to ignore depedencies in the .gems file as --ignore-depedencies and now it works thanks guyz for your time.
The issue is solved.
PS: I had kept the solution as a comment, now submitting it as an answer.

Resources