Skip to content

Commit 21530d8

Browse files
Merge pull request #1178 from RonnyPfannschmidt/fix-554-document-fallback-root
document fallback root in config/api
2 parents c1843e0 + 9169ece commit 21530d8

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

docs/config.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ Callables or other Python objects have to be passed in `setup.py` (via the `use_
9494
unset (the default), `setuptools-scm` will error if it fails to detect the
9595
version.
9696

97+
`fallback_root: Path | PathLike[str] = "."`
98+
: The directory to use when SCM metadata is not available (e.g., in extracted
99+
archives like PyPI tarballs). This is particularly useful for legacy
100+
configurations that need to work both in development (with SCM metadata)
101+
and from archives (without SCM metadata). Defaults to the current directory.
102+
103+
When SCM metadata is present, the `root` parameter is used; when it's not
104+
available, `fallback_root` is used instead. This allows the same configuration
105+
to work in both scenarios without modification.
106+
97107
`parse: Callable[[Path, Config], ScmVersion] | None = None`
98108
: A function that will be used instead of the discovered SCM
99109
for parsing the version. Use with caution,

docs/usage.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,24 @@ from setuptools_scm import get_version
178178
version = get_version(root='..', relative_to=__file__)
179179
```
180180

181+
For legacy configurations or when working with extracted archives (like PyPI tarballs),
182+
you may need to specify a `fallback_root` parameter. This is particularly useful
183+
for legacy Sphinx configurations that use `get_version()` instead of getting the
184+
version from the installed package:
185+
186+
```python
187+
from setuptools_scm import get_version
188+
# For legacy Sphinx conf.py that needs to work both in development and from archives
189+
version = get_version(root='..', fallback_root='..', relative_to=__file__)
190+
```
191+
192+
The `fallback_root` parameter specifies the directory to use when the SCM metadata
193+
is not available (e.g., in extracted tarballs), while `root` is used when SCM
194+
metadata is present.
195+
181196
### Usage from Sphinx
182197

198+
The recommended approach for Sphinx configurations is to use the installed package metadata:
183199

184200
``` {.python file=docs/.entangled/sphinx_conf.py}
185201
from importlib.metadata import version as get_version
@@ -192,6 +208,21 @@ The underlying reason is that services like *Read the Docs* sometimes change
192208
the working directory for good reasons and using the installed metadata
193209
prevents using needless volatile data there.
194210

211+
!!! note "Legacy Sphinx configurations"
212+
213+
If you have a legacy Sphinx configuration that still uses `setuptools_scm.get_version()`
214+
directly (instead of `importlib.metadata`), you may need to use the `fallback_root`
215+
parameter to ensure it works both in development and when building from archives:
216+
217+
```python
218+
from setuptools_scm import get_version
219+
# Legacy approach - use fallback_root for archive compatibility
220+
release = get_version(root='..', fallback_root='..', relative_to=__file__)
221+
version = ".".join(release.split('.')[:2])
222+
```
223+
224+
However, it's strongly recommended to migrate to the `importlib.metadata` approach above.
225+
195226

196227
### With Docker/Podman
197228

0 commit comments

Comments
 (0)