zestsoftware / zest.releaser

Python software releasing made easy and repeatable
https://zestreleaser.readthedocs.io
GNU General Public License v2.0
198 stars 62 forks source link

Support Hatchling as build backend #448

Open hcording opened 1 month ago

hcording commented 1 month ago

Hi, are there currently plans to support hatch as build backend? Since pyproject.toml is supported (#415), I think it would be an important feature to have. I have the following minimal code base:

pyproject.toml

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

[project]
name = "test"
dynamic = ["version"]
requires-python = ">=3.10"
license = "MIT"
authors = [
    { name = "Name", email ="mail@example.com" }
]
description = "Desc"
readme = "README.md"
classifiers = [
    "Programming Language :: Python :: 3.10",
    "Programming Language :: Python :: 3.11",
    "Programming Language :: Python :: 3.12",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
    "Development Status :: 1 - Planning",
    "Intended Audience :: Developers",
    "Topic :: Software Development :: Libraries :: Application Frameworks"
]
dependencies = [

]
keywords = ["kw"]

[project.urls]
"Homepage" = ""
"Bug Tracker" = ""

[tool.hatch.version]
path = "version.txt"
pattern = '^(?P<version>[^\n]+)'

version.txt

0.1.0

Running fullrelease, I get this output:

INFO: Starting prerelease.
Do you want to run check-manifest? (Y/n)? y
lists of files in version control and sdist match
Run pyroma on the package before tagging? (Y/n)? y
INFO: ------------------------------
INFO: Checking /media/mint/projects/test
INFO: Getting metadata for wheel...
INFO: Creating isolated environment: venv+pip...
INFO: Installing packages in isolated environment:
- hatchling
INFO: Getting build dependencies for wheel...
INFO: Getting metadata for wheel...
INFO: Found test
INFO: ------------------------------
INFO: Final rating: 10/10
INFO: Your cheese is so fresh most people think it's a cream: Mascarpone
INFO: ------------------------------
WARNING: No detectable version heading in the history file CHANGELOG
Changelog entries for version 0.1.0:

Enter version [0.1.0]: 
INFO: History file CHANGELOG updated.
INFO: The 'git diff':

diff --git a/CHANGELOG b/CHANGELOG
index e69de29..388fd1a 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -0,0 +1,2 @@
+0.1.0 (2024-05-26)
+------------------
\ No newline at end of file

OK to commit this (Y/n)? y
INFO: [main 02d102a] Preparing release 0.1.0
 1 file changed, 2 insertions(+)

INFO: Starting release.
Tag needed to proceed, you can use the following command:
git tag 0.1.0 -m 'Tagging 0.1.0'
Run this command (Y/n)? y

Check out the tag (for tweaks or pypi/distutils server upload) (Y/n)?  y
INFO: Doing a checkout...
Note: switching to '0.1.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>

Or undo this operation with:

git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 02d102a Preparing release 0.1.0

INFO: Tag checkout placed in /tmp/test-0.1.0-arx1eifp/gitclone
INFO: Changing to sub directory in tag checkout: /tmp/test-0.1.0-arx1eifp/gitclone
INFO: Making a source distribution of a fresh tag checkout (in /tmp/test-0.1.0-arx1eifp/gitclone).
INFO: Building sdist...

Traceback (most recent call last):
  File "/home/mint/miniforge3/envs/test/bin/fullrelease", line 8, in <module>
    sys.exit(main())
  File "/home/mint/miniforge3/envs/test/lib/python3.10/site-packages/zest/releaser/fullrelease.py", line 27, in main
    releaser.run()
  File "/home/mint/miniforge3/envs/test/lib/python3.10/site-packages/zest/releaser/baserelease.py", line 429, in run
    self.execute()
  File "/home/mint/miniforge3/envs/test/lib/python3.10/site-packages/zest/releaser/release.py", line 102, in execute
    self._release()
  File "/home/mint/miniforge3/envs/test/lib/python3.10/site-packages/zest/releaser/release.py", line 343, in _release
    self._upload_distributions(package)
  File "/home/mint/miniforge3/envs/test/lib/python3.10/site-packages/zest/releaser/release.py", line 151, in _upload_distributions
    builder.build("sdist", "./dist/")
  File "/home/mint/miniforge3/envs/test/lib/python3.10/site-packages/build/_builder.py", line 283, in build
    return self._call_backend(f'build_{distribution}', output_directory, config_settings, **kwargs)
  File "/home/mint/miniforge3/envs/test/lib/python3.10/site-packages/build/_builder.py", line 329, in _call_backend
    with self._handle_backend(hook_name):
  File "/home/mint/miniforge3/envs/test/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/mint/miniforge3/envs/test/lib/python3.10/site-packages/build/_builder.py", line 339, in _handle_backend
    raise BuildBackendException(
build._exceptions.BuildBackendException: Backend 'hatchling.build' is not available.

So it seems that hatchling.build is somehow not supported. Note that python -m build works, since pyroma succeeded beforehand.

Any help is appreciated!

mauritsvanrees commented 3 weeks ago

I think the way to do that, would be to create a virtualenv where you install both zest.releaser and hatchling.

mauritsvanrees commented 3 weeks ago

I tried your example. I need to add README.md and src/test/__init__.py, otherwise a plain python -m build . does not work.

Indeed then the prerelease part works, which also uses build and hatchling as called by pyroma, but the release part not. And you get basically the same error as if you would have done this:

$ bin/python -m build --no-isolation .
* Getting build dependencies for sdist...

Traceback (most recent call last):
  File "/Users/maurits/community/zest.releaser/lib/python3.11/site-packages/pyproject_hooks/_impl.py", line 321, in _call_hook
    raise BackendUnavailable(data.get('traceback', ''))
pyproject_hooks._impl.BackendUnavailable: Traceback (most recent call last):
  File "/Users/maurits/community/zest.releaser/lib/python3.11/site-packages/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
    obj = import_module(mod_path)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maurits/.pyenv/versions/3.11.7/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'hatchling'

ERROR Backend 'hatchling.build' is not available.

So release is probably calling build in non-isolated mode. We are doing it here and here.

pyroma seems to import build and call it.

mauritsvanrees commented 3 weeks ago

@hcording Can you try https://github.com/zestsoftware/zest.releaser/pull/449 ?