Skip to content

Commit 58663e8

Browse files
deivid-rodriguezhsbt
authored andcommitted
[rubygems/rubygems] Change behavior when gemfile and gemspec dep deps conflict
ruby/rubygems@7026b5f2e5
1 parent 504b4bd commit 58663e8

File tree

2 files changed

+41
-53
lines changed

2 files changed

+41
-53
lines changed

lib/bundler/dsl.rb

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,21 @@ def add_dependency(name, version = nil, options = {})
247247

248248
gemspec_dep = [dep, current].find(&:gemspec_dev_dep?)
249249
if gemspec_dep
250-
gemfile_dep = [dep, current].find(&:gemfile_dep?)
251-
252-
if gemfile_dep && !current_requirement_open
253-
Bundler.ui.warn "A gemspec development dependency (#{name}, #{gemspec_dep.requirement}) is being overridden by a Gemfile dependency (#{name}, #{gemfile_dep.requirement}).\n" \
254-
"This behaviour may change in the future. Please remove either of them, or make sure they both have the same requirement\n"
255-
elsif gemfile_dep.nil?
256-
require_relative "vendor/pub_grub/lib/pub_grub/version_range"
257-
require_relative "vendor/pub_grub/lib/pub_grub/version_constraint"
258-
require_relative "vendor/pub_grub/lib/pub_grub/version_union"
259-
require_relative "vendor/pub_grub/lib/pub_grub/rubygems"
260-
261-
current_gemspec_range = PubGrub::RubyGems.requirement_to_range(current.requirement)
262-
next_gemspec_range = PubGrub::RubyGems.requirement_to_range(dep.requirement)
263-
264-
if current_gemspec_range.intersects?(next_gemspec_range)
265-
dep = Dependency.new(name, current.requirement.as_list + dep.requirement.as_list, options)
250+
require_relative "vendor/pub_grub/lib/pub_grub/version_range"
251+
require_relative "vendor/pub_grub/lib/pub_grub/version_constraint"
252+
require_relative "vendor/pub_grub/lib/pub_grub/version_union"
253+
require_relative "vendor/pub_grub/lib/pub_grub/rubygems"
254+
255+
current_gemspec_range = PubGrub::RubyGems.requirement_to_range(current.requirement)
256+
next_gemspec_range = PubGrub::RubyGems.requirement_to_range(dep.requirement)
257+
258+
if current_gemspec_range.intersects?(next_gemspec_range)
259+
dep = Dependency.new(name, current.requirement.as_list + dep.requirement.as_list, options)
260+
else
261+
gemfile_dep = [dep, current].find(&:gemfile_dep?)
262+
263+
if gemfile_dep
264+
raise GemfileError, "The #{name} dependency has conflicting requirements in Gemfile (#{gemfile_dep.requirement}) and gemspec (#{gemspec_dep.requirement})"
266265
else
267266
raise GemfileError, "Two gemspec development dependencies have conflicting requirements on the same gem: #{dep} and #{current}"
268267
end

spec/bundler/commands/install_spec.rb

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@
474474
expect(the_bundle).to include_gems("my-private-gem 1.0")
475475
end
476476

477-
it "throws a warning if a gem is added once in Gemfile and also inside a gemspec as a development dependency, with different requirements" do
477+
it "does not warn if a gem is added once in Gemfile and also inside a gemspec as a development dependency, with compatible requirements" do
478478
build_lib "my-gem", path: bundled_app do |s|
479479
s.add_development_dependency "rubocop", "~> 1.36.0"
480480
end
@@ -494,14 +494,32 @@
494494

495495
bundle :install
496496

497-
expect(err).to include("A gemspec development dependency (rubocop, ~> 1.36.0) is being overridden by a Gemfile dependency (rubocop, >= 0).")
498-
expect(err).to include("This behaviour may change in the future. Please remove either of them, or make sure they both have the same requirement")
497+
expect(err).to be_empty
498+
499+
expect(the_bundle).to include_gems("rubocop 1.36.0")
500+
end
501+
502+
it "raises an error if a gem is added once in Gemfile and also inside a gemspec as a development dependency, with incompatible requirements" do
503+
build_lib "my-gem", path: bundled_app do |s|
504+
s.add_development_dependency "rubocop", "~> 1.36.0"
505+
end
506+
507+
build_repo4 do
508+
build_gem "rubocop", "1.36.0"
509+
build_gem "rubocop", "1.37.1"
510+
end
511+
512+
gemfile <<~G
513+
source "https://gem.repo4"
514+
515+
gemspec
516+
517+
gem "rubocop", "~> 1.37.0", group: :development
518+
G
499519

500-
# This is not the best behavior I believe, it would be better if both
501-
# requirements are considered if they are compatible, and a version
502-
# satisfying both is chosen. But not sure about changing it right now, so
503-
# I went with a warning for the time being.
504-
expect(the_bundle).to include_gems("rubocop 1.37.1")
520+
bundle :install, raise_on_error: false
521+
522+
expect(err).to include("The rubocop dependency has conflicting requirements in Gemfile (~> 1.37.0) and gemspec (~> 1.36.0)")
505523
end
506524

507525
it "includes the gem without warning if two gemspecs add it with the same requirement" do
@@ -590,35 +608,6 @@
590608
expect(err).to include("Two gemspec development dependencies have conflicting requirements on the same gem: rubocop (~> 1.36.0) and rubocop (~> 2.0). Bundler cannot continue.")
591609
end
592610

593-
it "warns when a Gemfile dependency is overriding a gemspec development dependency, with different requirements" do
594-
build_lib "my-gem", path: bundled_app do |s|
595-
s.add_development_dependency "rails", ">= 5"
596-
end
597-
598-
build_repo4 do
599-
build_gem "rails", "7.0.8"
600-
end
601-
602-
gemfile <<~G
603-
source "https://gem.repo4"
604-
605-
gem "rails", "~> 7.0.8"
606-
607-
gemspec
608-
G
609-
610-
bundle :install
611-
612-
expect(err).to include("A gemspec development dependency (rails, >= 5) is being overridden by a Gemfile dependency (rails, ~> 7.0.8).")
613-
expect(err).to include("This behaviour may change in the future. Please remove either of them, or make sure they both have the same requirement")
614-
615-
# This is not the best behavior I believe, it would be better if both
616-
# requirements are considered if they are compatible, and a version
617-
# satisfying both is chosen. But not sure about changing it right now, so
618-
# I went with a warning for the time being.
619-
expect(the_bundle).to include_gems("rails 7.0.8")
620-
end
621-
622611
it "does not warn if a gem is added once in Gemfile and also inside a gemspec as a development dependency, with same requirements, and different sources" do
623612
build_lib "my-gem", path: bundled_app do |s|
624613
s.add_development_dependency "activesupport"

0 commit comments

Comments
 (0)