pre-commit / pre-commit-hooks

Some out-of-the-box hooks for pre-commit
MIT License
5.34k stars 708 forks source link

`pretty_format_json` crashes when run as a `pre-commit` hook, but not when run alone with a similar config #1010

Closed aatango closed 8 months ago

aatango commented 8 months ago

SETUP

  1. Change to a (new) repository on Windows;

  2. use the following .pre-commit-config.yaml configuration:

    -   repo: 'https://github.com/pre-commit/pre-commit-hooks'
    rev: 'v4.5.0'
    hooks:
    -   id: 'pretty-format-json'
        args: ['--no-sort-keys', '--indent', '2', '--autofix']
  3. add a new JSON file with a non-ascii filename, for example: 漢字.json, or עִבְרִית‎.json; and

  4. attempt to commit both the pre-commit configuration and the JSON file to your repository.

EXPECTED

The newly added JSON file is properly handled both when running git commit, or the individual pretty_format_json.py file.

ACTUAL

NOTE

The error can be reproduced on Windows 10, but not on Ubuntu. Both have Python 3.10.

asottile commented 8 months ago

what codepage have you confused on Windows? have you tried enabling utf8 mode?

aatango commented 8 months ago

Activating the (still beta) support for UTF-8 mode in Windows appears to bypass the issue. In this mode, executing the script within the context of a git commit does not throw an error; a JSON file is properly formatted.

However, this is still an experimental support from Windows, and activating it causes problems elsewhere.

asottile commented 8 months ago

how about python's utf8 mode?

aatango commented 8 months ago

Setting the environment variable PYTHONUTF8=1 did solve the issue. Thank you for the help.