chruby icon indicating copy to clipboard operation
chruby copied to clipboard

chruby reports exit status 0 under Bash, but still throws a Ruby exception when switching to mruby

Open todd-a-jacobs opened this issue 2 years ago • 1 comments

Description

ruby-install sucessfully installs mruby-3.0.0 without errors. However, chruby reports a successful exit status under Bash, but still throws a Ruby exception when attempting to switch to mruby.

Whatever the underlying error is, it causes chruby-fish to exit non-zero, which prevents chruby from switching to mruby at all. This has been reported separately to chruby-fish as a related issue.

Steps To Reproduce

Steps to reproduce the bug:

  1. ruby-install mruby-3.0.0
  2. exec bash, which is required for chruby to pick up the newly installed Ruby
  3. chruby mruby; echo or chruby mruby-3.0.0

Expected Behavior

chruby should either exit non-zero if there's an error during the switch, or not display an error if the Ruby exception raised is expected.

Actual Behavior

My default Ruby is currently set to Ruby 3.1.2. ruby-install mruby-3.0.0 succeeds without error. Under Bash, the following error occurs:

$ chruby mruby
trace (most recent call last):
-:1: undefined method 'defined?' (NoMethodError)

$ echo $?
0

The current Ruby is still changed to mruby-3.0.0, RUBY_ROOT is set correctly, and mirb is executable.

Environment

$ bash --version | head -1
GNU bash, version 5.1.16(1)-release (x86_64-apple-darwin20.6.0)

$ chruby --version
chruby: 0.3.9

$ ruby --version
mruby 3.0.0 (2021-03-05)

$ gem --version
4.0.3

$ gem env | sed "s/$(id -un)/\$LOGNAME/g"
RubyGems Environment:
  - RUBYGEMS VERSION: 3.0.3
  - RUBY VERSION: 2.6.3 (2019-04-16 patchlevel 62) [universal.x86_64-darwin20]
  - INSTALLATION DIRECTORY: /Users/$LOGNAME/.gem
  - USER INSTALLATION DIRECTORY: /Users/$LOGNAME/.gem/ruby/2.6.0
  - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
  - GIT EXECUTABLE: /usr/local/bin/git
  - EXECUTABLE DIRECTORY: /Users/$LOGNAME/.gem/bin
  - SPEC CACHE DIRECTORY: /Users/$LOGNAME/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Library/Ruby/Site
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-20
  - GEM PATHS:
     - /Users/$LOGNAME/.gem
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["https://rubygems.org"]
     - :concurrent_downloads => 4
     - "gem" => "--env-shebang --minimal-deps --no-document --patch --suggestions --wrappers"
  - REMOTE SOURCES:
     - https://rubygems.org
  - SHELL PATH:
     - /Users/$LOGNAME/.gem///bin
     - /Users/$LOGNAME/.rubies/mruby-3.0.0/bin
     - /usr/local/opt/[email protected]/libexec/bin
     - /Users/$LOGNAME/bin
     - /Users/$LOGNAME/perl5/bin
     - /usr/local/bin
     - /usr/local/sbin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /opt/puppetlabs/pdk/bin
     - /opt/X11/bin
     - /opt/puppetlabs/bin
     - /Library/Apple/usr/bin
     - /Users/$LOGNAME/bin
     - /Users/$LOGNAME/perl5/bin
     - /usr/local/sbin
     - /usr/local/opt/openssl/bin
     - /usr/local/Cellar/[email protected]/3.9.12/libexec/bin
     - /usr/local/opt/[email protected]/bin
     - /usr/local/opt/openssl@3/bin
     - /Users/$LOGNAME/.local/bin/jetbrains
     - /usr/local/opt/[email protected]/bin
     - /usr/local/opt/gcc/bin
     - /Users/$LOGNAME/node_modules/bin
     - /Users/$LOGNAME/go/bin

todd-a-jacobs avatar May 01 '22 15:05 todd-a-jacobs

Ah, this was fixed by c44c6cc656104ab1d800d437313c967c59d9303a, which probably should get a patch release. The other bug is that chruby_use is not checking$? before executing the output of the ruby command. Something like this should catch it:

local ruby_env="$(RUBYGEMS_GEMDEPS="" "$RUBY_ROOT/bin/ruby" - <<EOF
puts "export RUBY_ENGINE=#{Object.const_defined?(:RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'};"
puts "export RUBY_VERSION=#{RUBY_VERSION};"
begin; require 'rubygems'; puts "export GEM_ROOT=#{Gem.default_dir.inspect};"; rescue LoadError; end
EOF)"

if (( $? != 0 )); then
  echo "chruby: failed to execute $1/bin/ruby command" >&2
  return -1
fi

postmodern avatar May 01 '22 19:05 postmodern