Skip to content

fix(no-ticket): read and rewrite config files as UTF-8 - #562

Open
cloudsmith-iduffy wants to merge 5 commits into
masterfrom
iduffy/fix-config-rewrite-utf8
Open

cloudsmith-iduffy wants to merge 5 commits into
masterfrom
iduffy/fix-config-rewrite-utf8

Conversation

@cloudsmith-iduffy

Copy link
Copy Markdown
Collaborator

Description

On Windows, the CLI reads config.ini and credentials.ini in the locale encoding, which is usually cp1252. click-configfile passes no encoding to configparser, and the code that changes the files (save_workspace after cloudsmith auth, and _set_api_key for login, logout and tokens ... --save-config) does the same. A UTF-8 file with Cyrillic text fails because cp1252 has no character for the bytes 0x81, 0x8D, 0x8F, 0x90 and 0x9D (for example, "с" is D1 81). So every command fails, not only auth and logout.

The CLI now picks the encoding for each file:

  • If the bytes decode as UTF-8, the CLI uses UTF-8. A file that starts with a UTF-8 byte order mark keeps the mark.
  • If not, the CLI uses the locale encoding, as it does today. A cp1252 file with José still loads, and a rewrite keeps the file byte for byte.
  • A new file is UTF-8.

ConfigReader.read_config replaces the click-configfile version so that it can give the encoding to configparser for each file. The other logic is the same as the click-configfile code.

When the CLI cannot decode a file in either encoding, the error now gives the path. Sentry CLOUDSMITH-CLI-K is an example of this.

Before (Windows, UTF-8 config.ini with a Cyrillic comment):

> cloudsmith whoami
Error: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 22: character maps to <undefined>

After: the command runs.

Before (a file that is not UTF-8, on macOS or Linux):

$ cloudsmith whoami --config-file config.ini
Error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 21: invalid start byte

After:

$ cloudsmith whoami --config-file config.ini
Error: Cannot read /home/me/config.ini as utf-8 text. Save the file as UTF-8.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactoring
  • Other (please describe)

Additional Notes

🤖 Generated with Claude Code

The CLI read config.ini and credentials.ini with the locale encoding.
On Windows that is cp1252, so a UTF-8 file with Cyrillic text failed
to load. The CLI now reads a file as UTF-8 and uses the locale encoding
only when UTF-8 decoding fails. When the CLI changes a file, it writes
the file back in the same encoding. A file that the CLI cannot decode
now gives an error with the file path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 24, 2026 18:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Both encoding-test assertions reject native Windows line endings, causing all six parameterized cases to fail on the target platform.

1 open issue
What changed in this PR

This PR fixes Windows encoding failures when the CLI reads or updates config.ini and credentials.ini, while retaining support for locale-encoded files.

Changes:

  • Detect each file’s encoding, preserving UTF-8 byte order marks and using UTF-8 for new files.
  • Report the affected path when a configuration file cannot be decoded.
  • Add encoding regression tests and register the Windows test marker.
File Description
pyproject.toml Registers the windows pytest marker.
cloudsmith_cli/​cli/​tests/​test_unexpected_errors.py Checks actionable decoding errors without Sentry reporting.
cloudsmith_cli/​cli/​tests/​test_config_encoding.py Tests UTF-8, BOM, and cp1252 read/rewrite behavior.
cloudsmith_cli/​cli/​config.py Adds per-file encoding detection for reads and updates.
CHANGELOG.md Documents encoding support and improved errors.

Review effort: Balanced

Fix line endings in workspace path assertion

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@cloudsmith-iduffy

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

…ewrite-utf8

# Conflicts:
#	CHANGELOG.md

Co-authored-by: cloudsmith-iduffy <178375997+cloudsmith-iduffy@users.noreply.github.com>

Copilot AI commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in 5709ff8 by merging master and retaining both sets of changelog entries. All 46 targeted tests and pre-commit checks passed; CodeQL found no alerts.

Co-authored-by: cloudsmith-iduffy <178375997+cloudsmith-iduffy@users.noreply.github.com>

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

Development

Successfully merging this pull request may close these issues.

3 participants