Skip to content

fix(changelog): report an unwritable changelog file - #2125

Open
qianeric-backup wants to merge 1 commit into
commitizen-tools:masterfrom
qianeric-backup:fix/report-unwritable-changelog-file
Open

qianeric-backup wants to merge 1 commit into
commitizen-tools:masterfrom
qianeric-backup:fix/report-unwritable-changelog-file

Conversation

@qianeric-backup

Copy link
Copy Markdown

cz changelog --file path/to/CHANGELOG.md crashed with a traceback when the file could not be written, for example when the parent directory does not exist:

$ cz changelog --file /nope/CHANGELOG.md
Traceback (most recent call last):
  File "commitizen/commands/changelog.py", line 166, in _write_changelog
    with smart_open(
  File "commitizen/git.py", line 206, in smart_open
    return open(name, mode, encoding=encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/nope/CHANGELOG.md'

Every other command reports such a problem with a dedicated exception and exit code.

Change

Adds UnableToWriteChangelog with the new ExitCode.CHANGELOG_WRITE_FAILED (33), raised from _write_changelog:

$ cz changelog --file /nope/CHANGELOG.md
Unable to write the changelog file '/nope/CHANGELOG.md': [Errno 2] No such file or directory: '/nope/CHANGELOG.md'
$ echo $?
33
case before after
--file in a missing directory traceback, exit 1 Unable to write the changelog file ..., exit 33
--file in a writable directory written unchanged
--file default (CHANGELOG.md) written unchanged
--dry-run nothing written unchanged

The OSError is caught only around the smart_open call, so an error raised by a changelog_hook keeps its own behaviour.

Tests

New test_changelog_unwritable_file in tests/commands/test_changelog_command.py; it fails with the raw FileNotFoundError before the change and passes after. I could not run the whole suite here — the container has no python3-venv, so the project's pytest plugins (freezer, regressions) are only partially available; the new test and its neighbours were run directly.

Note

cz changelog --file some/dir/CHANGELOG.md does not create the parent directory, which is the reason this case came up. Creating it like --result-json does in other tools is a separate decision; this PR only makes the failure visible.

Assisted By: AI

`cz changelog --file path/to/CHANGELOG.md` crashed with a traceback when the
file could not be written, for example when the parent directory does not exist:

    $ cz changelog --file /nope/CHANGELOG.md
    Traceback (most recent call last):
      File "commitizen/commands/changelog.py", line 166, in _write_changelog
        with smart_open(
      File "commitizen/git.py", line 206, in smart_open
        return open(name, mode, encoding=encoding)
    FileNotFoundError: [Errno 2] No such file or directory: '/nope/CHANGELOG.md'

Every other command reports such a problem with a dedicated exception and exit
code. This change adds `UnableToWriteChangelog` with the new
`ExitCode.CHANGELOG_WRITE_FAILED` (33) and raises it from `_write_changelog`:

    $ cz changelog --file /nope/CHANGELOG.md
    Unable to write the changelog file '/nope/CHANGELOG.md': [Errno 2] No such
    file or directory: '/nope/CHANGELOG.md'
    $ echo $?
    33

Unchanged: a writable `--file` path writes the changelog, `--dry-run` does not
touch the filesystem, and the in-place `CHANGELOG.md` flow keeps working.

Tests: `test_changelog_unwritable_file` in `tests/commands/test_changelog_command.py`
covers the case (fails with the traceback before the change).

Assisted By: AI
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

⚠️ JUnit XML file not found

The CLI was unable to find any JUnit XML files to upload.
For more help, visit our troubleshooting guide.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant