We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
No such file or directory: '/github/home/.cache/pre-commit/repo4mrvfeou/rbenv-system/.install_state_v1staging'
### search you tried in the issue tracker

Found this one #1658

### describe your issue

Running pre-commit GitHub action in a [custom container](https://github.com/platform-engineering-org/helper/blob/main/Dockerfile).

Workflow is [broken](https://github.com/platform-engineering-org/bootstrap/actions/runs/4342905858/jobs/7584289627):
```
[INFO] Installing environment for https://github.com/markdownlint/markdownlint.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: FileNotFoundError: [Errno 2] No such file or directory: '/github/home/.cache/pre-commit/repo4mrvfeou/rbenv-system/.install_state_v1staging'
```



### pre-commit --version

pre-commit 2.20.0

### .pre-commit-config.yaml

```yaml
---
ci:
  skip: [terraform_fmt, terraform_validate, terragrunt_validate]

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
      - id: end-of-file-fixer
      - id: trailing-whitespace
        args:
          - --markdown-linebreak-ext=md
      - id: check-docstring-first
      - id: requirements-txt-fixer
      - id: check-merge-conflict
      - id: no-commit-to-branch
        args:
          - "--branch"
          - "main"
      - id: check-symlinks
      - id: detect-private-key
      - id: detect-aws-credentials
        args:
          - --allow-missing-credentials
      - id: check-json
  - repo: https://github.com/markdownlint/markdownlint
    rev: v0.12.0
    hooks:
      - id: markdownlint
        additional_dependencies: [rake]
  - repo: https://github.com/maxbrunet/pre-commit-renovate
    rev: 34.157.1
    hooks:
      - id: renovate-config-validator
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.77.1
    hooks:
      - id: terraform_providers_lock
        args:
          - --tf-init-args=-upgrade
      - id: terraform_fmt
      - id: terraform_validate
      - id: terragrunt_validate
```


### ~/.cache/pre-commit/pre-commit.log (if present)

_No response_

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pre_commit/languages/ruby.py`
Content:
```
1 from __future__ import annotations
2 
3 import contextlib
4 import functools
5 import importlib.resources
6 import os.path
7 import shutil
8 import tarfile
9 from typing import Generator
10 from typing import IO
11 from typing import Sequence
12 
13 import pre_commit.constants as C
14 from pre_commit import lang_base
15 from pre_commit.envcontext import envcontext
16 from pre_commit.envcontext import PatchesT
17 from pre_commit.envcontext import UNSET
18 from pre_commit.envcontext import Var
19 from pre_commit.prefix import Prefix
20 from pre_commit.util import CalledProcessError
21 
22 ENVIRONMENT_DIR = 'rbenv'
23 health_check = lang_base.basic_health_check
24 run_hook = lang_base.basic_run_hook
25 
26 
27 def _resource_bytesio(filename: str) -> IO[bytes]:
28     return importlib.resources.open_binary('pre_commit.resources', filename)
29 
30 
31 @functools.lru_cache(maxsize=1)
32 def get_default_version() -> str:
33     if all(lang_base.exe_exists(exe) for exe in ('ruby', 'gem')):
34         return 'system'
35     else:
36         return C.DEFAULT
37 
38 
39 def get_env_patch(
40         venv: str,
41         language_version: str,
42 ) -> PatchesT:
43     patches: PatchesT = (
44         ('GEM_HOME', os.path.join(venv, 'gems')),
45         ('GEM_PATH', UNSET),
46         ('BUNDLE_IGNORE_CONFIG', '1'),
47     )
48     if language_version == 'system':
49         patches += (
50             (
51                 'PATH', (
52                     os.path.join(venv, 'gems', 'bin'), os.pathsep,
53                     Var('PATH'),
54                 ),
55             ),
56         )
57     else:  # pragma: win32 no cover
58         patches += (
59             ('RBENV_ROOT', venv),
60             (
61                 'PATH', (
62                     os.path.join(venv, 'gems', 'bin'), os.pathsep,
63                     os.path.join(venv, 'shims'), os.pathsep,
64                     os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),
65                 ),
66             ),
67         )
68     if language_version not in {'system', 'default'}:  # pragma: win32 no cover
69         patches += (('RBENV_VERSION', language_version),)
70 
71     return patches
72 
73 
74 @contextlib.contextmanager
75 def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
76     envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
77     with envcontext(get_env_patch(envdir, version)):
78         yield
79 
80 
81 def _extract_resource(filename: str, dest: str) -> None:
82     with _resource_bytesio(filename) as bio:
83         with tarfile.open(fileobj=bio) as tf:
84             tf.extractall(dest)
85 
86 
87 def _install_rbenv(
88         prefix: Prefix,
89         version: str,
90 ) -> None:  # pragma: win32 no cover
91     envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
92 
93     _extract_resource('rbenv.tar.gz', prefix.path('.'))
94     shutil.move(prefix.path('rbenv'), envdir)
95 
96     # Only install ruby-build if the version is specified
97     if version != C.DEFAULT:
98         plugins_dir = os.path.join(envdir, 'plugins')
99         _extract_resource('ruby-download.tar.gz', plugins_dir)
100         _extract_resource('ruby-build.tar.gz', plugins_dir)
101 
102 
103 def _install_ruby(
104         prefix: Prefix,
105         version: str,
106 ) -> None:  # pragma: win32 no cover
107     try:
108         lang_base.setup_cmd(prefix, ('rbenv', 'download', version))
109     except CalledProcessError:  # pragma: no cover (usually find with download)
110         # Failed to download from mirror for some reason, build it instead
111         lang_base.setup_cmd(prefix, ('rbenv', 'install', version))
112 
113 
114 def install_environment(
115         prefix: Prefix, version: str, additional_dependencies: Sequence[str],
116 ) -> None:
117     if version != 'system':  # pragma: win32 no cover
118         _install_rbenv(prefix, version)
119         with in_env(prefix, version):
120             # Need to call this before installing so rbenv's directories
121             # are set up
122             lang_base.setup_cmd(prefix, ('rbenv', 'init', '-'))
123             if version != C.DEFAULT:
124                 _install_ruby(prefix, version)
125             # Need to call this after installing to set up the shims
126             lang_base.setup_cmd(prefix, ('rbenv', 'rehash'))
127 
128     with in_env(prefix, version):
129         lang_base.setup_cmd(
130             prefix, ('gem', 'build', *prefix.star('.gemspec')),
131         )
132         lang_base.setup_cmd(
133             prefix,
134             (
135                 'gem', 'install',
136                 '--no-document', '--no-format-executable',
137                 '--no-user-install',
138                 *prefix.star('.gem'), *additional_dependencies,
139             ),
140         )
141
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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
@@ -114,6 +114,8 @@
 def install_environment(
         prefix: Prefix, version: str, additional_dependencies: Sequence[str],
 ) -> None:
+    envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
+
     if version != 'system':  # pragma: win32 no cover
         _install_rbenv(prefix, version)
         with in_env(prefix, version):
@@ -135,6 +137,8 @@
                 'gem', 'install',
                 '--no-document', '--no-format-executable',
                 '--no-user-install',
+                '--install-dir', os.path.join(envdir, 'gems'),
+                '--bindir', os.path.join(envdir, 'gems', 'bin'),
                 *prefix.star('.gem'), *additional_dependencies,
             ),
         )

