mtkennerly / poetry-dynamic-versioning

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

Plugin remains enabled after `poetry dynamic-versioning` (and later `poetry build`) #137

Closed bram-tv closed 10 months ago

bram-tv commented 10 months ago

When using the poetry dynamic-versioning command then the plugin is left enabled after a later poetry build.

This causes problems when later doing an install from the created .tar.gz file: it contains a pyproject.toml file which has the plugin enabled but it's not being used from inside the repository which then causes error.

(The .whl file works, but in my opinion the .tar.gz should also work)

Steps to reproduce

Example pyproject.toml:

[tool.poetry]
name = "example-package"
version = "0.0.0"
description = '..'
authors = ['..']
packages = [{ include = "example.py"}]

[tool.poetry-dynamic-versioning]
enable = true

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

(+ create an empty example.py file)

Without running poetry dynamic-versioning

This work as expected:

$ poetry build
Building example-package (0.0.0.post4.dev0+67683fa)
  - Building sdist
  - Built example_package-0.0.0.post4.dev0+67683fa.tar.gz
  - Building wheel
  - Built example_package-0.0.0.post4.dev0+67683fa-py2.py3-none-any.whl

$ tar -O -x -f  dist/example_package-0.0.0.post4.dev0+67683fa.tar.gz example_package-0.0.0.post4.dev0+67683fa/pyproject.toml
[tool.poetry]
name = "example-package"
version = "0.0.0.post4.dev0+67683fa"
description = '..'
authors = ['..']
packages = [{ include = "example.py"}]

[tool.poetry-dynamic-versioning]
enable = false

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

$ cd /tmp
$ pip install --no-deps --dry-run ..../dist/example_package-0.0.0.post4.dev0+67683fa.tar.gz
Looking in indexes: https://pypi.org/simple
Processing ...../dist/example_package-0.0.0.post4.dev0+67683fa.tar.gz
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Would install example-package-0.0.0.post4.dev0+67683fa

The changes made to the packaged toml file:

With running poetry dynamic-versioning first

$ poetry dynamic-versioning
Version: 0.0.0.post4.dev0+67683fa
Files with substitutions: none

$ cat pyproject.toml
[tool.poetry]
name = "example-package"
version = "0.0.0.post4.dev0+67683fa"
description = '..'
authors = ['..']
packages = [{ include = "example.py"}]

[tool.poetry-dynamic-versioning]
enable = true

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

$ poetry build
Building example-package (0.0.0.post4.dev0+67683fa)
  - Building sdist
  - Built example_package-0.0.0.post4.dev0+67683fa.tar.gz
  - Building wheel
  - Built example_package-0.0.0.post4.dev0+67683fa-py2.py3-none-any.whl

$ tar -O -x -f  dist/example_package-0.0.0.post4.dev0+67683fa.tar.gz example_package-0.0.0.post4.dev0+67683fa/pyproject.toml
[tool.poetry]
name = "example-package"
version = "0.0.0.post4.dev0+67683fa"
description = '..'
authors = ['..']
packages = [{ include = "example.py"}]

[tool.poetry-dynamic-versioning]
enable = true

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

$ cd /tmp
$ pip install --no-deps --dry-run ..../dist/example_package-0.0.0.post4.dev0+67683fa.tar.gz
Looking in indexes: https://pypi.org/simple
Processing ....../dist/example_package-0.0.0.post4.dev0+67683fa.tar.gz
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Preparing metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [26 lines of output]
      Traceback (most recent call last):

        ...
      RuntimeError: Unable to detect version control system. Checked: Git. Not installed: Mercurial, Darcs, Subversion, Bazaar, Fossil, Pijul.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

The changes made to the packaged toml file:

Additional information

Relevant code:

https://github.com/mtkennerly/poetry-dynamic-versioning/blob/322fe62f5a5270d324ca8a12b42224f67e822d2e/poetry_dynamic_versioning/cli.py#L71 This runs the apply version with retain=True. This causes the plugin to remain enable which is ok'ish

https://github.com/mtkennerly/poetry-dynamic-versioning/blob/322fe62f5a5270d324ca8a12b42224f67e822d2e/poetry_dynamic_versioning/__init__.py#L417 This code only disables the plugin when the version number changed. When poetry dynamic-versioning was first run the version number is already set so it doesn't detect a change and leaves the plugin as enabled.

Possible solutions

mtkennerly commented 10 months ago

Thanks for the thorough report!

Always disable the plugin during poetry build (i.e. remove the version check): no idea if this would have unwanted effects

I think this is the right solution. That version check was meant to avoid duplicate work, especially back when the plugin had to rely on some brittle monkey-patching, but there are other checks now that do the same thing better (namely in _get_and_apply_version when it checks _state.projects).