"env show --json" fails with an error if some plugin is not installed
I am working on integrating Hatch in PyCharm and encountered an issue with the --json flag, which breaks our integration.
steps to reproduce:
-
hatch-container plugin is not installed
-
git clone https://github.com/ofek/hatch-showcase
-
➜ hatch-showcase git:(master) hatch env show --json Environment
all.py3.7has unknown type: container -
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
@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.
@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?
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 Thanks for getting back. More info below: Steps to Reproduce
- Create a project that has the following in pyproject.toml:
[project.optional-dependencies]
test = ["pytest", "pytest-check", "pytest-cov"]
- Add a hatch.toml that includes:
[envs.hatch-test]
features = ["test"]
- 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)?
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
--jsonand--internalshows 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 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.