Sarah Sarah - 3 months ago 15
Ruby Question

Ruby: 'bundle exec' throws error for all shell commands

In my project, I have a Gem that contains a shell script in its bin folder, let's call the script do_something.sh.

do_something.sh actually executes a ruby script using Jruby command, the script is called ruby_script.rb.

I am trying to call do_something.sh from my project using:

bundle exec do_something.sh


it keeps throwing errors for all shell commands in the script. I erased all the contents of the script and added only one line "echo 'Hello'" and it is still throwing the following error

NoMethodError: undefined method
echo' for main:Object
/usr/local/rvm/gems/ruby-2.1.1@projectName/gems/gemName/bin/do_something.sh:10:in
'
/usr/local/rvm/gems/ruby-2.1.1@projectName/bin/do_something.sh:23:in
load'
/usr/local/rvm/gems/ruby-2.1.1@projectName/bin/do_something.sh:23:in
'


Edit#1
All the files has execute permission.

I also tried to put ruby_script.rb in the bin directory where are files are executables (according to a rule in the gemspec file) and tried calling

bundle exec ruby_script.rb


I get the error "bundler: command not found: ruby_script.rb"

bundle exec ruby ruby_script.rb


I get the error "ruby: No such file or directory -- ruby_script.rb (LoadError)"

Why am I getting this error and how can I solve it? I want to be able to either run do_something.sh or ruby_script.rb. Right now, ruby_script.rb is not recognised and do_something.sh does not recognise the commands.

Answer

Ok, finally after two days, I was able to make it work.

bundle install installs my GEM and creates a ruby wrapper by default to sh files in the 'bin' directory. So, when I call my script using bundle exec the ruby wrapper gets called first and it calls my script using a ruby 'load' function. This load function expects a ruby file and that is why all my shell script in the file threw errors, because they were expected to be in ruby.

A workaround to this is installing my gem before 'bundle install' using the following command:

gem install --no-wrapper my-gem

Apparently this command disabled creating the ruby wrapper and I was finally able to call the script using bundle execute do_something.sh