Rescue from errors during post-install rehash hook in rubygems_plugin.rb
#1513
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ruby backtick syntax does not raise exceptions if a non-zero exit code is returned by the command inside the backticks. This means we never reach inside the
rescue
block ofrubygems_plugin.rb
's hook lambda if therbenv rehash
command errors out (although we will still see any output thatrbenv-rehash
prints tostderr
).We can demonstrate this by purposely creating the rehash lock file, and attempting to install a gem which includes an executable:
We can see that
rbenv: cannot rehash: /Users/richiethomas/.rbenv/shims/.rbenv-shim exists
printed tostderr
, but the warning text, error class name, etc. inside the rescue block (i.e.rbenv: error in gem-rehash ...
) is not printed.This issue can be fixed using the Ruby standard library's
Open3
module, which runs a command taken from an argument, and returns the output fromstdout
andstderr
. If anystderr
text exists, we can raise an exception with said text, and rescue with the expected warning message.With this in place, we now see the following output:
Note that I did also try a non-backtick invocation of
rbenv rehash
, i.e.system "rbenv rehash"
, but this similarly failed to raise an exception.More info on Open3 here- https://docs.ruby-lang.org/en/2.0.0/Open3.html
Official module repo here- https://github.com/ruby/open3