Skip to content

Instantly share code, notes, and snippets.

@indirect
Created November 16, 2010 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indirect/b8d47d4ca590e7b00539 to your computer and use it in GitHub Desktop.
Save indirect/b8d47d4ca590e7b00539 to your computer and use it in GitHub Desktop.
[andre ~/sw/gems/bundler-testcases/i850]$ cat Gemfile
gem "rack"
[andre ~/sw/gems/bundler-testcases/i850]$ dbundle install
Using rack (1.2.1)
Using bundler (1.0.5)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
[andre ~/sw/gems/bundler-testcases/i850]$ cat test.rb
require 'rubygems'
require 'bundler/setup'
puts "With env: #{ENV['BUNDLE_GEMFILE']}"
Bundler.with_clean_env do
puts "With clean: #{ENV['BUNDLE_GEMFILE']}"
end
[andre ~/sw/gems/bundler-testcases/i850]$ echo $BUNDLE_GEMFILE
[andre ~/sw/gems/bundler-testcases/i850]$ ruby test.rb
With env: /Users/andre/sw/gems/bundler-testcases/i850/Gemfile
With clean:
@ahamid
Copy link

ahamid commented Feb 14, 2011

Is there a way to make nested loading of code with bundle-configured dependencies work?

[aaron@msi-cr620 bundle-plugins]$ cat Gemfile
source "http://rubygems.org"

gem "veritas"

[aaron@msi-cr620 bundle-plugins]$ cat app.rb
require "rubygems"
require "bundler"
Bundler.setup(:default)
require 'veritas'

puts "With env: #{ENV['BUNDLE_GEMFILE']}"

Dir[File.expand_path(File.dirname(__FILE__) + '/plugins/*.rb')].each do |f|
  puts "Changing to dir: #{File.dirname(f)}"
  Dir.chdir File.dirname(f) do
    Bundler.with_clean_env do
      puts "With clean: #{ENV['BUNDLE_GEMFILE']}"
      puts "Requiring #{f}"
      require f
    end
  end
end

[aaron@msi-cr620 bundle-plugins]$ cat plugins/Gemfile
source "http://rubygems.org"

group :sample_plugin do
  gem "json_pure"
end

[aaron@msi-cr620 bundle-plugins]$ cat plugins/sample.rb
require "rubygems"
require "bundler"

Bundler.setup(:sample_plugin)
puts "In plugin env: #{ENV['BUNDLE_GEMFILE']}"

require 'json/pure'
puts JSON::Parser

[aaron@msi-cr620 bundle-plugins]$ ls plugins/vendor/bundle/ruby/1.8/gems/
json_pure-1.5.1

[aaron@msi-cr620 bundle-plugins]$ ruby app.rb
With env: /home/aaron/workspace/bundle-plugins/Gemfile
Changing to dir: /home/aaron/workspace/bundle-plugins/plugins
With clean:
Requiring /home/aaron/workspace/bundle-plugin/plugins/sample.rb
In plugin env: /home/aaron/workspace/bundle-plugins/plugins/Gemfile
/home/aaron/workspace/bundle-plugins/plugins/sample.rb:6:in `require': no such file to load -- json/pure (LoadError)

[aaron@msi-cr620 bundle-plugins]$ cd plugins/; ruby ../app.rb
../app.rb:4:in `require': no such file to load -- veritas (LoadError)

@indirect
Copy link
Author

Sorry, I don't understand your question. What do you want to be able to do?

@ahamid
Copy link

ahamid commented Feb 14, 2011

From an application, load code which may itself have distinct dependencies described in separate Gemfiles. Plugins are a canonical example - it would be nice to be able to distribute plugins with 'bundle install --deployment' dependencies which can be loaded from the parent application without requiring all possible plugin dependencies to be added to the app Gemfile a priori.

@indirect
Copy link
Author

No, unfortunately that's not possible. The ruby interpreter is only able to load a single set of libraries, which is why Bundler has to resolve the entire dependency graph before the interpreter even starts.

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