Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions bundler/lib/bundler/ruby_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,21 @@ def ruby(*ruby_version)
# Loads the file relative to the dirname of the Gemfile itself.
def normalize_ruby_file(filename)
file_content = Bundler.read_file(gemfile.dirname.join(filename))
# match "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
# 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
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
Expand Down
27 changes: 25 additions & 2 deletions bundler/spec/bundler/ruby_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,34 @@ class MockDSL
let(:file_content) do
<<~TOML
[tools]
ruby = "#{version}"
ruby = #{quote}#{version}#{quote}
TOML
end

it_behaves_like "it stores the ruby version"
context "with double quotes" do
let(:quote) { '"' }

it_behaves_like "it stores the ruby version"
end

context "with single quotes" do
let(:quote) { "'" }

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")
Copy link
Contributor Author

@hituzi-no-sippo hituzi-no-sippo Dec 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the error message = is not a valid requirement ... is appropriate.

If I add = to ([^\s#"']+) # Unquoted version to make it ([^\s#"'=]+), the error message would become [tools]\nruby = \"2.0.0' is not a valid requirement ..., which I feel is better than = is not a valid requirement.
However, I'm concerned this change to ([^\s#"'=]+) might introduce regressions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not make any changes involving = at this time, as we don't yet fully support the mise format.

FYI: #8998

end
end
end

context "with a .tool-versions file format" do
Expand Down