diff options
| author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2026-05-07 18:38:46 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2026-05-08 06:43:37 +0000 |
| commit | c0a9fdc030b92930cef3ed398ad274a12e44ef97 (patch) | |
| tree | f21ec4c6c674d93ec2a495f2f554b6a8b626aedd | |
| parent | 29229d82bec6256e6b4f93486d772014cead9d8c (diff) | |
[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) <noreply@anthropic.com>
| -rw-r--r-- | lib/bundler/dsl.rb | 6 | ||||
| -rw-r--r-- | spec/bundler/bundler/dsl_spec.rb | 13 |
2 files changed, 18 insertions, 1 deletions
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) |
