juftin / hatch-pip-compile

hatch plugin to use pip-compile (or uv) to manage project dependencies and lockfiles
http://juftin.com/hatch-pip-compile/
MIT License
74 stars 3 forks source link

"PIP_COMPILE_DISABLE" does not affect exit code #66

Closed TobyGiacometti closed 7 months ago

TobyGiacometti commented 7 months ago

When the environment variable PIP_COMPILE_DISABLE is set to 1, and a lock file update is attempted, the exit code of the affected command is 0. For example: rm requirements-dev.txt; PIP_COMPILE_DISABLE=1 hatch --env dev run black /path/to/file exits with the status code 0. This could mask errors in CI/CD environments.

juftin commented 7 months ago

Hmmm... I'm not able to replicate this. I'm getting an exit code of 1 due to the raised error: https://github.com/juftin/hatch-pip-compile/blob/46d698dcf943ea6c8d55320c09c12f0283666052/hatch_pip_compile/plugin.py#L139-L142

rm requirements.txt
PIP_COMPILE_DISABLE=1 hatch --env default run python --version
image

Are you able to tell what version of hatch / hatch-pip-compile you're using? And what OS are you on?

juftin commented 7 months ago

Oh... I think I found something here. Is your dev environment detached @TobyGiacometti?

I've added a test here for this test case that only fails on a detached environment: https://github.com/juftin/hatch-pip-compile/pull/67/commits/ef73cf29a785b535582f92a2666d157712290036

TobyGiacometti commented 7 months ago

It happens with all environments, whether they are detached or not makes no difference in my case. I am using Hatch 1.9.3 and hatch-pip-compile 1.10.0 on Linux. Also tried with hatch-pip-compile configuration defaults, same thing.

juftin commented 7 months ago

Could you share a pyproject.toml / link to the project by chance? I can test inside docker which should hopefully allow me to replicate the issue.

I'm not actually able to replicate the issue in new unit tests

TobyGiacometti commented 7 months ago

Thank you for your continued efforts! I can reproduce the issue using following script:

mkdir /tmp/hatch-pip-compile-test
cd "$_"
cat <<'EOF' >hatch.toml
[env]
requires = [ "hatch-pip-compile" ]

[envs.default]
type = "pip-compile"
dependencies = [ "pyinfra>=2.9" ]
EOF
PIP_COMPILE_DISABLE=1 hatch -v env run --env default -- python --version
printf "%s\n" "$?"
juftin commented 7 months ago

🤷 I'm not able to replicate this in docker either, it throws an error with an exit code of 1 as expected again. Can you run the following commands and let me know your output?

docker run --rm -it python:3.11 /bin/bash
pip install hatch
mkdir /tmp/hatch-pip-compile-test
cd "$_"
cat <<'EOF' >hatch.toml
[env]
requires = [ "hatch-pip-compile" ]

[envs.default]
type = "pip-compile"
dependencies = [ "pyinfra>=2.9" ]
EOF
PIP_COMPILE_DISABLE=1 hatch -v env run --env default -- python --version
printf "%s\n" "$?"
console output

