Skip to content

Commit 315c061

Browse files
gvkhnadeivid-rodriguez
authored andcommitted
Support ruby file: ".tool-versions" in Gemfile (#6898)
Supports .tool-versions (ASDF) by checking for a line starting with "ruby" before falling back to reading the entire file, as in .ruby-version. (cherry picked from commit 6c0a3e7)
1 parent 15ac03d commit 315c061

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

bundler/lib/bundler/man/gemfile.5

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ ruby file: "\.ruby\-version"
9898
.
9999
.IP "" 0
100100
.
101+
.P
102+
The version file should conform to any of the following formats:
103+
.
104+
.IP "\(bu" 4
105+
\fB3\.1\.2\fR (\.ruby\-version)
106+
.
107+
.IP "\(bu" 4
108+
\fBruby 3\.1\.2\fR (\.tool\-versions, read: https://asdf\-vm\.com/manage/configuration\.html#tool\-versions)
109+
.
110+
.IP "" 0
111+
.
101112
.SS "ENGINE"
102113
Each application \fImay\fR specify a Ruby engine\. If an engine is specified, an engine version \fImust\fR also be specified\.
103114
.

bundler/lib/bundler/man/gemfile.5.ronn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ you can use the `file` option instead.
7474

7575
ruby file: ".ruby-version"
7676

77+
The version file should conform to any of the following formats:
78+
79+
- `3.1.2` (.ruby-version)
80+
- `ruby 3.1.2` (.tool-versions, read: https://asdf-vm.com/manage/configuration.html#tool-versions)
81+
7782
### ENGINE
7883

7984
Each application _may_ specify a Ruby engine. If an engine is specified, an

bundler/lib/bundler/ruby_dsl.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ def ruby(*ruby_version)
1111

1212
if options[:file]
1313
raise GemfileError, "Cannot specify version when using the file option" if ruby_version.any?
14-
ruby_version << Bundler.read_file(Bundler.root.join(options[:file])).strip
14+
file_content = Bundler.read_file(Bundler.root.join(options[:file]))
15+
if /^ruby\s+(.*)$/.match(file_content) # match .tool-versions files
16+
ruby_version << $1.split("#", 2).first.strip # remove trailing comment
17+
else
18+
ruby_version << file_content.strip
19+
end
1520
end
1621

1722
if options[:engine] == "ruby" && options[:engine_version] &&

bundler/spec/bundler/ruby_dsl_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,31 @@ class MockDSL
121121
end
122122
end
123123
end
124+
125+
context "with a (.tool-versions) file option" do
126+
let(:options) { { :file => "foo" } }
127+
let(:version) { "3.2.2" }
128+
let(:ruby_version) { "3.2.2" }
129+
let(:ruby_version_arg) { nil }
130+
let(:engine_version) { version }
131+
let(:patchlevel) { nil }
132+
let(:engine) { "ruby" }
133+
let(:project_root) { Pathname.new("/path/to/project") }
134+
135+
before do
136+
allow(Bundler).to receive(:read_file).with(project_root.join("foo")).and_return("nodejs 18.16.0\nruby #{version} # This is a comment\npnpm 8.6.12\n")
137+
allow(Bundler).to receive(:root).and_return(Pathname.new("/path/to/project"))
138+
end
139+
140+
it_behaves_like "it stores the ruby version"
141+
142+
context "and a version" do
143+
let(:ruby_version_arg) { "2.0.0" }
144+
145+
it "raises an error" do
146+
expect { subject }.to raise_error(Bundler::GemfileError, "Cannot specify version when using the file option")
147+
end
148+
end
149+
end
124150
end
125151
end

0 commit comments

Comments
 (0)