hatch: Problem with global `tool.hatch.build` table and sdist

Hello, I’ve encountered a problem while trying to build a project for sdist.

pyproject.toml:

[project]
name = "foo"
description = "foo != bar"
version = "0.1.4"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build]
packages = ["src/foo"]

Structure:

testprj/
├── pyproject.toml
├── .venv
├── src/
│   └── foo/
│       ├── __init__.py
│       └── __main__.py
└── dist/

Then I build the package: hatch build -t sdist -c.

When I run (within another env) pip install '<...>/dist/foo-0.1.4.tar.gz' pip reports the foo package as installed:

Package    Version
---------- -------
foo        0.1.4
pip        23.1.2
setuptools 65.5.0

However running python -c "import foo;print(foo)" throws an error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'foo'

and if I check site-packages/ there’s only foo-0.1.4.dist-info/ without the packages itself.

I figured out how to make it work, I just need to change the config a little:

[project]
name = "foo"
description = "foo != bar"
version = "0.1.4"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

-[tool.hatch.build]
+[tool.hatch.build.targets.sdist]
packages = ["src/foo"]

if I then install the package and run python -c "import foo;print(foo)", it works as expected.

I mark it as a “problem” because I’m not sure whether it’s a bug or not. I think it’d be nice if the global table worked because my project layout is the same across all the targets I may use, so there’s no reason for me to duplicate that information for each target.

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Comments: 19

Most upvoted comments

Yeap, now that you explained it looks like an obvious mistake by me haha ❤️

Basically, just be aware that the source distribution will often be how people such as re-distributors build your wheel so it should match the structure of your repository rather than the eventual installation (wheel).

You say currently, is it going to change later?

Probably yes to catch errors.

Looks perfect except you don’t need sources for the source distribution, in fact those two configurations are exactly the same.