This repository has been 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 localrbenv
setting specifies a version that is not present on the local machine:My
rbenv
hassystem
and2.6.1
installed, with2.6.1
set as the global version. However, theoh-my-zsh
custom pluginzsh-autosuggestions
has a.ruby-version
file in its project directory which specifiesruby
version2.5.3
, which is not installed.prompt_rbenv()
invokesrbenv version-name
to determine the local ruby version, which works fine when there is a local version that's installed withrbenv
or there is no local version specified. However, when a local version is specified that isn't present on the machine,rbenv version-name
displays nostdout
but does exit with a value of1
and produces thestderr
that'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/null
to therbenv version-name
call.Then, because
rbenv local
will 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-name
error out. It should be noted thatrbenv local
actually 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-name
fails, but in that case,rbenv version-name
simply returns the global version first.Similarly, this bug fix adds no additional overhead, since
rbenv local
will only be called in the relatively rare event thatrbenv version-name
first 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 ofrbenv
for 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.
ruby
version 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: