asdf-vm / asdf-ruby

Ruby plugin for asdf version manager
https://github.com/asdf-vm/asdf
MIT License
669 stars 134 forks source link

Support specifying ruby-version in Gemfile from a file #412

Closed espen closed 3 weeks ago

espen commented 2 months ago

It is possible to specify ruby version in the Gemfile using a file instead of a version:

ruby file: ".ruby-version"

This is not supported by asdf-ruby.

Related code: https://github.com/asdf-vm/asdf-ruby/blob/d6eb4145ef780fda868fd4439c60dd84a941cb8e/bin/parse-legacy-file#L7

Bundler Gemfile documentation: https://bundler.io/guides/gemfile_ruby.html

pboling commented 2 months ago

Adding support for reading from a Gemfile would be an issue for the rubygems project, which hosts the bundler gem, which handles parsing of Gemfile.

Oh, you're asking if asdf-ruby can support reading a ruby version from a Gemfile, that is itself reading from a file... sorry I misunderstood.

Why not just add .tool-versions to the project? If you are able to manipulate the project files, to change the Gemfile, you should be able to add .tool-versions, right?

espen commented 2 months ago

Using tool-versions has the same issue. asdf-ruby will parse the Gemfile when a Gemfile is present, and parsing the Gemfile does not support specifying ruby version using a file.

jmschp commented 3 weeks ago

Got curious about this one.

If you have the .ruby-version file, asdf should be picking it up, at least if you have the legacy_version_file = yes.

asdf won't pick up the version from Gemfile, if either ruby file: ".ruby-version" or ruby file: ".tool-versions", is set. But it will pick the actual file.

So what exactly is your intention?

espen commented 3 weeks ago

@jmschp asdf will read from the Gemfile when a Gemfile is present. asdf only support a specific version in the gemfile and not using ruby file: ".ruby-version". The intention is to set the Ruby version in ruby-version only and not also in Gemfile (hence using ruby file: ".ruby-version").

Perhaps a solution is to check for ruby-version before checking for Gemfile.

jmschp commented 3 weeks ago

Set in your .asdfrc the following config legacy_version_file = yes.

Now in your project you have a Gemfile with ruby file: ".ruby-version", and on that same project you have the .ruby-version file let's say with 3.3.5.

Then asdf will pick up the .ruby-version file, and read the version 3.3.5.

Do you want to avoid setting legacy_version_file = yes in .asdfrc?

espen commented 3 weeks ago

I am already using legacy_version_file = yes. I am not that familiar with asdf but looking at the code it seems like: 1) asdf will parse Gemfile when Gemfile is present, and thus not parse .ruby-version first. 2) the parser only works with a specified version and not setting it to read from a file.

jmschp commented 3 weeks ago

asdf is going to look at the files listed in bin/list-legacy-filenames. See .ruby-version is listed there, So then bin/parse-legacy-file is going to be ran with .ruby-version, and it will pick the version from the file.

Have a look at the docs https://asdf-vm.com/plugins/create.html#bin-list-legacy-filenames

espen commented 3 weeks ago

Sorry I was unclear. The problem is when using bundler. Bundler is unable to pick up on the ruby version when it is set using a file in the Gemfile. So it could be a bundler problem. It is even unable to tell me which ruby version in the gemfile is specified when showing an error: Your Ruby version is 3.3.5, but your Gemfile specified. I tried some debugging but was unable to find the source of the issue but see that the comparison is done against this: #<Bundler::RubyVersion:0x000000011e473708 @versions=[], @gem_version=#<Gem::Version "0">, @input_engine=nil, @engine="ruby", @engine_versions=[], @engine_gem_version=#<Gem::Version "0">, @patchlevel=nil>

I will investigate and either reopen or open an issue in the rubygems project.