hatch icon indicating copy to clipboard operation
hatch copied to clipboard

`post-install-commands` are run before installing environment dependencies

Open dhdaines opened this issue 1 year ago • 6 comments

I often would like to run something like mypy --install-types or pre-commit install when setting up a particular environment. Unfortunately, this does not work. For example if you put this in your pyproject.toml:

[tool.hatch.envs.dev]
dependencies = [ "mypy" ]
post-install-commands = [ "mypy --install-types --non-interactive src/mymodule" ]

Then you get this error:

/bin/sh: 1: mypy: not found
Failed with exit code: 127

Thankfully there is a workaround - if you add an optional-dependencies feature with these environment-specific dependencies, then they do get installed before post-install-commands are run, e.g.:

[project.optional-dependencies]
dev = [ "mypy" ]
[tool.hatch.envs.dev]
features = [ "dev" ]
post-install-commands = [ "mypy --install-types --non-interactive src/mymodule" ]

This seems unintuitive to me though. Hatch should install both the package and the environment dependencies before running post-install-commands as this seems like what the user would expect.

dhdaines avatar Mar 28 '24 20:03 dhdaines

Seems reasonable, I will do this.

ofek avatar Mar 28 '24 20:03 ofek

I haven't forgotten but this won't make it in the next minor release. Also FYI the way I will approach this is adding new commands e.g. for setup.

ofek avatar Apr 22 '24 02:04 ofek

Makes sense!

dhdaines avatar Apr 22 '24 11:04 dhdaines

Also just want to add a note to anyone who reads this: running mypy --install-types when setting up an environment is a bad example, don't do this :) since if your type checking fails, you won't have an environment! (you could add || true if you really want to)

dhdaines avatar Apr 22 '24 13:04 dhdaines

Is there any update on this? We are running into this issue atm.

I will look to try to work around it for now.

Mathieson avatar Feb 05 '25 18:02 Mathieson

Hi, I also run into this problem. Is it planned to be fixed in the next release?

jogehl avatar May 26 '25 15:05 jogehl