summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiroshi SHIBATA <hsbt@ruby-lang.org>2026-05-01 12:14:25 +0900
committergit <svn-admin@ruby-lang.org>2026-05-07 09:24:52 +0000
commit22fb622b486d17c26b9f9c04be760d31da1d96fd (patch)
treeb5b60872c599e707e17c2db26c58ca09aa270d65
parenta093612797e5436dd4a8b57231c3848cff86e35b (diff)
[ruby/rubygems] Reject duplicate target+field in override DSL
Two override statements that target the same gem and the same field make it ambiguous which operation should win. Raise ArgumentError when the new (target, field) pair already exists in the recorded overrides, leaving the previously recorded entry untouched. https://github.com/ruby/rubygems/commit/1c90030cf9 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
-rw-r--r--lib/bundler/dsl.rb6
-rw-r--r--spec/bundler/bundler/dsl_spec.rb22
2 files changed, 28 insertions, 0 deletions
diff --git a/lib/bundler/dsl.rb b/lib/bundler/dsl.rb
index 2f9d1f9054..7f14c0dca0 100644
--- a/lib/bundler/dsl.rb
+++ b/lib/bundler/dsl.rb
@@ -198,6 +198,7 @@ module Bundler
operations.each do |field, operation|
validate_override_field!(field)
validate_override_operation!(operation)
+ validate_override_uniqueness!(target, field)
end
operations.each do |field, operation|
@@ -290,6 +291,11 @@ module Bundler
end
end
+ def validate_override_uniqueness!(target, field)
+ return unless @overrides.any? {|o| o.target == target && o.field == field }
+ raise ArgumentError, "duplicate override for #{target.inspect} `#{field}:`"
+ end
+
def add_dependency(name, version = nil, options = {})
options["gemfile"] = @gemfile
options["source"] ||= @source
diff --git a/spec/bundler/bundler/dsl_spec.rb b/spec/bundler/bundler/dsl_spec.rb
index 86bcb3be8d..39f745c05e 100644
--- a/spec/bundler/bundler/dsl_spec.rb
+++ b/spec/bundler/bundler/dsl_spec.rb
@@ -447,5 +447,27 @@ RSpec.describe Bundler::Dsl do
end.to raise_error(ArgumentError, /unsupported override field/)
expect(subject.overrides).to eq([])
end
+
+ it "raises ArgumentError when the same target and field are overridden twice" do
+ subject.override("rails", version: ">= 8.0")
+ expect do
+ subject.override("rails", version: :ignore_upper)
+ end.to raise_error(ArgumentError, /duplicate override for "rails" `version:`/)
+ end
+
+ it "keeps the original override when a duplicate is rejected" do
+ subject.override("rails", version: ">= 8.0")
+ expect do
+ subject.override("rails", version: :ignore_upper)
+ end.to raise_error(ArgumentError)
+ expect(subject.overrides.size).to eq(1)
+ expect(subject.overrides.first.operation).to eq(">= 8.0")
+ end
+
+ it "allows different targets with the same field" do
+ subject.override("rails", version: ">= 8.0")
+ subject.override("nokogiri", version: :ignore_upper)
+ expect(subject.overrides.size).to eq(2)
+ end
end
end