From c0a9fdc030b92930cef3ed398ad274a12e44ef97 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Thu, 7 May 2026 18:38:46 +0900 Subject: [ruby/rubygems] Validate override version requirement at Gemfile evaluation time Before this change, `override "rails", version: "not a version"` was accepted by Bundler::Dsl#override and only failed later when the resolver tried to instantiate Gem::Requirement. Surface the error at the Gemfile evaluation step so the user sees it on the offending line. https://github.com/ruby/rubygems/commit/2ae83fbb4f Co-Authored-By: Claude Opus 4.7 (1M context) --- lib/bundler/dsl.rb | 6 +++++- spec/bundler/bundler/dsl_spec.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/bundler/dsl.rb b/lib/bundler/dsl.rb index c7a7d855ee..05eb493677 100644 --- a/lib/bundler/dsl.rb +++ b/lib/bundler/dsl.rb @@ -279,7 +279,9 @@ module Bundler def validate_override_operation!(operation) case operation - when String, nil + when String + Gem::Requirement.new(operation) + when nil # ok when Symbol return if SUPPORTED_OVERRIDE_SYMBOL_OPERATIONS.include?(operation) @@ -287,6 +289,8 @@ module Bundler else raise ArgumentError, "override operation must be a String, Symbol, or nil, got #{operation.inspect}" end + rescue Gem::Requirement::BadRequirementError => e + raise ArgumentError, "invalid override version requirement #{operation.inspect}: #{e.message}" end def validate_override_uniqueness!(target, field) diff --git a/spec/bundler/bundler/dsl_spec.rb b/spec/bundler/bundler/dsl_spec.rb index 39f745c05e..99ab9088f9 100644 --- a/spec/bundler/bundler/dsl_spec.rb +++ b/spec/bundler/bundler/dsl_spec.rb @@ -441,6 +441,19 @@ RSpec.describe Bundler::Dsl do end.to raise_error(ArgumentError, /unsupported override operation/) end + it "raises ArgumentError for an unparsable version string" do + expect do + subject.override("rails", version: "not a version") + end.to raise_error(ArgumentError, /invalid override version requirement/) + end + + it "does not record an override when the version string is invalid" do + expect do + subject.override("rails", version: "not a version") + end.to raise_error(ArgumentError) + expect(subject.overrides).to eq([]) + end + it "rejects atomically when one field in a multi-field call is invalid" do expect do subject.override("rails", version: ">= 8.0", required_ruby_version: :ignore_upper) -- cgit v1.2.3