sstephenson/rbenv — Issue #150: Handle Shim Missing
Author: leehambley
Created: 2011-11-22 12:14:03+00:00
State: open

I'm proposing a new feature (and if you would accept the feature request as being within the scope of `rbenv`, I'm willing to write it.)

I'd like to propose that there's a `shim missing` feature that detects when a command is not found, initializes `rbenv rehash`, tries again (once) - otherwise raises an appropriate error, and gives the shell back to the user.

I believe this is to be safe as calling `rbenv rehash` should not ever be destructive, is a fast operation (at least in wall time).

For bash, at least this could easily be implemented with something such as:

``` shell
function handle_missing_command {
status=$?;
lastcmd=$(history | tail -n1);
trap - ERR;
if [[ $status -eq 127 ]]; then
echo "You called a command that didn't exist ($(echo $lastcmd)), rbenv rehashing and trying again";
rbenv rehash;
$lastcmd;
fi;
trap "handle_missing_command" ERR;
}
trap "handle_missing_command" ERR;

```

My shocking code not withstanding, I would expect the following workflow:

``` shell
$ rvbenv local 1.9.3-p0
$ gem install bundler --no-rdoc --no-ri
$ bundle
> You called a command that didn't exist (bundle), rbenv is rehashing and trying again";
> ...snip...
> Your bundle was successfully installed.
```

Bash isn't my strong suit, and as may be quite evident I haven't tested this code, at all - but it's what I have in mind, and it would solve the chief complaint that I hear about `rbenv` which is "it sucks having to rebuild shims, that's lame". There's more that could be done, for example a quick look in the `$RBENV_HOME/versions/*/bin` directories could be used to determine whether this was a missing shim, or if the user just made a typo.

Personally, I don't think rebuilding shims is a problem but I've seen it be a problem enough for many of my coworkers, that for better or worse, `rbenv` doesn't work exactly as people are expecting.

Some things that are unclear to me right now:

* What should happen if the user has a custom defined trap method (think ubuntu command missing?)
* Inside of a bash script running with `-e` ? (appears to mean the script inherits the error trap from the parent shell)
* I'm not comfortable with the lack of a reliable way to get the last executed command without resorting to `history`, I at least run history with the
* It is of course only really advisable if zsh can do something similar.

Should almost certainly be an optional, but clearly documented part of `rbenv`.