This repository was archived by the owner on Apr 24, 2020. It is now read-only.
[Bugfix] prompt_rbenv fails if specified local ruby version is not present #1197
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.

There is a bug in
prompt_rbenv()that appears when a user enters a directory where a localrbenvsetting specifies a version that is not present on the local machine:My
rbenvhassystemand2.6.1installed, with2.6.1set as the global version. However, theoh-my-zshcustom pluginzsh-autosuggestionshas a.ruby-versionfile in its project directory which specifiesrubyversion2.5.3, which is not installed.prompt_rbenv()invokesrbenv version-nameto determine the local ruby version, which works fine when there is a local version that's installed withrbenvor there is no local version specified. However, when a local version is specified that isn't present on the machine,rbenv version-namedisplays nostdoutbut does exit with a value of1and produces thestderrthat's polluting the prompt:rbenv: version '2.5.3' is not installed...Since it doesn't supply anystdout,prompt_rbenv()has no version info to publish and simply shows the ruby logo, which is unhelpful for users trying to figure out what's going on.First, this fix silences the error that's escaping into the prompt by appending
2>/dev/nullto therbenv version-namecall.Then, because
rbenv localwill always supply the local version number (even if it's not present) the logical OR function||is used to ensure that it's called, shouldrbenv version-nameerror out. It should be noted thatrbenv localactually errors out if a local version isn't specified at all, but that's not a problem, since it will only be called if (and only if)rbenv version-namefails, but in that case,rbenv version-namesimply returns the global version first.Similarly, this bug fix adds no additional overhead, since
rbenv localwill only be called in the relatively rare event thatrbenv version-namefirst fails. However, I do agree with the discussion in #215, in that a way should probably be found to optimize or cache this function, since it is going to invoke at least one instance ofrbenvfor every prompt, even when there is no ruby project to be found and nothing additional is displayed on the prompt to atone for it.Finally, I thought it would be nice if there were also an indication that the requested⚠️ icon, or change the colors, or something else, but I didn't bother here.
rubyversion wasn't installed—rather than just printing it as if everything was normal—so I enclosed the version number in quotes in that instance. It could be a future enhancement to include a warning triangleSo, after the patch is applied, here's the new behavior: