From 94e67daadff87ff10976ad397b3332fb2bb93f33 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Melberg Loftsson <23453691+valentinoli@users.noreply.github.com> Date: Fri, 18 Jul 2025 22:51:47 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20extract=20relevant=20output?= =?UTF-8?q?=20from=20`hatch=20env=20show=20--json`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If `result.stdout` contains something more than the JSON output of all the environments, such as logs from Hatch plugins/hooks, this will result in JSONDecodeError. We must extract the relevant output to prevent such cases. --- hatch_pip_compile/cli.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index 3ddffbb..673b198 100644 --- a/hatch_pip_compile/cli.py +++ b/hatch_pip_compile/cli.py @@ -139,7 +139,12 @@ def _get_supported_environments(cls) -> set[str]: capture_output=True, check=True, ) - environment_dict: dict[str, Any] = json.loads(result.stdout) + # Extract the relevant JSON output by extracting the last line in stdout, + # to prevent JSONDecodeError. + # When present, previous lines might be logs from Hatch plugins, + # like custom environment collectors. + envs_raw = result.stdout.strip().rsplit(b"\n", maxsplit=1)[-1] + environment_dict: dict[str, Any] = json.loads(envs_raw) return { key for key, value in environment_dict.items() if value.get("type") == "pip-compile" } From 78248efb3adffb15846c9f32ebdfd1546f839207 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Melberg Loftsson <23453691+valentinoli@users.noreply.github.com> Date: Fri, 18 Jul 2025 22:56:20 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20ruff=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hatch_pip_compile/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index 673b198..da04d05 100644 --- a/hatch_pip_compile/cli.py +++ b/hatch_pip_compile/cli.py @@ -140,10 +140,10 @@ def _get_supported_environments(cls) -> set[str]: check=True, ) # Extract the relevant JSON output by extracting the last line in stdout, - # to prevent JSONDecodeError. + # to prevent JSONDecodeError. # When present, previous lines might be logs from Hatch plugins, # like custom environment collectors. - envs_raw = result.stdout.strip().rsplit(b"\n", maxsplit=1)[-1] + envs_raw = result.stdout.strip().rsplit(b"\n", maxsplit=1)[-1] environment_dict: dict[str, Any] = json.loads(envs_raw) return { key for key, value in environment_dict.items() if value.get("type") == "pip-compile"