hatch icon indicating copy to clipboard operation
hatch copied to clipboard

version-file build hooks seems to fail for setuptools-scm>=9.0.0

Open mpasson opened this issue 5 months ago • 1 comments

I'm not sure this is the correct place for this issue, but it seems that building a python package containing the version-file build hooks fails for versions of setuptools-scm greater-equal to 9.0.0.

.venv ❯ uv build
Building source distribution...
Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "/Users/marcopassoni/.cache/uv/builds-v0/.tmpX6Nh67/lib/python3.12/site-packages/hatchling/build.py", line 34, in build_sdist
    return os.path.basename(next(builder.build(directory=sdist_directory, versions=['standard'])))
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/marcopassoni/.cache/uv/builds-v0/.tmpX6Nh67/lib/python3.12/site-packages/hatchling/builders/plugin/interface.py", line 147, in build
    build_hook.initialize(version, build_data)
  File "/Users/marcopassoni/.cache/uv/builds-v0/.tmpX6Nh67/lib/python3.12/site-packages/hatch_vcs/build_hook.py", line 35, in initialize
    dump_version(self.root, self.metadata.version, self.config_version_file, **kwargs)
  File "/Users/marcopassoni/.cache/uv/builds-v0/.tmpX6Nh67/lib/python3.12/site-packages/setuptools_scm/_integration/dump_version.py", line 77, in dump_version
    write_version_to_path(
  File "/Users/marcopassoni/.cache/uv/builds-v0/.tmpX6Nh67/lib/python3.12/site-packages/setuptools_scm/_integration/dump_version.py", line 111, in write_version_to_path
    content = final_template.format(version=version, version_tuple=version_tuple)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'scm_version'
  × Failed to build `/Users/marcopassoni/Documents/MyPrograms/<package>`
  ├─▶ The build backend returned an error
  ╰─▶ Call to `hatchling.build.build_sdist` failed (exit status: 1)
      hint: This usually indicates a problem with the package or the build environment.

The relevant parts of the pyproject.toml are:

[project]
name = <package>
requires-python = ">=3.12"
dynamic = ['version']

[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[tool.hatch.version]
source = 'vcs'

[tool.hatch.build.hooks.vcs]
version-file = "src/<package>/_pinned_version.py"

Restricting stetuptools_scm to versions previous to 9.0.0:

requires = ["hatchling", "hatch-vcs", "setuptools-scm<9.0.0"]

solves the problem.

mpasson avatar Aug 04 '25 10:08 mpasson

Bugfix is in progress: https://github.com/pypa/setuptools-scm/issues/1180#issuecomment-3150183900

I guess this not a hatch issue. setuptools_scm v9.0.0 should be avoided - chances are it will be yanked once v9.0.1 is out.

DimitriPapadopoulos avatar Aug 04 '25 11:08 DimitriPapadopoulos