Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix Bundler.setup so it can be called more than once safely

  - fix group tracking so that groups are only loaded once, and
    loading the same group twice no longer loads all groups
  - fix Bundler.load so that it memoizes its results. creating
    more than one runtime in a single invokation is very bad
  - spec to make sure the runtime only gets created once

If you called setup(:default) and then require(:default), it would
try to set up every group. That is no longer the case.
  • Loading branch information...
commit 49801115d00316f850a9c9e26ca620b13c41e696 1 parent 75473e7
@indirect indirect authored
View
28 lib/bundler.rb
@@ -66,7 +66,17 @@ def bundle_path
end
def setup(*groups)
- load.setup(*groups)
+ if groups.empty? || @all_groups_loaded
+ # Load all groups, but only once
+ @all_groups_loaded ||= load.setup
+ else
+ # Figure out which groups haven't been loaded yet
+ unloaded = groups - (@completed_groups || [])
+ # Record groups that are now loaded
+ @completed_groups = groups | (@completed_groups || [])
+ # Load any groups that are not yet loaded
+ unloaded.any? ? load.setup(*unloaded) : load
+ end
end
def require(*groups)
@@ -74,17 +84,19 @@ def require(*groups)
end
def load
- if current_env_file?
- SharedHelpers.gem_loaded = true
- Kernel.require env_file
- Bundler
- else
- runtime
+ @load ||= begin
+ if current_env_file?
+ SharedHelpers.gem_loaded = true
+ Kernel.require env_file
+ Bundler
+ else
+ runtime
+ end
end
end
def runtime
- Runtime.new(root, definition)
+ @runtime ||= Runtime.new(root, definition)
end
def definition
View
4 lib/bundler/runtime.rb
@@ -15,9 +15,7 @@ def setup(*groups)
# Has to happen first
clean_load_path
- unloaded = groups - (@loaded_groups || [])
- @loaded_groups = groups | (@loaded_groups || [])
- specs = unloaded.any? ? specs_for(unloaded) : requested_specs
+ specs = groups.any? ? specs_for(groups) : requested_specs
cripple_rubygems(specs)
View
25 spec/install/gems/simple_case_spec.rb
@@ -280,4 +280,29 @@
should_be_installed "rack 1.0.0"
end
end
+
+ describe "when loading only the default group" do
+ it "should not load all groups" do
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
+ gem "rack"
+ gem "activesupport", :group => :development
+ G
+
+ ruby <<-R
+ require "bundler"
+ Bundler.setup :default
+ Bundler.require :default
+ puts RACK
+ begin
+ require "activesupport"
+ rescue LoadError
+ puts "no activesupport"
+ end
+ R
+
+ out.should include("1.0")
+ out.should include("no activesupport")
+ end
+ end
end
View
31 spec/runtime/load_spec.rb
@@ -4,6 +4,11 @@
before :each do
system_gems "rack-1.0.0"
+ # clear memoized method results
+ Bundler.instance_eval do
+ @load = nil
+ @runtime = nil
+ end
end
it "provides a list of the env dependencies" do
@@ -134,9 +139,33 @@
end
end
+ describe "when called twice" do
+ it "doesn't try to load the runtime twice" do
+ system_gems "rack-1.0.0", "activesupport-2.3.5"
+ gemfile <<-G
+ gem "rack"
+ gem "activesupport", :group => :test
+ G
+
+ ruby <<-R
+ require "bundler"
+ Bundler.setup :default
+ Bundler.require :default
+ puts RACK
+ begin
+ require "activesupport"
+ rescue LoadError
+ puts "no activesupport"
+ end
+ R
+
+ out.split("\n").should == ["1.0.0", "no activesupport"]
+ end
+ end
+
describe "not hurting brittle rubygems" do
before :each do
- system_gems ["activerecord-2.3.2", "activesupport-2.3.2"]
+ system_gems "activerecord-2.3.2", "activesupport-2.3.2"
end
it "does not inject #source into the generated YAML of the gem specs" do
Please sign in to comment.
Something went wrong with that request. Please try again.