Skip to content

Force UTF-8 stdout so validate-schema hook doesn't crash on Windows (cp1252)#158

Draft
voipcomjohn wants to merge 1 commit into
AgriciDaniel:mainfrom
voipcomjohn:fix-windows-emoji-encoding
Draft

Force UTF-8 stdout so validate-schema hook doesn't crash on Windows (cp1252)#158
voipcomjohn wants to merge 1 commit into
AgriciDaniel:mainfrom
voipcomjohn:fix-windows-emoji-encoding

Conversation

@voipcomjohn

Copy link
Copy Markdown

Problem

On Windows the console defaults to cp1252, which can't encode the status emoji (U+1F6D1 and U+26A0) that hooks/validate-schema.py prints when it finds schema issues. The moment the validator detects a placeholder or a deprecated @type in an HTML file's JSON-LD, it crashes:

UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f6d1' in position 0: character maps to <undefined>

So on Windows the PostToolUse hook exits 1 with a traceback exactly when it should be cleanly blocking the edit (exit 2), and the findings never reach the user. Every Write/Edit that trips the emoji path fails this way.

Fix

Reconfigure stdout/stderr to UTF-8 at the top of main(), guarded in try/except so it's a harmless no-op on streams that don't support it. No other behavior change.

Testing

Windows 11 + Python 3.12, against an HTML file whose JSON-LD contains [Business Name], [Phone], and @type: HowTo.

Before: UnicodeEncodeError, exit 1.

After (exit 2, cleanly blocking):

Schema validation ERRORS (blocking):
  - Block 1: Contains placeholder text: [Business Name]
  - Block 1: Contains placeholder text: [Phone]
  - Block 1: @type 'HowTo' is deprecated September 2023

Clean and non-HTML files still exit 0 silently.

🤖 Generated with Claude Code

…cp1252)

On Windows the console defaults to cp1252, which can't encode the U+1F6D1 /
U+26A0 status emoji this hook prints when it finds schema issues. As soon as
the validator hits a placeholder or deprecated @type it raises
UnicodeEncodeError (exit 1) instead of cleanly blocking the edit (exit 2), so
the findings never reach the user.

Reconfigure stdout/stderr to UTF-8 at the top of main(), guarded in try/except
so it's a no-op where unavailable. No other behavior change.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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