Skip to content

Commit bd4e40a

Browse files
Merge pull request #8631 from rubygems/deivid-rodriguez/let-normalize-remove-invalid-platforms
Let `bundle lock --normalize-platforms` remove invalid platforms (cherry picked from commit 025c0a7)
1 parent fb7262d commit bd4e40a

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

bundler/lib/bundler/definition.rb

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ def start_resolution
755755
end
756756

757757
if @most_specific_non_local_locked_platform
758-
if spec_set_incomplete_for_platform?(result, @most_specific_non_local_locked_platform)
758+
if result.incomplete_for_platform?(current_dependencies, @most_specific_non_local_locked_platform)
759759
@platforms.delete(@most_specific_non_local_locked_platform)
760760
elsif local_platform_needed_for_resolvability
761761
@platforms.delete(local_platform)
@@ -1168,25 +1168,16 @@ def dup_for_full_unlock
11681168
def remove_invalid_platforms!
11691169
return if Bundler.frozen_bundle?
11701170

1171-
@originally_invalid_platforms = platforms.select do |platform|
1172-
next if local_platform == platform ||
1173-
@new_platforms.include?(platform)
1171+
skips = (@new_platforms + [local_platform]).uniq
11741172

1175-
# We should probably avoid removing non-ruby platforms, since that means
1176-
# lockfile will no longer install on those platforms, so a error to give
1177-
# heads up to the user may be better. However, we have tests expecting
1178-
# non ruby platform autoremoval to work, so leaving that in place for
1179-
# now.
1180-
next if @dependency_changes && platform != Gem::Platform::RUBY
1173+
# We should probably avoid removing non-ruby platforms, since that means
1174+
# lockfile will no longer install on those platforms, so a error to give
1175+
# heads up to the user may be better. However, we have tests expecting
1176+
# non ruby platform autoremoval to work, so leaving that in place for
1177+
# now.
1178+
skips |= platforms - [Gem::Platform::RUBY] if @dependency_changes
11811179

1182-
spec_set_incomplete_for_platform?(@originally_locked_specs, platform)
1183-
end
1184-
1185-
@platforms -= @originally_invalid_platforms
1186-
end
1187-
1188-
def spec_set_incomplete_for_platform?(spec_set, platform)
1189-
spec_set.incomplete_for_platform?(current_dependencies, platform)
1180+
@originally_invalid_platforms = @originally_locked_specs.remove_invalid_platforms!(current_dependencies, platforms, skips: skips)
11901181
end
11911182

11921183
def source_map

bundler/lib/bundler/spec_set.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def for(dependencies, platforms_or_legacy_check = [nil], legacy_platforms = [nil
2929
end
3030

3131
def normalize_platforms!(deps, platforms)
32+
remove_invalid_platforms!(deps, platforms)
3233
add_extra_platforms!(platforms)
3334

3435
platforms.map! do |platform|
@@ -53,6 +54,20 @@ def add_originally_invalid_platforms!(platforms, originally_invalid_platforms)
5354
end
5455
end
5556

57+
def remove_invalid_platforms!(deps, platforms, skips: [])
58+
invalid_platforms = []
59+
60+
platforms.reject! do |platform|
61+
next false if skips.include?(platform)
62+
63+
invalid = incomplete_for_platform?(deps, platform)
64+
invalid_platforms << platform if invalid
65+
invalid
66+
end
67+
68+
invalid_platforms
69+
end
70+
5671
def add_extra_platforms!(platforms)
5772
if @specs.empty?
5873
platforms.concat([Gem::Platform::RUBY]).uniq

bundler/spec/commands/lock_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,4 +2619,49 @@
26192619
end
26202620
end
26212621
end
2622+
2623+
describe "--normalize-platforms with gems without generic variant" do
2624+
let(:original_lockfile) do
2625+
<<~L
2626+
GEM
2627+
remote: https://gem.repo4/
2628+
specs:
2629+
sorbet-static (1.0-x86_64-linux)
2630+
2631+
PLATFORMS
2632+
ruby
2633+
x86_64-linux
2634+
2635+
DEPENDENCIES
2636+
sorbet-static
2637+
2638+
BUNDLED WITH
2639+
#{Bundler::VERSION}
2640+
L
2641+
end
2642+
2643+
before do
2644+
build_repo4 do
2645+
build_gem "sorbet-static" do |s|
2646+
s.platform = "x86_64-linux"
2647+
end
2648+
end
2649+
2650+
gemfile <<~G
2651+
source "https://gem.repo4"
2652+
2653+
gem "sorbet-static"
2654+
G
2655+
2656+
lockfile original_lockfile
2657+
end
2658+
2659+
it "removes invalid platforms" do
2660+
simulate_platform "x86_64-linux" do
2661+
bundle "lock --normalize-platforms"
2662+
end
2663+
2664+
expect(lockfile).to eq(original_lockfile.gsub(/^ ruby\n/m, ""))
2665+
end
2666+
end
26222667
end

0 commit comments

Comments
 (0)