Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bundler/lib/bundler/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ def converge_sources
end
end

sources.metadata_source.checksum_store.merge!(@locked_gems.metadata_source.checksum_store) if @locked_gems

changes
end

Expand Down
17 changes: 16 additions & 1 deletion bundler/lib/bundler/lockfile_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def add_checksums
checksums = definition.resolve.map do |spec|
spec.source.checksum_store.to_lock(spec)
end
add_section("CHECKSUMS", checksums)

add_section("CHECKSUMS", checksums + bundler_checksum)
end

def add_locked_ruby_version
Expand Down Expand Up @@ -100,5 +101,19 @@ def add_section(name, value)
raise ArgumentError, "#{value.inspect} can't be serialized in a lockfile"
end
end

def bundler_checksum
return [] if Bundler.gem_version.to_s.end_with?(".dev")

require "rubygems/package"

bundler_spec = definition.sources.metadata_source.specs.search(["bundler", Bundler.gem_version]).last
return [] unless File.exist?(bundler_spec.cache_file)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I unfortunately need to have this File.exist?(bundler_spec.cache_file) condition here due to the way our rspec is setup. At the very beginning of the a test process, we install test dependencies and we use the local copy of bundler for that. We activate the bundler spec in a weird way, but there is no associated bundler.gem tarball, so we can't compute the checksum.

Note that this condition is only needed to install the dependencies of bundler itself. It doesn't affect tests itself (because we build a bundler.gem each time a test starts).


package = Gem::Package.new(bundler_spec.cache_file)
definition.sources.metadata_source.checksum_store.register(bundler_spec, Checksum.from_gem_package(package))

[definition.sources.metadata_source.checksum_store.to_lock(bundler_spec)]
end
end
end
9 changes: 8 additions & 1 deletion bundler/lib/bundler/lockfile_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def to_s

attr_reader(
:sources,
:metadata_source,
:dependencies,
:specs,
:platforms,
Expand Down Expand Up @@ -97,6 +98,7 @@ def self.bundled_with
def initialize(lockfile, strict: false)
@platforms = []
@sources = []
@metadata_source = Source::Metadata.new
@dependencies = {}
@parse_method = nil
@specs = {}
Expand Down Expand Up @@ -252,7 +254,12 @@ def parse_checksum(line)
version = Gem::Version.new(version)
platform = platform ? Gem::Platform.new(platform) : Gem::Platform::RUBY
full_name = Gem::NameTuple.new(name, version, platform).full_name
return unless spec = @specs[full_name]
spec = @specs[full_name]

if name == "bundler"
spec ||= LazySpecification.new(name, version, platform, @metadata_source)
end
return unless spec

if checksums
checksums.split(",") do |lock_checksum|
Expand Down
4 changes: 4 additions & 0 deletions bundler/lib/bundler/source/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def hash
def version_message(spec)
"#{spec.name} #{spec.version}"
end

def checksum_store
@checksum_store ||= Checksum::Store.new
end
end
end
end
9 changes: 5 additions & 4 deletions bundler/spec/commands/lock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,10 @@

bundle("lock --add-checksums", artifice: "endpoint")

checksums = checksums_section_when_enabled do |c|
c.no_checksum "warning", "18.0.0"
end

expect(lockfile).to eq <<~L
GEM
remote: https://gem.repo4/
Expand All @@ -2306,10 +2310,7 @@

DEPENDENCIES
warning

CHECKSUMS
warning (18.0.0)

#{checksums}
BUNDLED WITH
#{Bundler::VERSION}
L
Expand Down
7 changes: 4 additions & 3 deletions bundler/spec/commands/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1419,9 +1419,7 @@
#{lockfile_platforms}

DEPENDENCIES

CHECKSUMS

#{checksums_section_when_enabled}
RUBY VERSION
#{Bundler::RubyVersion.system}

Expand Down Expand Up @@ -1537,6 +1535,7 @@

checksums = checksums_section do |c|
c.checksum(gem_repo4, "myrack", "1.0")
c.checksum(gem_repo4, "bundler", "999.0.0")
end

install_gemfile <<-G
Expand Down Expand Up @@ -1621,6 +1620,7 @@

checksums = checksums_section do |c|
c.checksum(gem_repo4, "myrack", "1.0")
c.checksum(gem_repo4, "bundler", "9.9.9")
end

install_gemfile <<-G
Expand Down Expand Up @@ -1745,6 +1745,7 @@
# Only updates properly on modern RubyGems.
checksums = checksums_section_when_enabled do |c|
c.checksum(gem_repo4, "myrack", "1.0")
c.checksum(local_gem_path, "bundler", "9.0.0", Gem::Platform::RUBY, "cache")
end

expect(lockfile).to eq <<~L
Expand Down
19 changes: 14 additions & 5 deletions bundler/spec/support/checksums.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Spec
module Checksums
class ChecksumsBuilder
attr_reader :bundler_registered

def initialize(enabled = true, &block)
@enabled = enabled
@checksums = {}
Expand All @@ -14,9 +16,11 @@ def initialize_copy(original)
@checksums = @checksums.dup
end

def checksum(repo, name, version, platform = Gem::Platform::RUBY)
def checksum(repo, name, version, platform = Gem::Platform::RUBY, folder = "gems")
@bundler_registered = true if name == "bundler"

name_tuple = Gem::NameTuple.new(name, version, platform)
gem_file = File.join(repo, "gems", "#{name_tuple.full_name}.gem")
gem_file = File.join(repo, folder, "#{name_tuple.full_name}.gem")
File.open(gem_file, "rb") do |f|
register(name_tuple, Bundler::Checksum.from_gem(f, "#{gem_file} (via ChecksumsBuilder#checksum)"))
end
Expand Down Expand Up @@ -50,8 +54,13 @@ def register(name_tuple, checksum)
end
end

def checksums_section(enabled = true, &block)
ChecksumsBuilder.new(enabled, &block)
def checksums_section(enabled = true, bundler_checksum: true, &block)
ChecksumsBuilder.new(enabled, &block).tap do |builder|
next if builder.bundler_registered || !bundler_checksum

next if Bundler::VERSION.to_s.end_with?(".dev")
builder.checksum(system_gem_path, "bundler", Bundler::VERSION, Gem::Platform::RUBY, "cache")
end
end

def checksums_section_when_enabled(target_lockfile = nil, &block)
Expand All @@ -64,7 +73,7 @@ def checksums_section_when_enabled(target_lockfile = nil, &block)
end

def checksum_to_lock(*args)
checksums_section do |c|
checksums_section(true, bundler_checksum: false) do |c|
c.checksum(*args)
end.to_s.sub(/^CHECKSUMS\n/, "").strip
end
Expand Down
Loading