Skip to content

fix: keep non-ASCII characters when writing JSON files - #2140

Open
vinilpolepalli wants to merge 1 commit into
commitizen-tools:masterfrom
vinilpolepalli:fix-json-keep-non-ascii
Open

vinilpolepalli wants to merge 1 commit into
commitizen-tools:masterfrom
vinilpolepalli:fix-json-keep-non-ascii

Conversation

@vinilpolepalli

Copy link
Copy Markdown

Description

cz bump rewrites .cz.json, package.json, package-lock.json, npm-shrinkwrap.json and composer.json with json.dump/json.dumps. The default ensure_ascii=True escapes every non-ASCII character in the file, so after a bump an emoji in bump_message becomes 🚀 and an author name like José becomes José.

This passes ensure_ascii=False in JsonConfig.set_key, JsonProvider.set_version (composer) and NpmProvider.set_version. npm and composer also write these files without escaping.

It is the JSON version of #1164, which reports the same problem for .cz.yaml (#1966 fixes the YAML side).

Checklist

Was generative AI tooling used to co-author this PR?

  • Yes (please specify the tool below)

Generated-by: Claude Code following the guidelines

Assisted By: AI

Code Changes

  • Add test cases to all the changes you introduce
  • Run uv run poe all locally to ensure this change passes linter check and tests (ran format, ruff, mypy and the test suite separately; only the GPG-signing tests fail locally because gpg is not installed, same as on master)
  • Manually test the changes:
    • Verify the feature/bug fix works as expected in real-world scenarios
    • Test edge cases and error conditions
    • Ensure backward compatibility is maintained
    • Document any manual testing steps performed
  • Update the documentation for the changes (not needed, bug fix only)

Expected Behavior

cz bump changes only the version in JSON files. Other values keep their characters as they are.

Steps to Test This Pull Request

  1. In a new git repository, create .cz.json:
    {
      "commitizen": {
        "name": "cz_conventional_commits",
        "version": "1.0.0",
        "bump_message": "🚀 bump: $current_version → $new_version"
      }
    }
  2. Commit feat: a and run cz bump --yes.
  3. Before this change .cz.json contains "🚀 bump: $current_version → $new_version". With it, the message is unchanged and only version is 1.1.0.

The same check with version_provider = "npm" and a package.json with "author": "José Müller", or version_provider = "composer" with a Cyrillic description, gave José Müller / Тест before and the original text after.

Tests: test_json_provider_handles_various_unicode_characters now also calls set_version and checks the file, test_npm_provider_respects_configured_encoding_for_all_files checks that the latin-1 name is kept, and TestJsonConfig::test_set_key_keeps_non_ascii_characters is new. They fail without the fix and pass with it.

Additional Context

Related: #1164 and #1966 (same issue for YAML config).

🤖 Generated with Claude Code

`cz bump` rewrites `.cz.json`, `package.json`, `package-lock.json`,
`npm-shrinkwrap.json` and `composer.json` with `json.dumps`, which
escapes every non-ASCII character by default. A bump message with an
emoji or an author name with an accent became `🚀` or
`José` in the file.

Pass `ensure_ascii=False`, as `npm` and `composer` themselves do.

Assisted By: AI
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.29%. Comparing base (fb3d172) to head (87adbc2).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2140   +/-   ##
=======================================
  Coverage   98.29%   98.29%           
=======================================
  Files          61       61           
  Lines        2874     2874           
=======================================
  Hits         2825     2825           
  Misses         49       49           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant