hatch icon indicating copy to clipboard operation
hatch copied to clipboard

"env show --json" fails with an error if some plugin is not installed

Open vlegchilkin opened this issue 6 months ago • 3 comments

I am working on integrating Hatch in PyCharm and encountered an issue with the --json flag, which breaks our integration.

steps to reproduce:

  1. hatch-container plugin is not installed

  2. git clone https://github.com/ofek/hatch-showcase

  3. ➜ hatch-showcase git:(master) hatch env show --json Environment all.py3.7 has unknown type: container

  4. hatch-showcase git:(master) hatch env show Standalone ┏━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃ Name ┃ Type ┃ Dependencies ┃ Scripts ┃ ┡━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩ │ default │ virtual │ coverage[toml]>=6.5 │ cov │ │ │ │ pytest │ cov-report │ │ │ │ │ test │ │ │ │ │ test-cov │ ├─────────┼─────────┼─────────────────────┼────────────┤ │ lint │ virtual │ black>=22.10.0 │ all │ │ │ │ mypy>=0.991 │ fmt │ │ │ │ ruff>=0.0.166 │ style │ │ │ │ │ typing │ └─────────┴─────────┴─────────────────────┴────────────┘ Matrices ┏━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃ Name ┃ Type ┃ Envs ┃ Dependencies ┃ Scripts ┃ ┡━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩ │ all │ container │ all.py3.7 │ coverage[toml]>=6.5 │ cov │ │ │ │ all.py3.8 │ pytest │ cov-report │ │ │ │ all.py3.9 │ │ test │ │ │ │ all.py3.10 │ │ test-cov │ │ │ │ all.py3.11 │ │ │ └──────┴───────────┴────────────┴─────────────────────┴────────────┘

Expected Behaviour:

Hatch shows environments in json format, at least available ones

vlegchilkin avatar Jun 24 '25 09:06 vlegchilkin

@ofek Is it possible to make hatch env show --json work for container environments without having to install hatch-containers (given that hatch env show works without having to install hatch-containers)? This change prevents the completion of Hatch integration work in PyCharm.

mcarans avatar Jul 22 '25 20:07 mcarans

@cjames23 Thanks for becoming Hatch's new maintainer!

Is this issue which affects PyCharm's integration of Hatch something you or @ofek could look into?

mcarans avatar Dec 08 '25 19:12 mcarans

We are working through some other implementations at the moment. Can you explain more about what exactly you are trying to integrate into PyCharm that this is required? PyCharm already supports hatch environments.

cjames23 avatar Dec 10 '25 06:12 cjames23

@cjames23 Thanks for getting back. More info below: Steps to Reproduce

  1. Create a project that has the following in pyproject.toml:
[project.optional-dependencies]
test = ["pytest", "pytest-check", "pytest-cov"]
  1. Add a hatch.toml that includes:
[envs.hatch-test] 
features = ["test"]
  1. Add Hatch interpreter in PyCharm

Expected Result

"hatch-test" environment shows up under the Environment dropdown. Once selected and packages are installed, they should include pytest, pytest-check and pytest-cov since "test" is a feature of envs.hatch-test

Actual Result Only "default" shows up under the Environment dropdown

see https://youtrack.jetbrains.com/projects/PY/issues/PY-81270/

This seems to be because Hatch treats the environment as internal

@ofek said "I think it would be reasonable to expose those environments since many folks are satisfied, at least for testing, with the environments and wouldn't create their own. You can access those by using the -i/--internal flag."

@vlegchilkin who is a JetBrains dev replied "We want to use the JSON API to get both internal and external environments in a single call, but currently it has limitations."

Is it possible to make hatch env show --json work for container environments without having to install hatch-containers (given that hatch env show works without having to install hatch-containers)?

mcarans avatar Dec 14 '25 22:12 mcarans

Is it possible to make hatch env show --json work for container environments without having to install hatch-containers (given that hatch env show works without having to install hatch-containers)

What do you mean by this? hatch-containers is a plugin to create containerized environments, it is not a part of hatch core. They maintain separation of concerns as core and a plugin, despite having Ofek as the author of the plugin. Without that plugin installed hatch core has no concept of a container environment. Ofek and I can discuss add container environment support into core but I would be concerned about trying to make the core of hatch do too many things, the power of hatch is its plugin mechanism for extensibility.

