Skip to content

Commit

Permalink
Memoize materialized specs when requiring bundler/setup
Browse files Browse the repository at this point in the history
Calling `Bundler.definition.specs` will memoize materialized specs.
However, requiring `bundler/setup` will end up materializing the same
set of specs, but not memoize them.

This change makes things consistent.
  • Loading branch information
deivid-rodriguez committed Oct 29, 2021
1 parent 1b62207 commit e4c2b52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 5 additions & 4 deletions bundler/lib/bundler/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def initialize(lockfile, dependencies, sources, unlock, ruby_version = nil, opti
@dependency_changes = converge_dependencies
@local_changes = converge_locals

@locked_specs_incomplete_for_platform = !@locked_specs.for(expand_dependencies(requested_dependencies & locked_dependencies), true, true)
@locked_specs_incomplete_for_platform = !@locked_specs.for(requested_dependencies & expand_dependencies(locked_dependencies), true, true)

@requires = compute_requires
end
Expand Down Expand Up @@ -239,16 +239,17 @@ def locked_dependencies
end

def specs_for(groups)
groups = requested_groups if groups.empty?
return specs if groups.empty?
deps = dependencies_for(groups)
materialize(expand_dependencies(deps))
materialize(deps)
end

def dependencies_for(groups)
groups.map!(&:to_sym)
current_dependencies.reject do |d|
deps = current_dependencies.reject do |d|
(d.groups & groups).empty?
end
expand_dependencies(deps)
end

# Resolve all the dependencies specified in Gemfile. It ensures that
Expand Down
16 changes: 16 additions & 0 deletions bundler/spec/runtime/setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1468,5 +1468,21 @@ def lock_with(ruby_version = nil)

expect(last_command.stdboth).to eq("true")
end

it "memoizes initial set of specs when requiring bundler/setup, so that even if further code mutates dependencies, Bundler.definition.specs is not affected" do
install_gemfile <<~G
source "#{file_uri_for(gem_repo1)}"
gem "yard"
gem "rack", :group => :test
G

ruby <<-RUBY, :raise_on_error => false
require "bundler/setup"
Bundler.require(:test).select! {|d| (d.groups & [:test]).any? }
puts Bundler.definition.specs.map(&:name).join(", ")
RUBY

expect(out).to include("rack, yard")
end
end
end

0 comments on commit e4c2b52

Please sign in to comment.