```console [notice] A new release of pip is available: 23.2.1 -> 23.3.2 [notice] To update, run: pip install --upgrade pip No config file found, creating one with default settings now... Success! Please see `hatch config`. Collecting hatch-pip-compile Obtaining dependency information for hatch-pip-compile from https://files.pythonhosted.org/packages/62/dd/50b68457b875f177b1253c1c104d60658a53607722d7d31266be86c3562f/hatch_pip_compile-1.10.0-py3-none-any.whl.metadata Downloading hatch_pip_compile-1.10.0-py3-none-any.whl.metadata (17 kB) Requirement already satisfied: click in /usr/local/lib/python3.11/site-packages (from hatch-pip-compile) (8.1.7) Requirement already satisfied: hatch<2,>=1.7.0 in /usr/local/lib/python3.11/site-packages (from hatch-pip-compile) (1.9.3) Collecting pip-tools>=6 (from hatch-pip-compile) Obtaining dependency information for pip-tools>=6 from https://files.pythonhosted.org/packages/e8/df/47e6267c6b5cdae867adbdd84b437393e6202ce4322de0a5e0b92960e1d6/pip_tools-7.3.0-py3-none-any.whl.metadata Downloading pip_tools-7.3.0-py3-none-any.whl.metadata (23 kB) Requirement already satisfied: rich in /usr/local/lib/python3.11/site-packages (from hatch-pip-compile) (13.7.0) Requirement already satisfied: hatchling>=1.21.0 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (1.21.1) Requirement already satisfied: httpx>=0.22.0 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (0.26.0) Requirement already satisfied: hyperlink>=21.0.0 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (21.0.0) Requirement already satisfied: keyring>=23.5.0 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (24.3.0) Requirement already satisfied: packaging>=21.3 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (23.2) Requirement already satisfied: pexpect~=4.8 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (4.9.0) Requirement already satisfied: platformdirs>=2.5.0 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (4.2.0) Requirement already satisfied: shellingham>=1.4.0 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (1.5.4) Requirement already satisfied: tomli-w>=1.0 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (1.0.0) Requirement already satisfied: tomlkit>=0.11.1 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (0.12.3) Requirement already satisfied: userpath~=1.7 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (1.9.1) Requirement already satisfied: virtualenv>=20.16.2 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (20.25.0) Requirement already satisfied: zstandard<1 in /usr/local/lib/python3.11/site-packages (from hatch<2,>=1.7.0->hatch-pip-compile) (0.22.0) Collecting build (from pip-tools>=6->hatch-pip-compile) Obtaining dependency information for build from https://files.pythonhosted.org/packages/93/dd/b464b728b866aaa62785a609e0dd8c72201d62c5f7c53e7c20f4dceb085f/build-1.0.3-py3-none-any.whl.metadata Downloading build-1.0.3-py3-none-any.whl.metadata (4.2 kB) Requirement already satisfied: pip>=22.2 in /usr/local/lib/python3.11/site-packages (from pip-tools>=6->hatch-pip-compile) (23.2.1) Requirement already satisfied: setuptools in /usr/local/lib/python3.11/site-packages (from pip-tools>=6->hatch-pip-compile) (65.5.1) Requirement already satisfied: wheel in /usr/local/lib/python3.11/site-packages (from pip-tools>=6->hatch-pip-compile) (0.42.0) Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.11/site-packages (from rich->hatch-pip-compile) (3.0.0) Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.11/site-packages (from rich->hatch-pip-compile) (2.17.2) Requirement already satisfied: editables>=0.3 in /usr/local/lib/python3.11/site-packages (from hatchling>=1.21.0->hatch<2,>=1.7.0->hatch-pip-compile) (0.5) Requirement already satisfied: pathspec>=0.10.1 in /usr/local/lib/python3.11/site-packages (from hatchling>=1.21.0->hatch<2,>=1.7.0->hatch-pip-compile) (0.12.1) Requirement already satisfied: pluggy>=1.0.0 in /usr/local/lib/python3.11/site-packages (from hatchling>=1.21.0->hatch<2,>=1.7.0->hatch-pip-compile) (1.4.0) Requirement already satisfied: trove-classifiers in /usr/local/lib/python3.11/site-packages (from hatchling>=1.21.0->hatch<2,>=1.7.0->hatch-pip-compile) (2024.1.8) Requirement already satisfied: anyio in /usr/local/lib/python3.11/site-packages (from httpx>=0.22.0->hatch<2,>=1.7.0->hatch-pip-compile) (4.2.0) Requirement already satisfied: certifi in /usr/local/lib/python3.11/site-packages (from httpx>=0.22.0->hatch<2,>=1.7.0->hatch-pip-compile) (2023.11.17) Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx>=0.22.0->hatch<2,>=1.7.0->hatch-pip-compile) (1.0.2) Requirement already satisfied: idna in /usr/local/lib/python3.11/site-packages (from httpx>=0.22.0->hatch<2,>=1.7.0->hatch-pip-compile) (3.6) Requirement already satisfied: sniffio in /usr/local/lib/python3.11/site-packages (from httpx>=0.22.0->hatch<2,>=1.7.0->hatch-pip-compile) (1.3.0) Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx>=0.22.0->hatch<2,>=1.7.0->hatch-pip-compile) (0.14.0) Requirement already satisfied: jaraco.classes in /usr/local/lib/python3.11/site-packages (from keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (3.3.0) Requirement already satisfied: importlib-metadata>=4.11.4 in /usr/local/lib/python3.11/site-packages (from keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (7.0.1) Requirement already satisfied: SecretStorage>=3.2 in /usr/local/lib/python3.11/site-packages (from keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (3.3.3) Requirement already satisfied: jeepney>=0.4.2 in /usr/local/lib/python3.11/site-packages (from keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (0.8.0) Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich->hatch-pip-compile) (0.1.2) Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.11/site-packages (from pexpect~=4.8->hatch<2,>=1.7.0->hatch-pip-compile) (0.7.0) Requirement already satisfied: distlib<1,>=0.3.7 in /usr/local/lib/python3.11/site-packages (from virtualenv>=20.16.2->hatch<2,>=1.7.0->hatch-pip-compile) (0.3.8) Requirement already satisfied: filelock<4,>=3.12.2 in /usr/local/lib/python3.11/site-packages (from virtualenv>=20.16.2->hatch<2,>=1.7.0->hatch-pip-compile) (3.13.1) Collecting pyproject_hooks (from build->pip-tools>=6->hatch-pip-compile) Downloading pyproject_hooks-1.0.0-py3-none-any.whl (9.3 kB) Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.11/site-packages (from importlib-metadata>=4.11.4->keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (3.17.0) Requirement already satisfied: cryptography>=2.0 in /usr/local/lib/python3.11/site-packages (from SecretStorage>=3.2->keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (42.0.2) Requirement already satisfied: more-itertools in /usr/local/lib/python3.11/site-packages (from jaraco.classes->keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (10.2.0) Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.11/site-packages (from cryptography>=2.0->SecretStorage>=3.2->keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (1.16.0) Requirement already satisfied: pycparser in /usr/local/lib/python3.11/site-packages (from cffi>=1.12->cryptography>=2.0->SecretStorage>=3.2->keyring>=23.5.0->hatch<2,>=1.7.0->hatch-pip-compile) (2.21) Downloading hatch_pip_compile-1.10.0-py3-none-any.whl (16 kB) Downloading pip_tools-7.3.0-py3-none-any.whl (57 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.4/57.4 kB 1.2 MB/s eta 0:00:00 Downloading build-1.0.3-py3-none-any.whl (18 kB) Installing collected packages: pyproject_hooks, build, pip-tools, hatch-pip-compile Successfully installed build-1.0.3 hatch-pip-compile-1.10.0 pip-tools-7.3.0 pyproject_hooks-1.0.0 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv Finished syncing environment plugin requirements ────────────────────────────────────────────────────────────────────────────────────────────────────── default ─────────────────────────────────────────────────────────────────────────────────────────────────────── Finished creating environment: default Collecting pip-tools Downloading pip_tools-7.3.0-py3-none-any.whl.metadata (23 kB) Collecting build (from pip-tools) Downloading build-1.0.3-py3-none-any.whl.metadata (4.2 kB) Collecting click>=8 (from pip-tools) Downloading click-8.1.7-py3-none-any.whl.metadata (3.0 kB) Requirement already satisfied: pip>=22.2 in /root/.local/share/hatch/env/pip-compile/hatch-pip-compile-test/ahALYemN/hatch-pip-compile-test/lib/python3.11/site-packages (from pip-tools) (23.3.1) Requirement already satisfied: setuptools in /root/.local/share/hatch/env/pip-compile/hatch-pip-compile-test/ahALYemN/hatch-pip-compile-test/lib/python3.11/site-packages (from pip-tools) (69.0.2) Requirement already satisfied: wheel in /root/.local/share/hatch/env/pip-compile/hatch-pip-compile-test/ahALYemN/hatch-pip-compile-test/lib/python3.11/site-packages (from pip-tools) (0.42.0) Collecting packaging>=19.0 (from build->pip-tools) Downloading packaging-23.2-py3-none-any.whl.metadata (3.2 kB) Collecting pyproject_hooks (from build->pip-tools) Downloading pyproject_hooks-1.0.0-py3-none-any.whl (9.3 kB) Downloading pip_tools-7.3.0-py3-none-any.whl (57 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.4/57.4 kB 1.2 MB/s eta 0:00:00 Downloading click-8.1.7-py3-none-any.whl (97 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 5.2 MB/s eta 0:00:00 Downloading build-1.0.3-py3-none-any.whl (18 kB) Downloading packaging-23.2-py3-none-any.whl (53 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.0/53.0 kB 7.6 MB/s eta 0:00:00 Installing collected packages: pyproject_hooks, packaging, click, build, pip-tools Successfully installed build-1.0.3 click-8.1.7 packaging-23.2 pip-tools-7.3.0 pyproject_hooks-1.0.0 ╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮ │ /usr/local/lib/python3.11/site-packages/hatch/cli/__init__.py:221 in main │ │ │ │ 218 │ │ 219 def main(): # no cov │ │ 220 │ try: │ │ ❱ 221 │ │ return hatch(prog_name='hatch', windows_expand_args=False) │ │ 222 │ except Exception: # noqa: BLE001 │ │ 223 │ │ from rich.console import Console │ │ 224 │ │ │ │ /usr/local/lib/python3.11/site-packages/click/core.py:1157 in __call__ │ │ │ │ /usr/local/lib/python3.11/site-packages/click/core.py:1078 in main │ │ │ │ /usr/local/lib/python3.11/site-packages/click/core.py:1688 in invoke │ │ │ │ /usr/local/lib/python3.11/site-packages/click/core.py:1688 in invoke │ │ │ │ /usr/local/lib/python3.11/site-packages/click/core.py:1434 in invoke │ │ │ │ /usr/local/lib/python3.11/site-packages/click/core.py:783 in invoke │ │ │ │ /usr/local/lib/python3.11/site-packages/click/decorators.py:45 in new_func │ │ │ │ /usr/local/lib/python3.11/site-packages/hatch/cli/env/run.py:180 in run │ │ │ │ 177 │ │ │ if env_name == 'system': │ │ 178 │ │ │ │ environment.exists = lambda: True │ │ 179 │ │ │ │ │ ❱ 180 │ │ │ app.prepare_environment(environment) │ │ 181 │ │ │ app.run_shell_commands( │ │ 182 │ │ │ │ environment, │ │ 183 │ │ │ │ [environment.join_command_args(args)], │ │ │ │ /usr/local/lib/python3.11/site-packages/hatch/cli/application.py:107 in prepare_environment │ │ │ │ 104 │ │ │ │ │ with self.status('Running post-installation commands'): │ │ 105 │ │ │ │ │ │ self.run_shell_commands(environment, environment.post_install_co │ │ 106 │ │ │ │ ❱ 107 │ │ new_dep_hash = environment.dependency_hash() │ │ 108 │ │ current_dep_hash = self.env_metadata.dependency_hash(environment) │ │ 109 │ │ if new_dep_hash != current_dep_hash: │ │ 110 │ │ │ with self.status('Checking dependencies'): │ │ │ │ /usr/local/lib/python3.11/site-packages/hatch_pip_compile/plugin.py:97 in dependency_hash │ │ │ │ 94 │ │ """ │ │ 95 │ │ Get the dependency hash │ │ 96 │ │ """ │ │ ❱ 97 │ │ self.run_pip_compile() │ │ 98 │ │ hatch_hash = super().dependency_hash() │ │ 99 │ │ if not self.dependencies: │ │ 100 │ │ │ return hatch_hash │ │ │ │ /usr/local/lib/python3.11/site-packages/hatch_pip_compile/plugin.py:129 in run_pip_compile │ │ │ │ 126 │ │ │ │ │ _ = self.piptools_lock.compare_python_versions( │ │ 127 │ │ │ │ │ │ verbose=self.config.get("pip-compile-verbose", None) │ │ 128 │ │ │ │ │ ) │ │ ❱ 129 │ │ │ │ self.pip_compile_cli() │ │ 130 │ │ │ 131 │ def pip_compile_cli(self) -> None: │ │ 132 │ │ """ │ │ │ │ /usr/local/lib/python3.11/site-packages/hatch_pip_compile/plugin.py:142 in pip_compile_cli │ │ │ │ 139 │ │ no_compile = bool(os.getenv("PIP_COMPILE_DISABLE")) │ │ 140 │ │ if no_compile: │ │ 141 │ │ │ msg = "hatch-pip-compile is disabled but attempted to run a lockfile update. │ │ ❱ 142 │ │ │ raise HatchPipCompileError(msg) │ │ 143 │ │ upgrade = bool(os.getenv("PIP_COMPILE_UPGRADE")) │ │ 144 │ │ upgrade_packages = os.getenv("PIP_COMPILE_UPGRADE_PACKAGE") or None │ │ 145 │ │ upgrade_args = [] │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ HatchPipCompileError: hatch-pip-compile is disabled but attempted to run a lockfile update. 1 root@c1328f7fe265:/tmp/hatch-pip-compile-test# ```

image
TobyGiacometti commented 7 months ago

I did some more digging. Turns out that this issue only occurs when using the standalone binaries from https://hatch.pypa.io/latest/install/. Will report this issue upstream.

ofek commented 4 months ago

For posterity, this was an error in Hatch: https://github.com/pypa/hatch/pull/1389