callowayproject/bump-my-version

pyproject.toml file erased after UnicodeError on WIndows

Closed this issue · 2 comments

  • bump-my-version version: 0.18.3
  • Python version: 3.12
  • Operating System: Windows 10

Description

With the following configuration:

[tool.bumpversion]
current_version = "1.0.0-b3"
parse = """(?x)
    (?P<major>0|[1-9]\\d*)\\.
    (?P<minor>0|[1-9]\\d*)\\.
    (?P<patch>0|[1-9]\\d*)
    (?:
        -                             # dash seperator for pre-release section
        (?P<pre_l>[a-zA-Z-]+)         # pre-release label
        (?P<pre_n>0|[1-9]\\d*)        # pre-release version number
    )?                                # pre-release section is optional
"""
serialize = [
    "{major}.{minor}.{patch}-{pre_l}{pre_n}",
    "{major}.{minor}.{patch}",
]
search = "{current_version}"
replace = "{new_version}"
regex = false
ignore_missing_version = false
tag = true
sign_tags = false
tag_name = "v{new_version}"
tag_message = "Bump version: {current_version} → {new_version}"
allow_dirty = false
commit = true
message = "Bump version: {current_version} → {new_version}"
commit_args = "--no-verify"

[tool.bumpversion.parts.pre_l]
values = ["dev", "a", "b", "rc", "final"]
optional_value = "final"

[[tool.bumpversion.files]]
filename = "bsb_hdf5/__init__.py"

My pyproject.toml file gets erased when I run bump-my-version bump pre_n:

PS C:\Users\pwd06\git\bsb-hdf5> bump-my-version bump pre_n
  Specified version (1.0.0-b3) does not match last tagged version (1.0.0)
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Python312\Scripts\bump-my-version.exe\__main__.py", line 7, in <module>
  File "C:\Python312\Lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\rich_click\rich_command.py", line 126, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\bumpversion\cli.py", line 326, in bump
    do_bump(version_part, new_version, config, found_config_file, dry_run)
  File "C:\Python312\Lib\site-packages\bumpversion\bump.py", line 101, in do_bump
    update_config_file(config_file, config, version, next_version, ctx, dry_run)
  File "C:\Python312\Lib\site-packages\bumpversion\config\files.py", line 150, in update_config_file
    updater.update_file(current_version, new_version, context, dry_run)
  File "C:\Python312\Lib\site-packages\bumpversion\files.py", line 335, in update_file
    self._update_toml_file(search_for, raw_search_pattern, replace_with, dry_run)
  File "C:\Python312\Lib\site-packages\bumpversion\files.py", line 362, in _update_toml_file
    self.path.write_text(tomlkit.dumps(toml_data))
  File "C:\Python312\Lib\pathlib.py", line 1048, in write_text
    return f.write(data)
           ^^^^^^^^^^^^^
  File "C:\Python312\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\u2192' in position 1394: character maps to <undefined>

Using WSL2 the command thinks every file in my git repository is modified, but if I add --allow-dirty then the command is executed and leads to the desired outcome.

@Helveg I may need some help from you on this one, since my primary dev machine is a Mac (even though tests run on Windows). I'd love to write a test for this, but I'm not sure how.

The Unicode right arrows (unicode 2192 →) in the tag_message and message configurations is the source of this (I believe). The problem appears that it is writing the configuration file using Windows encoding (CP-1252) instead of UTF-8.

I'll force the output to use UTF-8, but I'm not sure how to properly test this. Any ideas?

I'm not familiar with the test suite of this repository, but wouldn't it suffice to call do_bump or update_config_file on a valid configuration file that contains that arrow character?