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

"Can't find ruby interpreter in the PATH" #115

Open vg250156 opened 2 years ago

vg250156 commented 2 years ago

Hello, I'm looking for usage information on this project, looking to incorporate it into an existing mono-repo. For our purposes as a primarily golang project, I can't rely on ruby already being pre-installed on our devs' machines, so I'm doing this in a VM that doesn't have ruby installed. A very basic install with the WORKSPACE, BUILD.bazel, a Gemfile and a ruby source file fails to bazel run with /usr/bin/env: ‘ruby’: No such file or directory

It will also output a DEBUG message with DEBUG: /home/vince/.cache/bazel/_bazel_vince/acbc1f1ef0fcc039a89f9e3efce79ee0/external/bazelruby_rules_ruby/ruby/private/toolchains/ruby_runtime.bzl:91:14: Can't find ruby interpreter in the PATH

This is due to the build target boilerplate calling /usr/bin/env ruby in a shebang. I would have assumed that rules_ruby would put the ruby that it has just built into PATH when executing a ruby_binary but this apparently isn't the case. Is there some kind of WORKSPACE invocation I'm missing? How can I get it to use the ruby that it's installing?

vg250156 commented 2 years ago

I have my test project repo here: https://github.com/vg250156/ruby-rules-test/

To reproduce, ensure there is no ruby in PATH, then execute bazel run //:bar

kigster commented 2 years ago

I think most people rely on the preinstalled or rbenv-managed ruby.

While our rules do build ruby if none is found, this code path has not been widely used or tested.

In order for #!/usr/bin/env ruby to work the binary folder must simply be in the $PATH

The rules that build ruby interpreter were forked from the Sorbet project. Perhaps you can create an issue there or see how they set this up?

kigster commented 2 years ago

I would love to work with you on solving this together. Let me know if you are down.

mvgijssel commented 2 years ago

Running into this problem as well! The created ruby_binary is actually not using the pre-built interpreter but my system Ruby (which happens to be a different version breaking everything 😅).

Looking through the code it seems that the right interpreter binary is passed to the ruby_binary wrapper https://github.com/bazelruby/rules_ruby/blob/master/ruby/private/binary.bzl#L68 but the interpreter value is actually not used in the wrapper template itself https://github.com/bazelruby/rules_ruby/blob/master/ruby/private/binary_wrapper.tpl.

Secondly the interpreter executable is not added to the runfiles of the ruby_binary https://github.com/bazelruby/rules_ruby/blob/master/ruby/private/binary.bzl#L75-L78, so even if it would be used within the wrapper Ruby still wouldn't be able to find it.

Not a Bazel expert by far, so my observations could be completely wrong 🙈

kigster commented 1 year ago

Looking for additional core maintainers: https://github.com/bazelruby/rules_ruby/discussions/146