-
Notifications
You must be signed in to change notification settings - Fork 24
Description
🧩 Problem
In some corporate environments, source control metadata (like .git) is stripped or unavailable during the packaging process. This causes setuptools-scm to fail when trying to resolve the version, which in turn can break builds using hatchling and other build backends.
✅ Proposed Solution
Introduce a mechanism that allows suppressing setuptools-scm lookup failures and fallback to a predefined static version instead. This fallback enables artifact packaging to proceed without SCM access, while still benefiting from dynamic versioning during development.
💡 Use Case
This pattern is particularly helpful in corporate or air-gapped environments where:
- SCM metadata is not available during artifact packaging.
- Artifactories or CI systems set the version downstream
- Developers still want dynamic versioning from
setuptools-scmwhen working locally.
🧪 Minimal Working Example
"""Dynamic hatchling code during artifact build."""
from __future__ import annotations
from contextlib import suppress
from pathlib import Path
from typing import Any
from hatchling.metadata.plugin.interface import MetadataHookInterface
from setuptools_scm import get_version
class CustomMetadataHook(MetadataHookInterface):
"""Dynamically set project metadata."""
def update(self, metadata: dict[str, Any]) -> None:
""":param metadata: the metadata to alter"""
version = "0.0.1+notset"
with suppress(ValueError):
version = get_version(root=Path.cwd(), relative_to=__file__)
metadata["version"] = version
__all__ = [
"CustomMetadataHook",
]This allows local builds to benefit from SCM-based versioning, while fallback ensures packaging doesn’t fail in restricted environments.