@@ -215,9 +215,86 @@ implemented. A literal `$` must be quoted as `$$`.
215215Added support for default value syntax ` ${NAME:-}`.
216216```
217217
218+ ``` {versionchanged} 0.76.0
219+
220+ Added the `${__version__}` template variable.
221+ ```
222+
223+ #### The ` __version__ ` variable
224+
225+ When fromager resolves the version of a package it injects the version
226+ string into the template environment as ` __version__ ` . You can
227+ reference it in env values with ` ${__version__} ` .
228+
229+ ##### Fixing setuptools-scm / flit_scm builds
230+
231+ Packages that use ` setuptools-scm ` or ` flit_scm ` for version detection fail
232+ when built from source archives without ` .git ` metadata. The build backend
233+ raises a ` LookupError ` during import because it cannot determine the version.
234+
235+ Setting ` SETUPTOOLS_SCM_PRETEND_VERSION_FOR_{DIST_NAME} ` tells
236+ setuptools-scm to use the provided version instead of reading from SCM.
237+ This is checked before any ` .git ` or ` PKG-INFO ` lookup, so the error is
238+ avoided entirely.
239+
240+ Use the ** per-package** form with the distribution name normalized to
241+ uppercase with hyphens, dots, and underscores replaced by a single ` _ `
242+ (adapted [ PEP 503] ( https://peps.python.org/pep-0503/ ) semantics). For
243+ example, for a package named ` foo ` :
244+
245+ ``` yaml
246+ env :
247+ SETUPTOOLS_SCM_PRETEND_VERSION_FOR_FOO : " ${__version__}"
248+ ` ` `
249+
250+ The per-package form is
251+ [preferred by setuptools-scm](https://setuptools-scm.readthedocs.io/en/latest/config/)
252+ over the generic ` SETUPTOOLS_SCM_PRETEND_VERSION`, which can leak into
253+ other setuptools-scm packages in the same build environment.
254+
255+ This is simpler than writing a custom `prepare_source` plugin override
256+ to inject version metadata.
257+
258+ # #### Availability
259+
260+ ` __version__` is set for `build_sdist`, `build_wheel`, and all
261+ dependency hooks (`get_build_backend_dependencies`,
262+ ` get_build_sdist_dependencies` , etc.) that run *after* version
263+ resolution.
264+
265+ It is **not** available :
266+
267+ - During the `resolve` phase itself — the version has not yet been
268+ determined.
269+ - When bootstrapping from a **git URL whose reference is not a valid
270+ PEP 440 version** (for example
271+ ` pkg @ git+https://host/repo.git` or
272+ ` pkg @ git+https://host/repo.git@main` ). In this case fromager
273+ must build the package metadata just to discover the version, so
274+ the early dependency-resolution hooks run with `version=None`.
275+
276+ If your env var is used in a phase where the version might be
277+ unknown, add a fallback default so the substitution does not fail :
278+
279+ ` ` ` yaml
280+ env:
281+ # safe — falls back to empty string when version is not yet known
282+ MY_VAR: "${__version__:-}"
283+ ` ` `
284+
285+ Without the fallback, a bare `${__version__}` raises an error when
286+ the version is unavailable.
287+
288+ # #### Examples
289+
218290` ` ` yaml
219- # example
220291env:
292+ # fix setuptools-scm builds from source archives (use per-package form)
293+ SETUPTOOLS_SCM_PRETEND_VERSION_FOR_FOO: "${__version__}"
294+ # use the resolved package version in a download URL
295+ LIB_URL: "https://github.com/org/lib/archive/v${__version__}.tar.gz"
296+ # safe for git-URL bootstrapping where version may not yet be known
297+ OPTIONAL_URL: "https://example.com/lib-${__version__:-latest}.tar.gz"
221298 # pre-pend '/global/bin' to PATH
222299 PATH: "/global/bin:$PATH"
223300 # default CFLAGS to empty string and append " -g"
0 commit comments