New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bundle install does not work from rails generator #3153

Closed
tanraya opened this Issue Sep 28, 2011 · 15 comments

Comments

Projects
None yet
5 participants
@tanraya
Copy link
Contributor

tanraya commented Sep 28, 2011

How to reproduce this:

$ rails new testapp
$ cd ./testapp
$ rails g generator somegen

Then change generator to this:

class SomegenGenerator < Rails::Generators::Base
 source_root File.expand_path('../templates', __FILE__)

 def install
   gem "unicorn"
   run "bundle install"
 end
end

And then run generator:

$ rails g somegen

This will generate output:

 gemfile  unicorn
 run      bundle install

Could not find gem 'unicorn (>= 0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

I'm using ruby 1.9.2, Rails 3.1, RVM, Ubuntu

@dyba

This comment has been minimized.

Copy link
Contributor

dyba commented Oct 7, 2011

Rails Generators use Wycats' Thor gem. I would recommend brushing up on Thor actions: thor/actions.rb

The following code works fine:

class SomethingGenerator < Rails::Generators::Base
  source_root File.expand_path('../templates', __FILE__)

  def install
    gem "unicorn"
    inside Rails.root do
      run "bundle install"
    end
  end
end

@josevalim, @spastorino, @tenderlove, @fxn, @jonleighton: I think this issue should be closed. By the way, I'd rather not CC everyone, is there someone in particular I should CC when requesting to close an issue?

@tanraya

This comment has been minimized.

Copy link
Contributor Author

tanraya commented Oct 7, 2011

dyba, did you tested it? I try your generator in action:

$ rails g something
     gemfile  unicorn
     run  bundle install from "."
Could not find gem 'unicorn (>= 0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
@dyba

This comment has been minimized.

Copy link
Contributor

dyba commented Oct 7, 2011

@tanraya, no test for this one. It runs just fine. I changed "unicorn" for "haml", but the idea is the same.

Paintdexter@R0M3 Thu Oct 06 22:52 [ ~/Code/Rails/Issue3153 ]
>_ rails generate -h
/Users/Paintdexter/.rvm/gems/ruby-1.9.2-p290@RailsIssue3153/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV_
Usage: rails generate GENERATOR [args] [options]

General options:
  -h, [--help]     # Print generator's options and usage
  -p, [--pretend]  # Run but do not make any changes
  -f, [--force]    # Overwrite files that already exist
  -s, [--skip]     # Skip files that already exist
  -q, [--quiet]    # Suppress status output

Please choose a generator below.

Rails:
  assets
  controller
  generator
  helper
  integration_test
  mailer
  migration
  model
  observer
  performance_test
  plugin
  resource
  scaffold
  scaffold_controller
  session_migration

Coffee:
  coffee:assets

Jquery:
  jquery:install

Js:
  js:assets

Something:
  something

Paintdexter@R0M3 Thu Oct 06 22:52 [ ~/Code/Rails/Issue3153 ]
>_ rails generate something install
/Users/Paintdexter/.rvm/gems/ruby-1.9.2-p290@RailsIssue3153/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV_
     gemfile  haml
         run  bundle install from "."
Using rake (0.9.2) 
Using multi_json (1.0.3) 
Using activesupport (3.1.0) 
Using bcrypt-ruby (3.0.1) 
Using builder (3.0.0) 
Using i18n (0.6.0) 
Using activemodel (3.1.0) 
Using erubis (2.7.0) 
Using rack (1.3.4) 
Using rack-cache (1.0.3) 
Using rack-mount (0.8.3) 
Using rack-test (0.6.1) 
Using hike (1.2.1) 
Using tilt (1.3.3) 
Using sprockets (2.0.2) 
Using actionpack (3.1.0) 
Using mime-types (1.16) 
Using polyglot (0.3.2) 
Using treetop (1.4.10) 
Using mail (2.3.0) 
Using actionmailer (3.1.0) 
Using arel (2.2.1) 
Using tzinfo (0.3.30) 
Using activerecord (3.1.0) 
Using activeresource (3.1.0) 
Using ansi (1.3.0) 
Using bundler (1.0.18) 
Using coffee-script-source (1.1.2) 
Using execjs (1.2.9) 
Using coffee-script (2.2.0) 
Using rack-ssl (1.3.2) 
Using rdoc (3.9.4) 
Using thor (0.14.6) 
Using railties (3.1.0) 
Using coffee-rails (3.1.1) 
Using haml (3.1.3) 
Using jquery-rails (1.0.14) 
Using rails (3.1.0) 
Using sass (3.1.10) 
Using sass-rails (3.1.4) 
Using sqlite3 (1.3.4) 
Using turn (0.8.2) 
Using uglifier (1.0.3) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Paintdexter@R0M3 Thu Oct 06 22:52 [ ~/Code/Rails/Issue3153 ]
>_ cat Gemfile
source 'http://rubygems.org'

gem 'rails', '3.1.0'

# Bundle edge Rails instead:
# gem 'rails',     :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

gem 'jquery-rails'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

group :test do
  # Pretty printed test output
  gem 'turn', :require => false
end
gem "haml"
@tanraya

This comment has been minimized.

Copy link
Contributor Author

tanraya commented Oct 7, 2011

