Skip to content

Commit 9e620c6

Browse files
Merge pull request #7319 from rubygems/fix-more-dev-deps-warnings
Fix incorrect error when Gemfile overrides a gemspec development dependency (cherry picked from commit ae670d2)
1 parent 0035192 commit 9e620c6

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

bundler/lib/bundler/dependency.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def should_include?
6868
@should_include && current_env? && current_platform?
6969
end
7070

71+
def gemspec_dev_dep?
72+
type == :development
73+
end
74+
7175
def current_env?
7276
return true unless @env
7377
if @env.is_a?(Hash)

bundler/lib/bundler/dsl.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,21 @@ def gem(name, *args)
103103
# if there's already a dependency with this name we try to prefer one
104104
if current = @dependencies.find {|d| d.name == dep.name }
105105
# Always prefer the dependency from the Gemfile
106-
deleted_dep = @dependencies.delete(current) if current.type == :development
106+
@dependencies.delete(current) if current.gemspec_dev_dep?
107107

108108
if current.requirement != dep.requirement
109109
current_requirement_open = current.requirements_list.include?(">= 0")
110110

111-
if current.type == :development
112-
unless current_requirement_open || dep.type == :development
113-
Bundler.ui.warn "A gemspec development dependency (#{dep.name}, #{current.requirement}) is being overridden by a Gemfile dependency (#{dep.name}, #{dep.requirement}).\n" \
114-
"This behaviour may change in the future. Please remove either of them, or make sure they both have the same requirement\n" \
111+
gemspec_dep = [dep, current].find(&:gemspec_dev_dep?)
112+
if gemspec_dep
113+
gemfile_dep = [dep, current].find(&:runtime?)
114+
115+
unless current_requirement_open
116+
Bundler.ui.warn "A gemspec development dependency (#{gemspec_dep.name}, #{gemspec_dep.requirement}) is being overridden by a Gemfile dependency (#{gemfile_dep.name}, #{gemfile_dep.requirement}).\n" \
117+
"This behaviour may change in the future. Please remove either of them, or make sure they both have the same requirement\n"
115118
end
119+
120+
return if dep.gemspec_dev_dep?
116121
else
117122
update_prompt = ""
118123

@@ -130,8 +135,8 @@ def gem(name, *args)
130135
"You specified: #{current.name} (#{current.requirement}) and #{dep.name} (#{dep.requirement})" \
131136
"#{update_prompt}"
132137
end
133-
elsif current.type == :development || dep.type == :development
134-
return if deleted_dep.nil?
138+
elsif current.gemspec_dev_dep? || dep.gemspec_dev_dep?
139+
return if dep.gemspec_dev_dep?
135140
elsif current.source != dep.source
136141
raise GemfileError, "You cannot specify the same gem twice coming from different sources.\n" \
137142
"You specified that #{dep.name} (#{dep.requirement}) should come from " \

bundler/spec/commands/install_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,35 @@
460460
expect(the_bundle).to include_gems("rubocop 1.37.1")
461461
end
462462

463+
it "warns when a Gemfile dependency is overriding a gemspec development dependency, with different requirements" do
464+
build_lib "my-gem", path: bundled_app do |s|
465+
s.add_development_dependency "rails", ">= 5"
466+
end
467+
468+
build_repo4 do
469+
build_gem "rails", "7.0.8"
470+
end
471+
472+
gemfile <<~G
473+
source "#{file_uri_for(gem_repo4)}"
474+
475+
gem "rails", "~> 7.0.8"
476+
477+
gemspec
478+
G
479+
480+
bundle :install
481+
482+
expect(err).to include("A gemspec development dependency (rails, >= 5) is being overridden by a Gemfile dependency (rails, ~> 7.0.8).")
483+
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")
484+
485+
# This is not the best behavior I believe, it would be better if both
486+
# requirements are considered if they are compatible, and a version
487+
# satisfying both is chosen. But not sure about changing it right now, so
488+
# I went with a warning for the time being.
489+
expect(the_bundle).to include_gems("rails 7.0.8")
490+
end
491+
463492
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
464493
build_lib "my-gem", path: bundled_app do |s|
465494
s.add_development_dependency "activesupport"

0 commit comments

Comments
 (0)