diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index cb94063b05..192e2e2d0c 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -81,7 +81,7 @@ module Bundler @resolved_bundler_version = nil @locked_ruby_version = nil - @new_platform = nil + @new_platforms = [] @removed_platform = nil if lockfile_exists? @@ -457,8 +457,10 @@ module Bundler end def add_platform(platform) - @new_platform ||= !@platforms.include?(platform) - @platforms |= [platform] + return if @platforms.include?(platform) + + @new_platforms << platform + @platforms << platform end def remove_platform(platform) @@ -482,7 +484,7 @@ module Bundler !@source_changes && !@dependency_changes && - !@new_platform && + @new_platforms.empty? && !@path_changes && !@local_changes && !@missing_lockfile_dep && @@ -703,7 +705,7 @@ module Bundler [ [@source_changes, "the list of sources changed"], [@dependency_changes, "the dependencies in your gemfile changed"], - [@new_platform, "you added a new platform to your gemfile"], + [@new_platforms.any?, "you added a new platform to your gemfile"], [@path_changes, "the gemspecs for path gems changed"], [@local_changes, "the gemspecs for git local gems changed"], [@missing_lockfile_dep, "your lock file is missing \"#{@missing_lockfile_dep}\""], @@ -1061,7 +1063,7 @@ module Bundler platforms.reverse_each do |platform| next if local_platform == platform || - (@new_platform && platforms.last == platform) || + @new_platforms.include?(platform) || @path_changes || @dependency_changes || @locked_spec_with_invalid_deps || diff --git a/spec/bundler/commands/lock_spec.rb b/spec/bundler/commands/lock_spec.rb index 989f4fc154..450436372e 100644 --- a/spec/bundler/commands/lock_spec.rb +++ b/spec/bundler/commands/lock_spec.rb @@ -629,6 +629,62 @@ RSpec.describe "bundle lock" do expect(lockfile.platforms).to match_array(default_platform_list(java, x86_mingw32)) end + it "supports adding new platforms, when most specific locked platform is not the current platform, and current resolve is not compatible with the target platform" do + simulate_platform "arm64-darwin-23" do + build_repo4 do + build_gem "foo" do |s| + s.platform = "arm64-darwin" + end + + build_gem "foo" do |s| + s.platform = "java" + end + end + + gemfile <<-G + source "https://gem.repo4" + + gem "foo" + G + + lockfile <<-L + GEM + remote: https://gem.repo4/ + specs: + foo (1.0-arm64-darwin) + + PLATFORMS + arm64-darwin + + DEPENDENCIES + foo + + BUNDLED WITH + #{Bundler::VERSION} + L + + bundle "lock --add-platform java" + + expect(lockfile).to eq <<~L + GEM + remote: https://gem.repo4/ + specs: + foo (1.0-arm64-darwin) + foo (1.0-java) + + PLATFORMS + arm64-darwin + java + + DEPENDENCIES + foo + + BUNDLED WITH + #{Bundler::VERSION} + L + end + end + it "supports adding new platforms with force_ruby_platform = true" do lockfile <<-L GEM