Skip to content

Commit 485a603

Browse files
khasinskik0kubun
authored andcommitted
Fix bundled gems warning for all subfeatures of hyphenated gems
PR ruby#15822 fixed the warning for direct hyphenated gem requires like `benchmark/ips` → `benchmark-ips`. However, hyphenated gems often provide multiple files under their namespace. For example, `benchmark-ips` provides: - benchmark/ips.rb - benchmark/timing.rb - benchmark/compare.rb When requiring `benchmark/timing`, the previous fix only checked for `benchmark-timing` gem (doesn't exist), not `benchmark-ips` which actually provides the file. This fix checks if ANY gem matching `{prefix}-*` is in the bundle specs, which covers all subfeatures provided by hyphenated gems. Reported in ruby#15822 (comment)
1 parent f3bfffe commit 485a603

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

lib/bundled_gems.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,10 @@ def self.warning?(name, specs: nil)
129129
return if specs.include?(name)
130130

131131
# Don't warn if a hyphenated gem provides this feature
132-
# (e.g., benchmark-ips provides benchmark/ips, not the benchmark gem)
132+
# (e.g., benchmark-ips provides benchmark/ips, benchmark/timing, etc.)
133133
if subfeature
134-
feature_parts = feature.split("/")
135-
if feature_parts.size >= 2
136-
hyphenated_gem = "#{feature_parts[0]}-#{feature_parts[1]}"
137-
return if specs.include?(hyphenated_gem)
138-
end
134+
prefix = feature.split("/").first + "-"
135+
return if specs.any? { |spec, _| spec.start_with?(prefix) }
139136
end
140137

141138
return if WARNED[name]

test/test_bundled_gems.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ def test_no_warning_for_hyphenated_gem
3939
assert_nil Gem::BUNDLED_GEMS.warning?("benchmark/ips", specs: {"benchmark-ips" => true})
4040
end
4141

42+
def test_no_warning_for_subfeatures_of_hyphenated_gem
43+
# When benchmark-ips gem is in specs, requiring any "benchmark/*" subfeature
44+
# should not warn, since hyphenated gems may provide multiple files
45+
# (e.g., benchmark-ips provides benchmark/ips, benchmark/timing, benchmark/compare)
46+
assert_nil Gem::BUNDLED_GEMS.warning?("benchmark/timing", specs: {"benchmark-ips" => true})
47+
assert_nil Gem::BUNDLED_GEMS.warning?("benchmark/compare", specs: {"benchmark-ips" => true})
48+
end
49+
4250
def test_warning_without_hyphenated_gem
4351
# When benchmark-ips is NOT in specs, requiring "benchmark/ips" should warn
4452
warning = Gem::BUNDLED_GEMS.warning?("benchmark/ips", specs: {})

0 commit comments

Comments
 (0)