Skip to content

Commit 2bafb57

Browse files
authored
Merge pull request #9439 from 55728/fix/improve-platform-mismatch-error-message
Improve error message when current platform is not in lockfile
2 parents 9baf3e0 + 02d2179 commit 2bafb57

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

bundler/lib/bundler/resolver.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,27 @@ def raise_not_found!(package)
353353
message << "\n#{other_specs_matching_message(specs, matching_part)}"
354354
end
355355

356+
if specs_matching_requirement.any? && (hint = platform_mismatch_hint)
357+
message << "\n\n#{hint}"
358+
end
359+
356360
raise GemNotFound, message
357361
end
358362

363+
def platform_mismatch_hint
364+
locked_platforms = Bundler.locked_gems&.platforms
365+
return unless locked_platforms
366+
367+
local_platform = Bundler.local_platform
368+
return if locked_platforms.include?(local_platform)
369+
return if locked_platforms.any? {|p| p == Gem::Platform::RUBY }
370+
371+
"Your current platform (#{local_platform}) is not included in the lockfile's platforms (#{locked_platforms.join(", ")}). " \
372+
"Add the current platform to the lockfile with\n`bundle lock --add-platform #{local_platform}` and try again."
373+
rescue GemfileNotFound
374+
nil
375+
end
376+
359377
def filtered_versions_for(package)
360378
@gem_version_promoter.filter_versions(package, @all_versions[package])
361379
end

spec/install/gemfile/specific_platform_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,41 @@
535535
expect(err).to include(error_message).once
536536
end
537537

538+
it "shows a platform mismatch hint when the current platform is not in the lockfile's platforms" do
539+
build_repo4 do
540+
build_gem("sorbet-static", "0.5.6433") {|s| s.platform = "x86_64-linux-musl" }
541+
end
542+
543+
gemfile <<~G
544+
source "https://gem.repo4"
545+
546+
gem "sorbet-static", "0.5.6433"
547+
G
548+
549+
lockfile <<~L
550+
GEM
551+
remote: https://gem.repo4/
552+
specs:
553+
sorbet-static (0.5.6433-x86_64-linux-musl)
554+
555+
PLATFORMS
556+
x86_64-linux-musl
557+
558+
DEPENDENCIES
559+
sorbet-static (= 0.5.6433)
560+
561+
BUNDLED WITH
562+
#{Bundler::VERSION}
563+
L
564+
565+
simulate_platform "x86_64-linux" do
566+
bundle "install", raise_on_error: false
567+
end
568+
569+
expect(err).to include("Your current platform (x86_64-linux) is not included in the lockfile's platforms (x86_64-linux-musl)")
570+
expect(err).to include("bundle lock --add-platform x86_64-linux")
571+
end
572+
538573
it "does not resolve if the current platform does not match any of available platform specific variants for a transitive dependency" do
539574
build_repo4 do
540575
build_gem("sorbet", "0.5.6433") {|s| s.add_dependency "sorbet-static", "= 0.5.6433" }

spec/install/gems/resolving_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@
440440
The source contains the following gems matching 'sorbet-static (= 0.5.10554)':
441441
* sorbet-static-0.5.10554-universal-darwin-21
442442
E
443-
expect(err).to end_with(nice_error)
443+
expect(err).to include(nice_error)
444+
expect(err).to include("Your current platform (aarch64-linux) is not included in the lockfile's platforms (arm64-darwin-21)")
445+
expect(err).to include("bundle lock --add-platform aarch64-linux")
444446
end
445447
end
446448

0 commit comments

Comments
 (0)