Repository: pre-commit/pre-commit

Issue:
Ruby hooks failing with rbenv installed
Pre-commit has been failing for the past few weeks.

https://gist.github.com/ThatGerber/d6533155848076b25e5e0d5cb02e20eb

Seems to be an issue with the ruby (rbenv) environment.

Tried running `pre-commit clean && pre-commit` but it returns the same issue. Setting `rbenv global 2.2.4` and `rbenv shell 2.2.4` does help either.



Discussion:
indeed, seems broken for unspecified versions -- unfortunately we didn't notice this because we force a newer version that our (ancient) os supplies.

I'll get a fix for this (and hopefully a test to prevent this from regressing in the future!)

Thanks. Let me know if I can assist in any way.

Actually, I'm having some issues reproducing :(

Can you supply the following (might make it easier for me to reproduce):

`which rbenv`
`env | grep -Ei '(ruby|rbenv|rvm)'`

@ThatGerber ^

perhaps also the version of rbenv you're using as well :)

https://gist.github.com/ThatGerber/18a0f90b5b709ea05e67edfdec9f4ba4

Here you go.

Ah I can reproduce with the following (in a docker container)

```
    2  apt-get update
    3  apt-get install git libssl-dev libreadline-dev nano virtualenv python
    4  git clone http://github.com/rbenv/rbenv ~/.rbenv
    5  cd ~/.rbenv/
    7  git checkout 9fdce5d
   13  cd ~/.rbenv/
   15  apt-get install build-essential
   16  apt-get install curl
   17  ./src/configure 
   18  make -C src
  # Add the rbenv stuff to bashrc
   26  nano ~/.bashrc 
   27  . ~/.bashrc
   29  git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
   33  apt-get install libyaml-dev
   36  rbenv install 2.2.4
   38  rbenv global 2.2.4
...
    2  cd ~
    3  virtualenv venv
    4  . venv/bin/activate
    6  apt-get install python-dev python3-dev -y
    7  pip install pre-commit
    8  git clone git://github.com/pre-commit/demo-repo
    9  cd demo-repo/
   # Remove all the hooks except for the ruby one
   10  nano .pre-commit-config.yaml 
   13  pre-commit run --all-files
```

And then:

```
(venv) root@1fcbf03f029a:~/demo-repo# pre-commit run --all-files
[INFO] Initializing environment for git://github.com/pre-commit/mirrors-scss-lint.
[INFO] Installing environment for git://github.com/pre-commit/mirrors-scss-lint.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: Command: (u'/bin/bash', u'/root/.rbenv/shims/gem', 'build', '__fake_gem.gemspec')
Return code: 1
Expected return code: 0
Output: (none)
Errors: 
    rbenv: version `default' is not installed (set by RBENV_VERSION environment variable)

```

This patch seems to fix it \o/

``` diff
--- a/pre_commit/languages/ruby.py
+++ b/pre_commit/languages/ruby.py
@@ -19,16 +19,18 @@ ENVIRONMENT_DIR = 'rbenv'


 def get_env_patch(venv, language_version):
-    return (
+    patches = (
         ('GEM_HOME', os.path.join(venv, 'gems')),
         ('RBENV_ROOT', venv),
-        ('RBENV_VERSION', language_version),
         ('PATH', (
             os.path.join(venv, 'gems', 'bin'), os.pathsep,
             os.path.join(venv, 'shims'), os.pathsep,
             os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),
         )),
     )
+    if language_version != 'default':
+        patches += (('RBENV_VERSION', language_version),)
+    return patches


 @contextlib.contextmanager
```


Resolving patch:
diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py
--- a/pre_commit/languages/ruby.py
+++ b/pre_commit/languages/ruby.py
@@ -19,16 +19,18 @@
 
 
 def get_env_patch(venv, language_version):
-    return (
+    patches = (
         ('GEM_HOME', os.path.join(venv, 'gems')),
         ('RBENV_ROOT', venv),
-        ('RBENV_VERSION', language_version),
         ('PATH', (
             os.path.join(venv, 'gems', 'bin'), os.pathsep,
             os.path.join(venv, 'shims'), os.pathsep,
             os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),
         )),
     )
+    if language_version != 'default':
+        patches += (('RBENV_VERSION', language_version),)
+    return patches
 
 
 @contextlib.contextmanager


Test patch: