summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhituzi no sippo <dev@hituzi-no-sippo.me>2025-12-13 15:53:10 +0900
committergit <svn-admin@ruby-lang.org>2025-12-16 03:56:22 +0000
commitf3b9509b52bbf845d95bf799d76dad41783919e5 (patch)
tree671885946a96bf5eb95754d418f48a8d43a391d0
parent3b50f4ba41ececd01dcf2e35c4071495f250d609 (diff)
[ruby/rubygems] Fix quote handling in mise format ruby version parsing
The previous regex didn't properly match quoted strings it would capture the opening quote as part of the version if quotes were mismatched. This change properly parses double-quoted, single-quoted, and unquoted version strings separately. https://github.com/ruby/rubygems/commit/81e48c8185
-rw-r--r--lib/bundler/ruby_dsl.rb25
-rw-r--r--spec/bundler/bundler/ruby_dsl_spec.rb13
2 files changed, 27 insertions, 11 deletions
diff --git a/lib/bundler/ruby_dsl.rb b/lib/bundler/ruby_dsl.rb
index 228904850a..5e52f38c8f 100644
--- a/lib/bundler/ruby_dsl.rb
+++ b/lib/bundler/ruby_dsl.rb
@@ -43,17 +43,20 @@ module Bundler
def normalize_ruby_file(filename)
file_content = Bundler.read_file(gemfile.dirname.join(filename))
# match "ruby-3.2.2", ruby = "3.2.2", ruby = '3.2.2' or "ruby 3.2.2" capturing version string up to the first space or comment
- if /^ # Start of line
- ruby # Literal "ruby"
- [\s-]* # Optional whitespace or hyphens (for "ruby-3.2.2" format)
- (?:=\s*)? # Optional equals sign with whitespace (for ruby = "3.2.2" format)
- ["']? # Optional opening quote
- ( # Start capturing group
- [^\s#"']+ # One or more chars that aren't spaces, #, or quotes
- ) # End capturing group
- ["']? # Optional closing quote
- /x.match(file_content)
- $1
+ version_match = /^ # Start of line
+ ruby # Literal "ruby"
+ [\s-]* # Optional whitespace or hyphens (for "ruby-3.2.2" format)
+ (?:=\s*)? # Optional equals sign with whitespace (for ruby = "3.2.2" format)
+ (?:
+ "([^"]+)" # Double quoted version
+ |
+ '([^']+)' # Single quoted version
+ |
+ ([^\s#"']+) # Unquoted version
+ )
+ /x.match(file_content)
+ if version_match
+ version_match[1] || version_match[2] || version_match[3]
else
file_content.strip
end
diff --git a/spec/bundler/bundler/ruby_dsl_spec.rb b/spec/bundler/bundler/ruby_dsl_spec.rb
index 4ef0296695..45a37c5795 100644
--- a/spec/bundler/bundler/ruby_dsl_spec.rb
+++ b/spec/bundler/bundler/ruby_dsl_spec.rb
@@ -193,6 +193,19 @@ RSpec.describe Bundler::RubyDsl do
it_behaves_like "it stores the ruby version"
end
+
+ context "with mismatched quotes" do
+ let(:file_content) do
+ <<~TOML
+ [tools]
+ ruby = "#{version}'
+ TOML
+ end
+
+ it "raises an error" do
+ expect { subject }.to raise_error(Bundler::InvalidArgumentError, "= is not a valid requirement on the Ruby version")
+ end
+ end
end
context "with a .tool-versions file format" do