Repository: pypa/setuptools_scm

Issue:
force_write_version_files added with different default in version 8
The parameter `force_write_version_files` was added to `_get_version` with a different default than in <8, which causes `_get_version` to no longer write out version files.

Seen in https://github.com/scikit-build/scikit-build-core/issues/507

Discussion:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 3658763..cc663fd 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -3,6 +3,9 @@ v8.0.2
 
 bugfix
 ------
+
+* fix #919: restore legacy version-file behaviour for external callers + add Deprecation warning
+* fix #918: use packaging from setuptools for self-build
 * fix #914: ignore the deprecated git archival plugin as its integrated now
 * fix #912: ensure mypy safety of the version template + regression test
 * fix #913: use 240s timeout instead of 20 for git unshallow
diff --git a/pyproject.toml b/pyproject.toml
index dc5475c..983789f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,6 @@
 build-backend = "_own_version_helper"
 requires = [
   'importlib-metadata>=4.6; python_version < "3.10"',
-  "packaging>=20",
   "rich",
   "setuptools>=61",
   'tomli; python_version < "3.11"',
diff --git a/src/setuptools_scm/_cli.py b/src/setuptools_scm/_cli.py
index 560650c..66099b1 100644
--- a/src/setuptools_scm/_cli.py
+++ b/src/setuptools_scm/_cli.py
@@ -31,7 +31,7 @@ def main(args: list[str] | None = None) -> None:
         )
         config = Configuration(inferred_root)
 
-    version = _get_version(config)
+    version = _get_version(config, force_write_version_files=False)
     if version is None:
         raise SystemExit("ERROR: no version found for", opts)
     if opts.strip_dev:
diff --git a/src/setuptools_scm/_get_version_impl.py b/src/setuptools_scm/_get_version_impl.py
index a5015c7..8be513a 100644
--- a/src/setuptools_scm/_get_version_impl.py
+++ b/src/setuptools_scm/_get_version_impl.py
@@ -84,12 +84,20 @@ def write_version_files(
 
 
 def _get_version(
-    config: Configuration, force_write_version_files: bool = False
+    config: Configuration, force_write_version_files: bool | None = None
 ) -> str | None:
     parsed_version = parse_version(config)
     if parsed_version is None:
         return None
     version_string = _format_version(parsed_version)
+    if force_write_version_files is None:
+        force_write_version_files = True
+        warnings.warn(
+            "force_write_version_files ought to be set,"
+            " presuming the legacy True value",
+            DeprecationWarning,
+        )
+
     if force_write_version_files:
         write_version_files(config, version=version_string, scm_version=parsed_version)
 
diff --git a/src/setuptools_scm/_version_cls.py b/src/setuptools_scm/_version_cls.py
index e62c9fa..55c00c6 100644
--- a/src/setuptools_scm/_version_cls.py
+++ b/src/setuptools_scm/_version_cls.py
@@ -5,8 +5,12 @@ from typing import cast
 from typing import Type
 from typing import Union
 
-from packaging.version import InvalidVersion
-from packaging.version import Version as Version
+try:
+    from packaging.version import InvalidVersion
+    from packaging.version import Version as Version
+except ImportError:
+    from setuptools.extern.packaging.version import InvalidVersion  # type: ignore
+    from setuptools.extern.packaging.version import Version as Version  # type: ignore
 
 
 class NonNormalizedVersion(Version):


Test patch:
diff --git a/testing/test_basic_api.py b/testing/test_basic_api.py
index 6fa02fe..95afa0f 100644
--- a/testing/test_basic_api.py
+++ b/testing/test_basic_api.py
@@ -242,3 +242,20 @@ def test_custom_version_cls() -> None:
     #  to create a test?
     # monkeypatch.setenv(setuptools_scm.PRETEND_KEY, "1.0.1")
     # assert setuptools_scm.get_version(version_cls=MyVersion) == "1"
+
+
+def test_internal_get_version_warns_for_version_files(tmp_path: Path) -> None:
+    tmp_path.joinpath("PKG-INFO").write_text("Version: 0.1")
+    c = Configuration(root=tmp_path, fallback_root=tmp_path)
+    with pytest.warns(
+        DeprecationWarning,
+        match="force_write_version_files ought to be set,"
+        " presuming the legacy True value",
+    ):
+        ver = setuptools_scm._get_version(c)
+    assert ver == "0.1"
+
+    # force write won't write as no version file is configured
+    assert setuptools_scm._get_version(c, force_write_version_files=False) == ver
+
+    assert setuptools_scm._get_version(c, force_write_version_files=True) == ver