{"golden_diff": "diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py\n--- a/pre_commit/languages/ruby.py\n+++ b/pre_commit/languages/ruby.py\n@@ -114,6 +114,8 @@\n def install_environment(\n         prefix: Prefix, version: str, additional_dependencies: Sequence[str],\n ) -> None:\n+    envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)\n+\n     if version != 'system':  # pragma: win32 no cover\n         _install_rbenv(prefix, version)\n         with in_env(prefix, version):\n@@ -135,6 +137,8 @@\n                 'gem', 'install',\n                 '--no-document', '--no-format-executable',\n                 '--no-user-install',\n+                '--install-dir', os.path.join(envdir, 'gems'),\n+                '--bindir', os.path.join(envdir, 'gems', 'bin'),\n                 *prefix.star('.gem'), *additional_dependencies,\n             ),\n         )\n", "issue": "No such file or directory: '/github/home/.cache/pre-commit/repo4mrvfeou/rbenv-system/.install_state_v1staging'\n### search you tried in the issue tracker\n\nFound this one #1658\n\n### describe your issue\n\nRunning pre-commit GitHub action in a [custom container](https://github.com/platform-engineering-org/helper/blob/main/Dockerfile).\r\n\r\nWorkflow is [broken](https://github.com/platform-engineering-org/bootstrap/actions/runs/4342905858/jobs/7584289627):\r\n```\r\n[INFO] Installing environment for https://github.com/markdownlint/markdownlint.\r\n[INFO] Once installed this environment will be reused.\r\n[INFO] This may take a few minutes...\r\nAn unexpected error has occurred: FileNotFoundError: [Errno 2] No such file or directory: '/github/home/.cache/pre-commit/repo4mrvfeou/rbenv-system/.install_state_v1staging'\r\n```\r\n\r\n\n\n### pre-commit --version\n\npre-commit 2.20.0\n\n### .pre-commit-config.yaml\n\n```yaml\n---\r\nci:\r\n  skip: [terraform_fmt, terraform_validate, terragrunt_validate]\r\n\r\nrepos:\r\n  - repo: https://github.com/pre-commit/pre-commit-hooks\r\n    rev: v4.4.0\r\n    hooks:\r\n      - id: end-of-file-fixer\r\n      - id: trailing-whitespace\r\n        args:\r\n          - --markdown-linebreak-ext=md\r\n      - id: check-docstring-first\r\n      - id: requirements-txt-fixer\r\n      - id: check-merge-conflict\r\n      - id: no-commit-to-branch\r\n        args:\r\n          - \"--branch\"\r\n          - \"main\"\r\n      - id: check-symlinks\r\n      - id: detect-private-key\r\n      - id: detect-aws-credentials\r\n        args:\r\n          - --allow-missing-credentials\r\n      - id: check-json\r\n  - repo: https://github.com/markdownlint/markdownlint\r\n    rev: v0.12.0\r\n    hooks:\r\n      - id: markdownlint\r\n        additional_dependencies: [rake]\r\n  - repo: https://github.com/maxbrunet/pre-commit-renovate\r\n    rev: 34.157.1\r\n    hooks:\r\n      - id: renovate-config-validator\r\n  - repo: https://github.com/antonbabenko/pre-commit-terraform\r\n    rev: v1.77.1\r\n    hooks:\r\n      - id: terraform_providers_lock\r\n        args:\r\n          - --tf-init-args=-upgrade\r\n      - id: terraform_fmt\r\n      - id: terraform_validate\r\n      - id: terragrunt_validate\n```\n\n\n### ~/.cache/pre-commit/pre-commit.log (if present)\n\n_No response_\n", "before_files": [{"content": "from __future__ import annotations\n\nimport contextlib\nimport functools\nimport importlib.resources\nimport os.path\nimport shutil\nimport tarfile\nfrom typing import Generator\nfrom typing import IO\nfrom typing import Sequence\n\nimport pre_commit.constants as C\nfrom pre_commit import lang_base\nfrom pre_commit.envcontext import envcontext\nfrom pre_commit.envcontext import PatchesT\nfrom pre_commit.envcontext import UNSET\nfrom pre_commit.envcontext import Var\nfrom pre_commit.prefix import Prefix\nfrom pre_commit.util import CalledProcessError\n\nENVIRONMENT_DIR = 'rbenv'\nhealth_check = lang_base.basic_health_check\nrun_hook = lang_base.basic_run_hook\n\n\ndef _resource_bytesio(filename: str) -> IO[bytes]:\n    return importlib.resources.open_binary('pre_commit.resources', filename)\n\n\n@functools.lru_cache(maxsize=1)\ndef get_default_version() -> str:\n    if all(lang_base.exe_exists(exe) for exe in ('ruby', 'gem')):\n        return 'system'\n    else:\n        return C.DEFAULT\n\n\ndef get_env_patch(\n        venv: str,\n        language_version: str,\n) -> PatchesT:\n    patches: PatchesT = (\n        ('GEM_HOME', os.path.join(venv, 'gems')),\n        ('GEM_PATH', UNSET),\n        ('BUNDLE_IGNORE_CONFIG', '1'),\n    )\n    if language_version == 'system':\n        patches += (\n            (\n                'PATH', (\n                    os.path.join(venv, 'gems', 'bin'), os.pathsep,\n                    Var('PATH'),\n                ),\n            ),\n        )\n    else:  # pragma: win32 no cover\n        patches += (\n            ('RBENV_ROOT', venv),\n            (\n                'PATH', (\n                    os.path.join(venv, 'gems', 'bin'), os.pathsep,\n                    os.path.join(venv, 'shims'), os.pathsep,\n                    os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),\n                ),\n            ),\n        )\n    if language_version not in {'system', 'default'}:  # pragma: win32 no cover\n        patches += (('RBENV_VERSION', language_version),)\n\n    return patches\n\n\n@contextlib.contextmanager\ndef in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:\n    envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)\n    with envcontext(get_env_patch(envdir, version)):\n        yield\n\n\ndef _extract_resource(filename: str, dest: str) -> None:\n    with _resource_bytesio(filename) as bio:\n        with tarfile.open(fileobj=bio) as tf:\n            tf.extractall(dest)\n\n\ndef _install_rbenv(\n        prefix: Prefix,\n        version: str,\n) -> None:  # pragma: win32 no cover\n    envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)\n\n    _extract_resource('rbenv.tar.gz', prefix.path('.'))\n    shutil.move(prefix.path('rbenv'), envdir)\n\n    # Only install ruby-build if the version is specified\n    if version != C.DEFAULT:\n        plugins_dir = os.path.join(envdir, 'plugins')\n        _extract_resource('ruby-download.tar.gz', plugins_dir)\n        _extract_resource('ruby-build.tar.gz', plugins_dir)\n\n\ndef _install_ruby(\n        prefix: Prefix,\n        version: str,\n) -> None:  # pragma: win32 no cover\n    try:\n        lang_base.setup_cmd(prefix, ('rbenv', 'download', version))\n    except CalledProcessError:  # pragma: no cover (usually find with download)\n        # Failed to download from mirror for some reason, build it instead\n        lang_base.setup_cmd(prefix, ('rbenv', 'install', version))\n\n\ndef install_environment(\n        prefix: Prefix, version: str, additional_dependencies: Sequence[str],\n) -> None:\n    if version != 'system':  # pragma: win32 no cover\n        _install_rbenv(prefix, version)\n        with in_env(prefix, version):\n            # Need to call this before installing so rbenv's directories\n            # are set up\n            lang_base.setup_cmd(prefix, ('rbenv', 'init', '-'))\n            if version != C.DEFAULT:\n                _install_ruby(prefix, version)\n            # Need to call this after installing to set up the shims\n            lang_base.setup_cmd(prefix, ('rbenv', 'rehash'))\n\n    with in_env(prefix, version):\n        lang_base.setup_cmd(\n            prefix, ('gem', 'build', *prefix.star('.gemspec')),\n        )\n        lang_base.setup_cmd(\n            prefix,\n            (\n                'gem', 'install',\n                '--no-document', '--no-format-executable',\n                '--no-user-install',\n                *prefix.star('.gem'), *additional_dependencies,\n            ),\n        )\n", "path": "pre_commit/languages/ruby.py"}], "after_files": [{"content": "from __future__ import annotations\n\nimport contextlib\nimport functools\nimport importlib.resources\nimport os.path\nimport shutil\nimport tarfile\nfrom typing import Generator\nfrom typing import IO\nfrom typing import Sequence\n\nimport pre_commit.constants as C\nfrom pre_commit import lang_base\nfrom pre_commit.envcontext import envcontext\nfrom pre_commit.envcontext import PatchesT\nfrom pre_commit.envcontext import UNSET\nfrom pre_commit.envcontext import Var\nfrom pre_commit.prefix import Prefix\nfrom pre_commit.util import CalledProcessError\n\nENVIRONMENT_DIR = 'rbenv'\nhealth_check = lang_base.basic_health_check\nrun_hook = lang_base.basic_run_hook\n\n\ndef _resource_bytesio(filename: str) -> IO[bytes]:\n    return importlib.resources.open_binary('pre_commit.resources', filename)\n\n\n@functools.lru_cache(maxsize=1)\ndef get_default_version() -> str:\n    if all(lang_base.exe_exists(exe) for exe in ('ruby', 'gem')):\n        return 'system'\n    else:\n        return C.DEFAULT\n\n\ndef get_env_patch(\n        venv: str,\n        language_version: str,\n) -> PatchesT:\n    patches: PatchesT = (\n        ('GEM_HOME', os.path.join(venv, 'gems')),\n        ('GEM_PATH', UNSET),\n        ('BUNDLE_IGNORE_CONFIG', '1'),\n    )\n    if language_version == 'system':\n        patches += (\n            (\n                'PATH', (\n                    os.path.join(venv, 'gems', 'bin'), os.pathsep,\n                    Var('PATH'),\n                ),\n            ),\n        )\n    else:  # pragma: win32 no cover\n        patches += (\n            ('RBENV_ROOT', venv),\n            (\n                'PATH', (\n                    os.path.join(venv, 'gems', 'bin'), os.pathsep,\n                    os.path.join(venv, 'shims'), os.pathsep,\n                    os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),\n                ),\n            ),\n        )\n    if language_version not in {'system', 'default'}:  # pragma: win32 no cover\n        patches += (('RBENV_VERSION', language_version),)\n\n    return patches\n\n\n@contextlib.contextmanager\ndef in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:\n    envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)\n    with envcontext(get_env_patch(envdir, version)):\n        yield\n\n\ndef _extract_resource(filename: str, dest: str) -> None:\n    with _resource_bytesio(filename) as bio:\n        with tarfile.open(fileobj=bio) as tf:\n            tf.extractall(dest)\n\n\ndef _install_rbenv(\n        prefix: Prefix,\n        version: str,\n) -> None:  # pragma: win32 no cover\n    envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)\n\n    _extract_resource('rbenv.tar.gz', prefix.path('.'))\n    shutil.move(prefix.path('rbenv'), envdir)\n\n    # Only install ruby-build if the version is specified\n    if version != C.DEFAULT:\n        plugins_dir = os.path.join(envdir, 'plugins')\n        _extract_resource('ruby-download.tar.gz', plugins_dir)\n        _extract_resource('ruby-build.tar.gz', plugins_dir)\n\n\ndef _install_ruby(\n        prefix: Prefix,\n        version: str,\n) -> None:  # pragma: win32 no cover\n    try:\n        lang_base.setup_cmd(prefix, ('rbenv', 'download', version))\n    except CalledProcessError:  # pragma: no cover (usually find with download)\n        # Failed to download from mirror for some reason, build it instead\n        lang_base.setup_cmd(prefix, ('rbenv', 'install', version))\n\n\ndef install_environment(\n        prefix: Prefix, version: str, additional_dependencies: Sequence[str],\n) -> None:\n    envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)\n\n    if version != 'system':  # pragma: win32 no cover\n        _install_rbenv(prefix, version)\n        with in_env(prefix, version):\n            # Need to call this before installing so rbenv's directories\n            # are set up\n            lang_base.setup_cmd(prefix, ('rbenv', 'init', '-'))\n            if version != C.DEFAULT:\n                _install_ruby(prefix, version)\n            # Need to call this after installing to set up the shims\n            lang_base.setup_cmd(prefix, ('rbenv', 'rehash'))\n\n    with in_env(prefix, version):\n        lang_base.setup_cmd(\n            prefix, ('gem', 'build', *prefix.star('.gemspec')),\n        )\n        lang_base.setup_cmd(\n            prefix,\n            (\n                'gem', 'install',\n                '--no-document', '--no-format-executable',\n                '--no-user-install',\n                '--install-dir', os.path.join(envdir, 'gems'),\n                '--bindir', os.path.join(envdir, 'gems', 'bin'),\n                *prefix.star('.gem'), *additional_dependencies,\n            ),\n        )\n", "path": "pre_commit/languages/ruby.py"}]}