Skip to content

Commit fa46739

Browse files
committed
Raise an error when building a gem that has a self reference:
- ### Problem A gem that has a self-reference in its dependencies would previously get a warning during `gem build`, saying it's "discouradged". A gem that includes a self-reference can't be updated due to bundler filtering it out. https://github.com/ruby/rubygems/blob/6fd37f4afeb3943cf508d1394fcf4338a1266f2e/bundler/lib/bundler/resolver.rb#L405-L410 I think we should be more strict and prevent the gem from building. ### Solution Raise an explicit error. This codepath is only hit when running `gem build`, so this change won't affect existing consumers of those gems (it was previously possible to install those gems, but not update them, see #9346 for more context).
1 parent 643174d commit fa46739

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

lib/rubygems/specification_policy.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,14 @@ def validate_duplicate_dependencies # :nodoc:
192192
# Checks that the gem does not depend on itself.
193193

194194
def validate_dependencies # :nodoc:
195-
warning_messages = []
195+
error_messages = []
196196
@specification.dependencies.each do |dep|
197-
if dep.name == @specification.name # warn on self reference
198-
warning_messages << "Self referencing dependency is unnecessary and strongly discouraged."
197+
if dep.name == @specification.name # error on self reference
198+
error_messages << "Dependencies of this gem include a self-reference."
199199
end
200200
end
201-
if warning_messages.any?
202-
warning_messages.each {|warning_message| warning warning_message }
203-
end
201+
202+
error error_messages.join if error_messages.any?
204203
end
205204

206205
def validate_required_ruby_version

test/rubygems/test_gem_specification.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,14 +2812,13 @@ def test_validate_self_referencing_dependencies
28122812
Dir.chdir @tempdir do
28132813
@a1.add_dependency @a1.name, "1"
28142814

2815-
use_ui @ui do
2815+
e = assert_raise Gem::InvalidSpecificationException do
28162816
@a1.validate
28172817
end
28182818

2819-
assert_equal <<-EXPECTED, @ui.error
2820-
#{w}: Self referencing dependency is unnecessary and strongly discouraged.
2821-
#{w}: See https://guides.rubygems.org/specification-reference/ for help
2822-
EXPECTED
2819+
expected = "Dependencies of this gem include a self-reference."
2820+
2821+
assert_equal expected, e.message
28232822
end
28242823
end
28252824

0 commit comments

Comments
 (0)