Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fallback version when no Git tags are found? #269

Closed
pawamoy opened this issue Nov 14, 2024 · 2 comments
Closed

Use fallback version when no Git tags are found? #269

pawamoy opened this issue Nov 14, 2024 · 2 comments

Comments

@pawamoy
Copy link
Contributor

pawamoy commented Nov 14, 2024

Currently, it looks like the fallback version is only used when pdm-backend completely fails reading anything from the SCM, i.e. the project is not a Git repository. When the project is a Git repository, but doesn't have any tags, pdm-backend will infer a version like 0.1.devN+gHASH.dDATE.

I'm in a situation where uv refuses to solve my dependencies, because my current project dev-depends on a library that depends on back the current project, and since the current project version is computed as 0.1.etc (because it doesn't have Git tags1) , it's not compatible with what the library requires, and uv fails. See astral-sh/uv#8148. I think uv should not fail, or have a config flag to ignore the current project version.

In the meantime, I'm trying to use the fallback version, but since my project is a Git repo, the fallback version doesn't seem to be used. That's also what I gather from reading the code:

            scm_version = get_version_from_scm(
                context.root, tag_regex=tag_regex, tag_filter=tag_filter
            )
            if scm_version is None:  # <-- probably false, and branch is not entered
                if fallback_version is not None:
                    version = fallback_version

Do you think it would be feasible to use the fallback version if no tags were found, even though the project is a proper Git repository?

Footnotes

  1. It doesn't have Git tags for various reasons. For example it could be a contributor fork, and GitHub doesn't copy tags when creating a fork.

@pawamoy
Copy link
Contributor Author

pawamoy commented Nov 14, 2024

OK I found a uv-based solution (see astral-sh/uv#8148 (comment)), this issue can be closed 🙂

@pawamoy pawamoy closed this as not planned Won't fix, can't repro, duplicate, stale Nov 14, 2024
@pawamoy
Copy link
Contributor Author

pawamoy commented Dec 23, 2024

Actually that led to other issues. So I used my brain and found a pdm-backend solution:

[tool.pdm.version]
source = "call"
getter = "scripts.get_version:get_version"
# scripts/get_version.py
import re
from contextlib import suppress
from pathlib import Path

from pdm.backend.hooks.version import SCMVersion, Version, default_version_formatter, get_version_from_scm

_root = Path(__file__).parent.parent
_changelog = _root / "CHANGELOG.md"
_changelog_version_re = re.compile(r"^## \[(\d+\.\d+\.\d+)\].*$")
_default_scm_version = SCMVersion(Version("0.0.0"), None, False, None, None)


def get_version():
    scm_version = get_version_from_scm(_root) or _default_scm_version
    if scm_version.version <= Version("0.1"):  # Missing Git tags?
        with suppress(OSError, StopIteration):
            with _changelog.open("r", encoding="utf8") as file:
                match = next(filter(None, map(_changelog_version_re.match, file)))
                scm_version = scm_version._replace(version=Version(match.group(1)))
    return default_version_formatter(scm_version)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant