Skip to content

Commit b413118

Browse files
Fix bundle lock --add-checksums
Due to a typo in the spec, the issue was not caught initially. If Bundler does not need to re-resolve, `bundle lock` is a noop so Bundler does not add checksums. To fix the issue, we do something similar to what `bundle install` does, just without actually installation. First set the domain (local or remote) according to whether a re-resolve is necessary, and then materialize lazy specifications into real specifications, so that checksums are actually fetched from each source.
1 parent 4ff04d6 commit b413118

3 files changed

Lines changed: 74 additions & 5 deletions

File tree

bundler/lib/bundler/cli/lock.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def run
4040

4141
Bundler.settings.temporary(frozen: false) do
4242
definition = Bundler.definition(update, file)
43-
definition.locked_checksums = true if options["add-checksums"]
43+
definition.add_checksums if options["add-checksums"]
4444

4545
Bundler::CLI::Common.configure_gem_version_promoter(definition, options) if options[:update]
4646

bundler/lib/bundler/definition.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class << self
1313

1414
attr_reader(
1515
:dependencies,
16+
:locked_checksums,
1617
:locked_deps,
1718
:locked_gems,
1819
:platforms,
@@ -22,8 +23,6 @@ class << self
2223
:sources
2324
)
2425

25-
attr_accessor :locked_checksums
26-
2726
# Given a gemfile and lockfile creates a Bundler definition
2827
#
2928
# @param gemfile [Pathname] Path to Gemfile
@@ -182,7 +181,7 @@ def check!
182181
#
183182
# @return [Boolean] Whether fetching remote information will be necessary or not
184183
#
185-
def setup_domain!(options)
184+
def setup_domain!(options = {})
186185
prefer_local! if options[:"prefer-local"]
187186

188187
if options[:local] || (no_resolve_needed? && !missing_specs?)
@@ -541,6 +540,14 @@ def unlocking?
541540

542541
attr_writer :source_requirements
543542

543+
def add_checksums
544+
@locked_checksums = true
545+
546+
setup_domain!
547+
548+
specs # force materialization to real specifications, so that checksums are fetched
549+
end
550+
544551
private
545552

546553
def should_add_extra_platforms?

bundler/spec/commands/lock_spec.rb

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@
17721772
expect(err).not_to include("ERROR REPORT TEMPLATE")
17731773
end
17741774

1775-
it "adds checksums to an existing lockfile" do
1775+
it "adds checksums to an existing lockfile, when re-resolving is necessary" do
17761776
build_repo4 do
17771777
build_gem "nokogiri", "1.14.2"
17781778
build_gem "nokogiri", "1.14.2" do |s|
@@ -1786,6 +1786,8 @@
17861786
gem "nokogiri"
17871787
G
17881788

1789+
# lockfile has a typo (nogokiri) in the dependencies section, so Bundler
1790+
# sees dependencies have changed, and re-resolves
17891791
lockfile <<~L
17901792
GEM
17911793
remote: https://gem.repo4/
@@ -1832,6 +1834,66 @@
18321834
L
18331835
end
18341836

1837+
it "adds checksums to an existing lockfile, when no re-resolve is necessary" do
1838+
build_repo4 do
1839+
build_gem "nokogiri", "1.14.2"
1840+
build_gem "nokogiri", "1.14.2" do |s|
1841+
s.platform = "x86_64-linux"
1842+
end
1843+
end
1844+
1845+
gemfile <<-G
1846+
source "https://gem.repo4"
1847+
1848+
gem "nokogiri"
1849+
G
1850+
1851+
lockfile <<~L
1852+
GEM
1853+
remote: https://gem.repo4/
1854+
specs:
1855+
nokogiri (1.14.2)
1856+
nokogiri (1.14.2-x86_64-linux)
1857+
1858+
PLATFORMS
1859+
ruby
1860+
x86_64-linux
1861+
1862+
DEPENDENCIES
1863+
nokogiri
1864+
1865+
BUNDLED WITH
1866+
#{Bundler::VERSION}
1867+
L
1868+
1869+
simulate_platform "x86_64-linux" do
1870+
bundle "lock --add-checksums"
1871+
end
1872+
1873+
checksums = checksums_section do |c|
1874+
c.checksum gem_repo4, "nokogiri", "1.14.2"
1875+
c.checksum gem_repo4, "nokogiri", "1.14.2", "x86_64-linux"
1876+
end
1877+
1878+
expect(lockfile).to eq <<~L
1879+
GEM
1880+
remote: https://gem.repo4/
1881+
specs:
1882+
nokogiri (1.14.2)
1883+
nokogiri (1.14.2-x86_64-linux)
1884+
1885+
PLATFORMS
1886+
ruby
1887+
x86_64-linux
1888+
1889+
DEPENDENCIES
1890+
nokogiri
1891+
#{checksums}
1892+
BUNDLED WITH
1893+
#{Bundler::VERSION}
1894+
L
1895+
end
1896+
18351897
it "generates checksums by default if configured to do so" do
18361898
build_repo4 do
18371899
build_gem "nokogiri", "1.14.2"

0 commit comments

Comments
 (0)