From 29d3ea1e84d44335d998cadaf7cf3b45270a962e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 13 Dec 2024 19:17:40 +0100 Subject: [PATCH] [rubygems/rubygems] Fix `bundle lock --add-checksums` when gems are already installed https://github.com/rubygems/rubygems/commit/a087c452ad --- lib/bundler/definition.rb | 54 +++++++++++++++----------- spec/bundler/commands/lock_spec.rb | 62 ++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 23 deletions(-) diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index 76397c4045..85951f9314 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -186,13 +186,13 @@ module Bundler def setup_domain!(options = {}) prefer_local! if options[:"prefer-local"] - if options[:local] || no_install_needed? - Bundler.settings.set_command_option(:jobs, 1) if no_install_needed? # to avoid the overhead of Bundler::Worker - with_cache! - false - else + if options[:add_checksums] || (!options[:local] && install_needed?) remotely! true + else + Bundler.settings.set_command_option(:jobs, 1) unless install_needed? # to avoid the overhead of Bundler::Worker + with_cache! + false end end @@ -513,26 +513,11 @@ module Bundler end def nothing_changed? - return false unless lockfile_exists? - - !@source_changes && - !@dependency_changes && - !@current_platform_missing && - @new_platforms.empty? && - !@path_changes && - !@local_changes && - !@missing_lockfile_dep && - !@unlocking_bundler && - !@locked_spec_with_missing_deps && - !@locked_spec_with_invalid_deps - end - - def no_install_needed? - no_resolve_needed? && !missing_specs? + !something_changed? end def no_resolve_needed? - !unlocking? && nothing_changed? + !resolve_needed? end def unlocking? @@ -544,13 +529,36 @@ module Bundler def add_checksums @locked_checksums = true - setup_domain! + setup_domain!(add_checksums: true) specs # force materialization to real specifications, so that checksums are fetched end private + def install_needed? + resolve_needed? || missing_specs? + end + + def something_changed? + return true unless lockfile_exists? + + @source_changes || + @dependency_changes || + @current_platform_missing || + @new_platforms.any? || + @path_changes || + @local_changes || + @missing_lockfile_dep || + @unlocking_bundler || + @locked_spec_with_missing_deps || + @locked_spec_with_invalid_deps + end + + def resolve_needed? + unlocking? || something_changed? + end + def should_add_extra_platforms? !lockfile_exists? && generic_local_platform_is_ruby? && !Bundler.settings[:force_ruby_platform] end diff --git a/spec/bundler/commands/lock_spec.rb b/spec/bundler/commands/lock_spec.rb index 7044cd3175..db600e356f 100644 --- a/spec/bundler/commands/lock_spec.rb +++ b/spec/bundler/commands/lock_spec.rb @@ -1894,6 +1894,68 @@ RSpec.describe "bundle lock" do L end + it "adds checksums to an existing lockfile, when gems are already installed" do + build_repo4 do + build_gem "nokogiri", "1.14.2" + build_gem "nokogiri", "1.14.2" do |s| + s.platform = "x86_64-linux" + end + end + + gemfile <<-G + source "https://gem.repo4" + + gem "nokogiri" + G + + lockfile <<~L + GEM + remote: https://gem.repo4/ + specs: + nokogiri (1.14.2) + nokogiri (1.14.2-x86_64-linux) + + PLATFORMS + ruby + x86_64-linux + + DEPENDENCIES + nokogiri + + BUNDLED WITH + #{Bundler::VERSION} + L + + simulate_platform "x86_64-linux" do + bundle "install" + + bundle "lock --add-checksums" + end + + checksums = checksums_section do |c| + c.checksum gem_repo4, "nokogiri", "1.14.2" + c.checksum gem_repo4, "nokogiri", "1.14.2", "x86_64-linux" + end + + expect(lockfile).to eq <<~L + GEM + remote: https://gem.repo4/ + specs: + nokogiri (1.14.2) + nokogiri (1.14.2-x86_64-linux) + + PLATFORMS + ruby + x86_64-linux + + DEPENDENCIES + nokogiri + #{checksums} + BUNDLED WITH + #{Bundler::VERSION} + L + end + it "generates checksums by default if configured to do so" do build_repo4 do build_gem "nokogiri", "1.14.2"