My Environment
Vanilla Ubuntu 12.10, no rvm or renv.
> gem --version
1.8.23
> ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
> bundle --version
Bundler version 1.2.1
My problem
I have a rake task to package my gems and upload them to my development and production servers. The problem is that the rake task fails when the Gemfile includes git or path gems. Bundler already supports packaging of these type of gems and it works great on my terminal but it fails when running withing a rake task and I cannot find out why.
My rake task
> cat lib/tasks/upload.rake
namespace :deploy do
desc "Package all gems and upload to remote server"
task :upload => [:environment] do |t, args|
if ! system("bundle package --all")
raise "TOTAL FAIL"
end
# Magic method to upload vendor/cache to remote server
end
end
My tries
Running bundle package in the terminal works:
> bundle package --all
....
Using bson (1.7.0)
Using bson_ext (1.7.0)
Using cancan (1.6.8) from git://github.com/ryanb/cancan.git (at /home/ryujin/Projects/rails/Encluster4/vendor/cache/cancan-4dcd54459482)
Using carrierwave (0.7.0)
Using coffee-script-source (1.4.0)
....
Updating files in vendor/cache
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Updating files in vendor/cache
Running bundle package withing irb also works:
> irb
irb> system("bundle package --all")
...
Using ansi (1.4.3)
Using bson (1.7.0)
Using bson_ext (1.7.0)
Using cancan (1.6.8) from git://github.com/ryanb/cancan.git (at /home/ryujin/Projects/rails/Encluster4/vendor/cache/cancan-4dcd54459482)
Using carrierwave (0.7.0)
Using coffee-script-source (1.4.0)
...
Updating files in vendor/cache
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Updating files in vendor/cache
=> true
But bundle package does not work when executed within my simple rake task:
> bundle exec rake deploy:upload
Updating files in vendor/cache
Could not find cancan-1.6.8.gem for installation
rake aborted!
TOTAL FAIL
Tasks: TOP => deploy:upload
(See full trace by running task with --trace)
I find not reason why this could fail. I am running in the same environment all the time. I already checked the bundle exec file is the same (/usr/local/bin/bundle) on all three cases. I have no traces or rvm or renv or anything like that. Also tried running the task without bundle exec and the same problem.
Thanks in advance for any tips on why this happens.
I had the same trouble.
I checked the environment variables and find out that the bundler modifies RUBYOPT:
$ bundle exec env > benv.txt
$ env > env.txt
$ diff -u env.txt benv.txt
--- env.txt 2012-12-05 17:13:10.000000000 +0400
+++ benv.txt 2012-12-05 17:13:07.000000000 +0400
## -72,4 +72,8 ##
CDPATH=.
install_flag=1
rvm_hook=
-_=/usr/bin/env
+_=/Users/denis/.rvm/gems/ruby-1.9.3-p327#global/bin/bundle
+_ORIGINAL_GEM_PATH=/Users/denis/.rvm/gems/ruby-1.9.3-p327:/Users/denis/.rvm/gems/ruby-1.9.3-p327#global
+BUNDLE_BIN_PATH=/Users/denis/.rvm/gems/ruby-1.9.3-p327#global/gems/bundler-1.2.3/bin/bundle
+BUNDLE_GEMFILE=/Users/denis/src/my-project/Gemfile
+RUBYOPT=-I/Users/denis/.rvm/gems/ruby-1.9.3-p327#global/gems/bundler-1.2.3/lib -rbundler/setup
So, to workaround I do this:
system %Q(/usr/bin/env RUBYOPT= bundle package)
Hope this helps!
One can also prefer to use
Bundler.with_clean_env do
sh "bundle update --source .."
end
and indeed it is an RUBYOPT problem as mentioned by Denis
Related
Whenever I run a ruby script through bundler it always gives me output similar to:
Fetching gem metadata from https://foo.bar.com/........
...
Using ...
Fetching ...
Installing ...
....
Bundle complete! 19 Gemfile dependencies, 145 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
How can I disable this in bundler so that it just shows the output of the actual script being run?
Easy way is to tell it to keep quiet about what it's doing:
bundle install --quiet
You may want to redirect it to a file in case something happens and it couldn't install gems:
bundle install > /tmp/bundle-install.out
Where you can inspect that if things go awry.
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
I'm trying to run a command using bundle exec in my Ruby project. Whenever I run it, though, I get an error message that looks like:
$ bundle exec rake test
`mri_22` is not a valid platform. The available options are: [:ruby, :ruby_18,
:ruby_19, :ruby_20, :ruby_21, :mri, :mri_18, :mri_19, :mri_20, :mri_21, :rbx,
:jruby, :jruby_18, :jruby_19, :mswin, :mingw, :mingw_18, :mingw_19, :mingw_20,
:mingw_21, :x64_mingw, :x64_mingw_20, :x64_mingw_21]
How do I get bundle to work?
The version of bundler that you're running is out of date. Install the newest bundle gem by running
gem update bundler
Source: https://github.com/codeforamerica/congress/issues/10
We are downloading the latest Cordova documentation and want to generate it with Jenkins.
We are using RVM to manage the Ruby instances.
Here is the relevant shell execution in the Jenkins job.
#!/bin/bash
source "$HOME/.rvm/scripts/rvm"
echo "====== Setting up ruby ========================================"
rvm use "1.7.8"
set -e
echo "====== Installing cordova docs dependencies ==================="
gem install bundler
gem install nokogiri
cd "${WORKSPACE}/submodule_apache_cordova_docs"
bundle install
cd -
echo "====== Generating documents ==================================="
rvm current
ruby -v
${WORKSPACE}/submodule_apache_cordova_docs/bin/generate
and this is the output for that section
[workspace] $ /bin/bash /tmp/hudson1421417307959072745.sh
====== Setting up ruby =====================================
Using /var/lib/jenkins/.rvm/gems/jruby-1.7.8
====== Installing cordova docs dependencies ================
Successfully installed bundler-1.7.2
1 gem installed
Successfully installed nokogiri-1.6.3.1-java
1 gem installed
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Using json 1.8.1
Using nokogiri 1.5.9
Using rspec 1.3.0
Using bundler 1.7.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
/var/lib/jenkins/jobs/Build-Documentation/workspace
====== Generating documents ================================
jruby-1.7.8
jruby 1.7.8 (1.9.3p392) 2013-11-14 0ce429e on OpenJDK 64-Bit Server VM 1.7.0_65-b32 [linux-amd64]
SyntaxError: /var/lib/jenkins/jobs/Build-Documentation/workspace/submodule_apache_cordova_docs/bin/../lib/cordova/jodoc.rb:28: syntax error, unexpected tCONSTANT
#template_directories = [ File.join TEMPLATE_PATH, 'default' ]
^
require at org/jruby/RubyKernel.java:1084
require at /var/lib/jenkins/.rvm/rubies/jruby-1.7.8/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:55
require at /var/lib/jenkins/.rvm/rubies/jruby-1.7.8/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:53
(root) at /var/lib/jenkins/jobs/Build-Documentation/workspace/submodule_apache_cordova_docs/bin/../lib/docs_generator.rb:1
require at org/jruby/RubyKernel.java:1084
(root) at /var/lib/jenkins/jobs/Build-Documentation/workspace/submodule_apache_cordova_docs/bin/../lib/docs_generator.rb:26
(root) at /var/lib/jenkins/jobs/Build-Documentation/workspace/submodule_apache_cordova_docs/bin/generate:22
Build step 'Execute shell' marked build as failure
Finished: FAILURE
As far as I can see all the dependency versions are correct, including Ruby.
This is the file that errors: https://github.com/apache/cordova-docs/blob/master/lib/cordova/jodoc.rb
What is causing the error?
Thanks
I'm building a simple thor based generator for some internal projects, and can't seem to get bundle install running from the correct directory.
As I run the new [APP_NAME] function, it should create the directories and files, then run bundle install to install the gems required for the application.
The source of the generator function:
def create
puts "Creating application #{name}"
directory 'application', "#{name}"
Dir.chdir("#{Dir.pwd}/#{name}") do
puts `bundle install`
end
end
And the console output from running the command that calls this create method:
$ bundle exec bin/my_gem new test_app
Creating application test_app
create test_app
create test_app/Gemfile
create test_app/Guardfile
create test_app/README.md
create test_app/app/controllers
create test_app/app/helpers
create test_app/app/models
create test_app/app/views
Using thor (0.14.6)
Using my_gem (0.0.1)
Using bundler (1.1.3)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
As you can see, it is running bundle install but it's running it in my current directory (thor, bundler, my_gem), as opposed to the test_app directory (guard, guard-coffeescript, guard-less, and others) .
Running other commands such as ls or pwd give the expected results:
Gemfile
Guardfile
README.md
app
and
/Users/davidlumley/Development/Gems/my_gem/test_app
Not sure if it makes any difference, but I use RVM for managing my rubies.
Sounds like your app is already using bundler and you have a bundler-inside-bundler problem. Try this:
Bundler.with_clean_env do
puts `bundle install`
end
I'm guessing what's happening is that your outer bundler sets the BUNDLE_GEMFILE env variable to your app's Gemfile, and then your inner bundler ends up inheriting it.
Please try this i will sure helps you.
rvm gemset create app_name
rvm use ruby-1.9.2#app_name
Ruby version what else you used.
then install bundler gem
gem install bundler.
Then bundle install and run server..