Skip to content

Commit

Permalink
Properly parse .ruby-version file (#9012)
Browse files Browse the repository at this point in the history
In ccb965e, I used
Bundler::RubyVersion.from_string, but this method is used to parse the
Ruby version present in the Gemfile, of the form `ruby 3.3.0p0`, while
we want to parse `3.3.0` or `ruby-3.3.0`.

The code is lifted from `Bundler::RubyDsl#normalize_ruby_file`, so it
only supports either `ruby-{version}` or `ruby {version}` or a plain
version. That's what `ruby file: ".ruby-version"` supports.

Co-authored-by: Jenny Shen <jenny.shen@shopify.com>
  • Loading branch information
etiennebarrie and jenshenny committed Feb 14, 2024
1 parent 5c637ec commit 935ab99
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
module BundlerDefinitionRubyVersionPatch
def ruby_version
super || begin
Bundler::RubyVersion.from_string(File.read(".ruby-version", chomp: true))
file_content = Bundler.read_file(".ruby-version")
ruby_version =
if /^ruby(-|\s+)([^\s#]+)/ =~ file_content
::Regexp.last_match(2)
else
file_content.strip
end
Bundler::RubyVersion.new(ruby_version, nil, nil, nil) if ruby_version
rescue SystemCallError
# .ruby-version doesn't exist, fallback to the Ruby Dependabot runs
end
Expand Down
40 changes: 40 additions & 0 deletions bundler/helpers/v2/spec/ruby_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# typed: false
# frozen_string_literal: true

require "native_spec_helper"
require "shared_contexts"

RSpec.describe BundlerDefinitionRubyVersionPatch do
include_context "in a temporary bundler directory"
include_context "stub rubygems compact index"

let(:project_name) { "ruby_version_implied" }
before do
@ui = Bundler.ui
Bundler.ui = Bundler::UI::Silent.new
end
after { Bundler.ui = @ui }

it "updates to the most recent version" do
in_tmp_folder do
File.delete(".ruby-version")
definition = Bundler::Definition.build("Gemfile", "Gemfile.lock", gems: ["statesman"])
definition.resolve_remotely!
specs = definition.resolve["statesman"]
expect(specs.size).to eq(1)
spec = specs.first
expect(spec.version).to eq("7.2.0")
end
end

it "doesn't update to a version that is not compatible with the Ruby version implied by .ruby-version" do
in_tmp_folder do
definition = Bundler::Definition.build("Gemfile", "Gemfile.lock", gems: ["statesman"])
definition.resolve_remotely!
specs = definition.resolve["statesman"]
expect(specs.size).to eq(1)
spec = specs.first
expect(spec.version).to eq("2.0.1")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.1.10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "https://rubygems.org"

gem "business"
gem "statesman"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
business (1.12.0)
statesman (2.0.1)

PLATFORMS
ruby

DEPENDENCIES
business
statesman

BUNDLED WITH
2.5.3

0 comments on commit 935ab99

Please sign in to comment.