mtkennerly / poetry-dynamic-versioning

Plugin for Poetry to enable dynamic versioning based on VCS tags
MIT License
588 stars 36 forks source link

__version__.py file not included in build #172

Closed wabiloo closed 3 months ago

wabiloo commented 3 months ago

Hi

My package relies on a __version__.py file which contains a __version__ string. I would like to understand whether it's recommended to not add it to VCS, as your documentation seems to imply...

I did this, with the following configuration:

[tool.poetry]
name = "opinionated-media-processor"
version = "0.0.0"
description = "Manipulate, Encode and Package - the simple way"
authors = ["Fabre Lambeau <fabre.lambeau@gmail.com>"]
readme = "README.md"
packages = [{ include = "opinionated_media_processor" }]

[tool.poetry.dependencies]
...

[tool.poetry.scripts]
omp = "opinionated_media_processor.omp:main"

[tool.poetry-dynamic-versioning]
enable = true
style = "semver"
metadata = true
dirty = true

[tool.poetry-dynamic-versioning.substitution]

[tool.poetry-dynamic-versioning.files."opinionated_media_processor/__version__.py"]
persistent-substitution = true
initial-content = """
  # Version placeholders, to be replaced during poetry dynamic-versioning substitution.
  __version__ = "0.0.0"
  __version_tuple__ = (0, 0, 0)
"""

[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
build-backend = "poetry_dynamic_versioning.backend"

The __version__.py is in the .gitignore

However, after building and installing locally, the omp --version command, I get an error that seems to indicate that the file is missing:

Traceback (most recent call last):
  File "/opt/homebrew/bin/omp", line 5, in <module>
    from opinionated_media_processor.omp import main
  File "/opt/homebrew/lib/python3.11/site-packages/opinionated_media_processor/omp.py", line 4, in <module>
    from __version__ import __version__
ModuleNotFoundError: No module named '__version__'

And indeed, if I unzip the dist file, there is no such file.

mtkennerly commented 3 months ago

The __version__.py is in the .gitignore

That will cause Poetry to ignore it as well, by default:

https://python-poetry.org/docs/pyproject/#include-and-exclude

If a VCS is being used for a package, the exclude field will be seeded with the VCS’ ignore settings (.gitignore for git for example).

Explicitly declaring entries in include will negate VCS' ignore settings.

If you add a line for include = ["opinionated_media_processor/__version__.py"] in the [tool.poetry] section, then it should work :)

wabiloo commented 3 months ago

I ended up removing the __version__.py from .gitignore and committing it with "0.0.0" inside.

On running poetry build I (naturally) get it into the dist file, with a proper version number. That sort of takes care of that.

However that means I cannot wrap the application in a Docker image from local files. So I changed that do copy the dist file and pip install it in the image.

Still, I'm wondering what the best/recommended way to go is.

With this method I obviously need to remember to never commit that version file if I use poetry dynamic-versioning command. But the same applies to the pyproject.toml file in this case anyway (which can't go in .gitignore)...

Why is it recommended in the first place not to add the version file to VCS? Seems it could make running files locally more error-prone...

(I'm still a beginner in Python when it comes to that sort of thing...)

mtkennerly commented 3 months ago

Why is it recommended in the first place not to add the version file to VCS? Seems it could make running files locally more error-prone...

Mainly so you don't have to worry about accidentally committing the plugin's changes when using commands like poetry build or poetry run. You're right that you do still have to be careful with poetry dynamic-versioning, of course, but for most commands, you don't have to think about it.