"We want to use the JSON API to get both internal and external environments in a single call, but currently it has limitations. Using the above minimal reproducible example and calling hatch env show with both --json and --internal shows the following

{"default":{"type":"virtual"},"types":{"extra-dependencies":["mypy>=1.0.0"],"scripts":{"check":["mypy --install-types --non-interactive src/hatchtesting tests"]},"type":"virtual"},"hatch-build":{"skip-install":true,"builder":true,"installer":"uv","type":"virtual"},"hatch-static-analysis":{"skip-install":true,"installer":"uv","dependencies":["ruff==0.13.2"],"scripts":{"format-check":["ruff format --check --diff ."],"format-fix":["ruff format ."],"lint-check":["ruff check ."],"lint-fix":["ruff check --fix ."]},"type":"virtual"},"hatch-test.py3.14":{"installer":"uv","dependencies":["coverage-enable-subprocess==1.0","coverage[toml]~=7.11","pytest~=9.0","pytest-mock~=3.12","pytest-randomly~=3.15","pytest-rerunfailures~=14.0","pytest-xdist[psutil]~=3.5"],"scripts":{"run":["pytest"],"run-cov":["coverage run -m pytest"],"cov-combine":["coverage combine"],"cov-report":["coverage report"]},"type":"virtual","python":"3.14"},"hatch-test.py3.13":{"installer":"uv","dependencies":["coverage-enable-subprocess==1.0","coverage[toml]~=7.11","pytest~=9.0","pytest-mock~=3.12","pytest-randomly~=3.15","pytest-rerunfailures~=14.0","pytest-xdist[psutil]~=3.5"],"scripts":{"run":["pytest"],"run-cov":["coverage run -m pytest"],"cov-combine":["coverage combine"],"cov-report":["coverage report"]},"type":"virtual","python":"3.13"},"hatch-test.py3.12":{"installer":"uv","dependencies":["coverage-enable-subprocess==1.0","coverage[toml]~=7.11","pytest~=9.0","pytest-mock~=3.12","pytest-randomly~=3.15","pytest-rerunfailures~=14.0","pytest-xdist[psutil]~=3.5"],"scripts":{"run":["pytest"],"run-cov":["coverage run -m pytest"],"cov-combine":["coverage combine"],"cov-report":["coverage report"]},"type":"virtual","python":"3.12"},"hatch-test.py3.11":{"installer":"uv","dependencies":["coverage-enable-subprocess==1.0","coverage[toml]~=7.11","pytest~=9.0","pytest-mock~=3.12","pytest-randomly~=3.15","pytest-rerunfailures~=14.0","pytest-xdist[psutil]~=3.5"],"scripts":{"run":["pytest"],"run-cov":["coverage run -m pytest"],"cov-combine":["coverage combine"],"cov-report":["coverage report"]},"type":"virtual","python":"3.11"},"hatch-test.py3.10":{"installer":"uv","dependencies":["coverage-enable-subprocess==1.0","coverage[toml]~=7.11","pytest~=9.0","pytest-mock~=3.12","pytest-randomly~=3.15","pytest-rerunfailures~=14.0","pytest-xdist[psutil]~=3.5"],"scripts":{"run":["pytest"],"run-cov":["coverage run -m pytest"],"cov-combine":["coverage combine"],"cov-report":["coverage report"]},"type":"virtual","python":"3.10"},"hatch-uv":{"skip-install":true,"installer":"uv","type":"virtual"}}

Is the issue here that the optional dependencies defined as features do not show up in the dependencies list? Because both flags do show project level environments as well as internals in one call as I defined types as an environment in my pyproject.toml for the test.

cjames23 avatar Dec 15 '25 02:12 cjames23

@cjames23 Please try following the steps (taken from the OP and expanded):

git clone https://github.com/ofek/hatch-showcase
cd hatch-showcase
virtualenv venv
source venv/bin/activate 
pip install hatch
hatch env show

It shows all environments

hatch env show --json

Fails: with Environment all.py3.7 has unknown type: container

I think the issue for the PyCharm dev(s) is that they need hatch env show --json to work out of the box.

mcarans avatar Dec 15 '25 07:12 mcarans