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

"hatch env remove" leaves partial environment in place #65

Closed TobyGiacometti closed 7 months ago

TobyGiacometti commented 7 months ago

Hatch environments created with hatch-pip-compile are not fully removed when running hatch env prune or hatch env remove. When entering the environment (for example with hatch shell) after running removal commands, the environment is not recreated and we enter a partial environment (no dependencies are installed except for Python itself).

juftin commented 7 months ago

Hmm... it seems like hatch env prune isn't doing exactly what I would expect it to while hatch env remove <envName> is:

❯ ls .venv/
hatch-pip-compile
❯ hatch env prune
❯ ls .venv/
hatch-pip-compile
❯ hatch env remove default
❯ ls .venv/
ls: .venv/: No such file or directory

Is this something you're able to replicate when using a virtual environment type instead of pip-compile type?

TobyGiacometti commented 7 months ago

Thanks for checking!

You are right, hatch env remove works most of the time, but there is an edge case where it fails as well. I must have encountered that edge case during my initial test. You can reproduce it as follows:

[env]
requires = [ "hatch-pip-compile" ]

[envs.default]
type = "pip-compile"
pip-compile-constraint = "default"
dependencies = [ "pyinfra>=2.9" ]

[envs.dev]
extra-dependencies = [ "black" ]
> hatch --env default shell
(default) > pyinfra
Usage: pyinfra [OPTIONS] INVENTORY OPERATIONS...
Try 'pyinfra --help' for help.

Error: Missing argument 'INVENTORY'.
(default) > exit
> hatch --env dev shell
(dev) > exit
> hatch env remove default
> hatch env remove dev
> hatch --env default shell
(default) > pyinfra
bash: pyinfra: command not found

The hatch env remove dev command removes the dev environment, but at the same time creates a broken default environment. While trying to reproduce this edge case, I noticed that the pip-compile-constraint = "default" setting is responsible for the issue. When this setting is not present, hatch env prune works as well.

juftin commented 7 months ago

Oh very interesting! I think this is actually the same as https://github.com/juftin/hatch-pip-compile/issues/63

In order for the pip-compile-constraint feature to work hatch-pip-compile has to check inside the constraint environment from whichever environment you're operating from. This is inadvertently creating the environment but not actually installing the dependencies into it.

I'll keep digging and get this fixed up. Thanks very much for reporting the issue.

juftin commented 7 months ago

:tada: This issue has been resolved in version 1.10.1 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

TobyGiacometti commented 7 months ago

Thanks @juftin, working as expected now 👍