bazelruby / rules_ruby

Formerly canonical rules for ruby, that are about 2-3 years behind current Bazel. If they work for you great, but if not — please try the new rules ruby by Alex Radionov: https://github.com/bazel-contrib/rules_ruby
Apache License 2.0
99 stars 37 forks source link

`ruby_library` not using specified SDK version #148

Open jgao54 opened 1 year ago

jgao54 commented 1 year ago

We are testing out the flow where the desired version of ruby is not installed on the host.

I expect for rules_ruby to be able to install and use v2.7.6. (Note this first requires bumping the ruby-build version, which I created a PR here).

With the version specified in WORKSPACE:

load("@bazelruby_rules_ruby//ruby:deps.bzl", "rules_ruby_select_sdk")
rules_ruby_select_sdk(version = "2.7.6")

I can confirm that ruby interpreter that's installed to the @org_ruby_lang_ruby_toolchain external repository is the right version:

➜  org_ruby_lang_ruby_toolchain build/bin/ruby -v
ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a) [arm64-darwin21]

However, running any ruby_library target i get this error on my macos:

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in 'require': incompatible library version...

I think this requires updating the way the binary wrapper is implemented, which defaults to the first ruby found in PATH today rather than the version that's specified in the SDK:

#!/usr/bin/env ruby

...

def find_ruby_binary
  File.join(
    RbConfig::CONFIG['bindir'],
    RbConfig::CONFIG['ruby_install_name'],
  )
end

In fact, it looks like there was an intention to substitute {interpreter} here but the wrapper script template itself does not have a {interpreter} placeholder anywhere, which looks like a bug.

jgao54 commented 1 year ago

I missed this issue, which points to the same bug.

kigster commented 10 months ago

That's not the issue afaik.

Thank you for including your error:

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in 'require': incompatible library version...

What this error tells you is that the Ruby being used here is the system Ruby that comes with Os-X. It's a Ruby 2.6, and it's installed into a read only file system. So naturally you can't install any gems there.

The rules expect your ruby to be installed by rbenv.

In order for rbenv to work, you must add the following to your shell initialization file:

eval "$(rbenv init -)"

Once you do and restart your terminal session, run the command which ruby, and if it worked you should see something like

That's how you know it's working.