Search code examples
ruby-on-railsrubyzshrbenv

Add local binstubs to rbenv (getting command not found)


If you have simple shell command binstubs in the local bin directory of a Rails project (e.g. not generated by or running a gem), rbenv seems to have trouble executing them. For example, @tpope's heroku binstubs generate a binstub named production in the local bin directory:

#!/bin/sh
HEROKU_APP=myapp-production HKAPP=myapp-production exec "${HEROKU_COMMAND:-heroku}" "$@"

After an rbenv rehash, the production command shows up in the ~/.rbenv/shims directory looking something like this:

#!/usr/bin/env bash
set -e
[ -n "$RBENV_DEBUG" ] && set -x

program="${0##*/}"
if [ "$program" = "ruby" ]; then
  for arg; do
    case "$arg" in
    -e* | -- ) break ;;
    */* )
      if [ -f "$arg" ]; then
        export RBENV_DIR="${arg%/*}"
        break
      fi
      ;;
    esac
  done
fi

export RBENV_ROOT="/Users/Username/.rbenv"
exec "/usr/local/Cellar/rbenv/0.4.0/libexec/rbenv" exec "$program" "$@"

So executing which production gives you:

/Users/Username/.rbenv/shims/production

But executing rbenv which production (or trying to run the command) gives you:

rbenv: production: command not found

I'm new to rbenv so maybe I missed a config step?


Solution

  • Apparently project-specific binstubs in Rails projects should be kept outside of the local bin/ directory since those are primarily for application scripts. So one approach (i.e. that used by the rbenv-binstubs plugin) is to keep local binstubs separate and override rbenv shims with that local binstub directory (e.g. .bundle/bin). So by using the plugin you might see this result from which production:

    /Users/Username/.rbenv/shims/production
    

    But if you move the production binstub to .bundle/bin, then rbenv which production should yield:

    {PROJECT_ROOT}/.bundle/bin/production