Hmm, its very very strange. I ran it on two different computers with Ubuntu/RVM/1.9.2/Rails 3.1, on the existing apps and got the same error:

$ rails g something
gemfile  unicorn
run  bundle install from "."
Could not find gem 'unicorn (>= 0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

I think this may be due to the RVM. Are you using RVM?

@dyba

This comment has been minimized.

Copy link
Contributor

dyba commented Oct 7, 2011

I am running on a Mac with RVM version 1.8.5. Are you using the latest version of RVM?

@dyba

This comment has been minimized.

Copy link
Contributor

dyba commented Oct 7, 2011

You should be running the command

rails g something install

Not

rails g something
@dyba

This comment has been minimized.

Copy link
Contributor

dyba commented Oct 7, 2011

@tanraya, do yo understand the error message you are getting? When it says it cannot find the gem in any of the sources listed in your Gemfile, it is very likely because you are not running the bundle install command in the right directory. That directory must contain a Gemfile, and that Gemfile must have a line that says source http://ruby gems.org or some other source.

@tanraya

This comment has been minimized.

Copy link
Contributor Author

tanraya commented Oct 8, 2011

Yep, I understand the message. I created new app with rails 3.1.1. Here is my Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.1.1'
gem 'sqlite3'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.1.4'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

group :test do
  # Pretty printed test output
  gem 'turn', :require => false
end

gem 'therubyracer'

And my generator (lib/generators/something_generator.rb):

class SomethingGenerator < Rails::Generators::Base
  source_root File.expand_path('../templates', __FILE__)

  def install
    gem "unicorn"
    inside Rails.root do
      run "bundle install"
    end
  end
end

Then I run generator:

$ rails g something install
/home/tanraya/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV_
     gemfile  unicorn
     run  bundle install from "."
Could not find gem 'unicorn (>= 0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
@tanraya

This comment has been minimized.

Copy link
Contributor Author

tanraya commented Oct 8, 2011

Hmm, after I install unicorn gem with gem install:

$ gem install unicorn

and run the generator:

$ rails g something install

It installed unicorn gem fine! Why is this happening?

@dyba

This comment has been minimized.

Copy link
Contributor

dyba commented Oct 8, 2011

Instead of running bundle install, try the following:

  def install
    inside Rails.root do
      run "cat Gemfile"
    end
  end

You should see the Gemfile displayed to the screen after you run the command rails g something install.

@cschiewek

This comment has been minimized.

Copy link

cschiewek commented Dec 29, 2011

I'm having the exact same issue. If the gem is not pre-installed on my machine, bundle install from within a generator fails. If I install the gem locally beforehand or run bundle install outside of the generator it works fine. Adding the inside Rails.root does nothing. Running 'cat Gemfile' returns the proper Gemfile with the new line for the new gem just fine.

@nicov

This comment has been minimized.

Copy link

nicov commented Jan 12, 2012

Hello,

I'm having the same issue too with rails 3.1.3

Actually I'm using templates, not generators, and the use of 'run "bundle install"' cause the same error using my templates with 'rake rails:template'.

When using the template at application creation (rails new app_name -m template) it works perfectly !!

Furthermore, the "bundle install" command succeed in any case if all the gems required by the Gemfile are locally installed.

I hope this will help

@nicov

This comment has been minimized.

Copy link

nicov commented Jan 13, 2012

After some investigation, I found out a way of executing bundle successfully in any case.

It appears that the issue occurs when the ENV['RUBYOPT'] is set to "-I$HOME/.rvm/gems/$RUBY_VERSION@$GEMSET/gems/bundler-$BUNDLER_VERSION/lib -rbundler/setup"

It is actually a known issue of bundler discuss there:"bundler/bundler#478"

For those facing the same issue, try to remove the "-rbundler/setup" from the ENV['RUBYOPT'] before executing any bundle command through run, or even use

require 'bundler'

Bundler.with_clean_env do
  # bundle command
end

I hope this workaround will work for you.

Nicolas

@tanraya

This comment has been minimized.

Copy link
Contributor Author

tanraya commented Jan 14, 2012

Thanks, Nicolas! I confirm that it works properly:

require 'bundler'

class SomegenGenerator < Rails::Generators::Base
 source_root File.expand_path('../templates', __FILE__)

 def install
   gem "unicorn"

   Bundler.with_clean_env do
     run "bundle install"
   end
 end
end

I think I can close this issue now.

@tanraya tanraya closed this Jan 14, 2012

@jarijokinen

This comment has been minimized.

Copy link
Contributor

jarijokinen commented Jan 18, 2012

Doesn't work for me. Also,

Bundler.with_clean_env do
  run 'echo $RUBYOPT'
end

outputs:

-I/home/jari/.rvm/gems/ruby-1.9.3-p0/gems/bundler-1.0.21/lib -rbundler/setup

Running 'echo $PWD' outputs the proper directory, and running 'cat Gemfile' outputs the proper Gemfile.

Anyway, this seems to work...

env_rubyopt = ENV['RUBYOPT']
ENV['RUBYOPT'] = env_rubyopt.sub(" -rbundler/setup", "")
run "bundle install"
ENV['RUBYOPT'] = env_rubyopt

RVM 1.10.2
Ruby 1.9.3
Bundler 1.0.21
Rails 3.2.0.rc2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment