Skip to content

Commit 69979ca

Browse files
committed
Fix calling get_version(root=...) with version-file strategy
1 parent 20de474 commit 69979ca

2 files changed

Lines changed: 29 additions & 20 deletions

File tree

CHANGELOG.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ Changelog
44
2.1
55
---
66

7+
.. changelog::
8+
:version: 2.1.1
9+
:released: 14.01.2026
10+
11+
.. change::
12+
:tags: bugfix
13+
14+
Fix calling ``get_version(root=...)`` in combination with version-file strategy -
15+
relative file version was resolved against ``os.cwd``, not explicitly passed ``root``.
16+
717
.. changelog::
818
:version: 2.1.0
919
:released: 10.01.2025

setuptools_git_versioning.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ def _set_default_options(config: dict):
165165

166166

167167
def _read_toml(name_or_path: str | os.PathLike = "pyproject.toml", root: str | os.PathLike | None = None) -> dict:
168-
file_path = Path(root or os.getcwd()).joinpath(name_or_path)
168+
project_root = Path(root or os.getcwd())
169+
file_path = project_root.joinpath(name_or_path)
169170
if not file_path.exists():
170171
log.log(INFO, "'%s' does not exist", file_path)
171172
return {}
@@ -193,10 +194,10 @@ def _read_toml(name_or_path: str | os.PathLike = "pyproject.toml", root: str | o
193194

194195

195196
def _infer_setup_py(name_or_path: str = "setup.py", root: str | os.PathLike | None = None) -> str | None:
196-
root_path = Path(root or os.getcwd())
197-
file_path = root_path.joinpath(name_or_path)
198-
if not file_path.exists():
199-
log.log(INFO, "'%s' does not exist", file_path)
197+
project_root = Path(root or os.getcwd())
198+
setup_py_path = project_root.joinpath(name_or_path)
199+
if not setup_py_path.exists():
200+
log.log(INFO, "'%s' does not exist", setup_py_path)
200201
return None
201202

202203
from distutils.core import run_setup
@@ -210,9 +211,9 @@ def _infer_setup_py(name_or_path: str = "setup.py", root: str | os.PathLike | No
210211
original_sys_path = sys.path.copy()
211212
original_sys_modules = sys.modules.copy()
212213
try:
213-
_add_to_sys_path(root_path)
214-
os.chdir(root_path)
215-
dist = run_setup(os.fspath(file_path), stop_after="init")
214+
_add_to_sys_path(project_root)
215+
os.chdir(project_root)
216+
dist = run_setup(os.fspath(setup_py_path), stop_after="init")
216217
return infer_version(dist, root=root)
217218
finally:
218219
sys.path[:] = original_sys_path
@@ -259,10 +260,6 @@ def infer_version(dist: Distribution, root: str | os.PathLike | None = None) ->
259260
return version
260261

261262

262-
def _read_version_from_file(name_or_path: str | os.PathLike, root: str | os.PathLike | None = None) -> str:
263-
return Path(root or os.getcwd()).joinpath(name_or_path).read_text().strip()
264-
265-
266263
def _substitute_env_variables(template: str) -> str:
267264
log.log(DEBUG, "Substitute environment variables in template %r", template)
268265
for var, default in ENV_VARS_REGEXP.findall(template):
@@ -321,10 +318,10 @@ def _resolve_substitutions(template: str, *args, **kwargs) -> str:
321318

322319

323320
def _add_to_sys_path(root: str | os.PathLike | None) -> None:
324-
root_path = os.fspath(Path(root or os.getcwd()))
325-
if root_path not in sys.path:
326-
log.log(DEBUG, "Adding '%s' folder to sys.path", root_path)
327-
sys.path.insert(0, root_path)
321+
project_root = os.fspath(Path(root or os.getcwd()))
322+
if project_root not in sys.path:
323+
log.log(DEBUG, "Adding '%s' folder to sys.path", project_root)
324+
sys.path.insert(0, project_root)
328325

329326

330327
def _import_reference(
@@ -489,7 +486,8 @@ def version_from_git(
489486
root: str | os.PathLike | None = None,
490487
) -> str:
491488
# Check if PKG-INFO file exists and Version is present in it
492-
pkg_info = Path(root or os.getcwd()).joinpath("PKG-INFO")
489+
project_root = Path(root or os.getcwd())
490+
pkg_info = project_root.joinpath("PKG-INFO")
493491
if pkg_info.exists():
494492
log.log(INFO, "File '%s' is found, reading its content", pkg_info)
495493
lines = pkg_info.read_text().splitlines()
@@ -537,17 +535,18 @@ def version_from_git(
537535
if version_file:
538536
log.log(INFO, "Checking for 'version_file'")
539537

540-
if not Path(version_file).exists():
538+
version_file_path = project_root.joinpath(version_file)
539+
if not version_file_path.exists():
541540
log.log(
542541
INFO,
543542
"version_file '%s' does not exist, return starting_version %r",
544-
version_file,
543+
version_file_path,
545544
starting_version,
546545
)
547546
tag = None
548547
else:
549548
log.log(INFO, "Reading version_file '%s' content", version_file)
550-
tag = _read_version_from_file(version_file, root=root) or None
549+
tag = version_file_path.read_text().strip() or None
551550

552551
if not tag:
553552
log.log(INFO, "File %r is empty", version_file)

0 commit comments

Comments
 (0)