Skip to content

[Bug]: workflow shell step fails or mojibakes UTF-8 output under non-UTF-8 locales on Windows #5541

Backlog

Description

@adityajai119

Bug Description

In src/specify_cli/workflows/step/shell/__init__.py, _decode_output() decodes captured subprocess stdout and stderr with locale.getpreferredencoding(False) first and only retries with locale.getencoding() when the two differ (added in #5209 for Python UTF-8 mode).

On default Windows Python 3.11-3.14 (without PYTHONUTF8=1), locale.getpreferredencoding(False) and locale.getencoding() are both the legacy ANSI code page (for example cp1252), so UTF-8 is never tried. The same happens on narrow POSIX locales (LC_ALL=C, where both are ascii).

This causes two real failures whenever a workflow shell step invokes a CLI that writes UTF-8 (including specify itself, which reconfigures stdout/stderr to UTF-8 at startup, as well as git, gh, node, jq, and Spec Kit's PowerShell/Python helpers):

  1. Step failure on UTF-8 bytes undefined in cp1252 (or any non-ASCII UTF-8 under ascii): Running specify version or specify check in a workflow shell step on default Windows immediately fails with Shell command stdout could not be decoded: 'charmap' codec can't decode byte 0x90 in position 199: character maps to <undefined> (from the banner's ═ / 0xE2 0x95 0x90 and ╝ / 0xE2 0x95 0x9D). Any UTF-8 output containing ” (0x9D), 🔍 (0x8D), CJK characters such as 后台 (0x90), or other sequences with bytes 0x81/0x8D/0x8F/0x90/0x9D fails the step identically.
  2. Silent mojibake (including in output_format: json): UTF-8 bytes that happen to be valid in cp1252 — such as café (\xc3\xa9 -> café), ✓ (\xe2\x9c\x93 -> ✓), → (\xe2\x86\x92 -> →), or 🎯 (\xf0\x9f\x8e\xaf -> 🎯) — are silently corrupted into mojibake in output.stdout, output.stderr, and parsed output.data.

Additionally, if locale.getpreferredencoding(False) returns an unrecognized encoding name, _read_text(data, encoding) raises an uncaught LookupError instead of falling back cleanly.

Steps to Reproduce

  1. On Windows with default Python (without PYTHONUTF8=1, where locale.getpreferredencoding(False) and locale.getencoding() are cp1252), execute a workflow shell step running specify version:
from specify_cli.workflows.base import StepContext
from specify_cli.workflows.step.shell import ShellStep

result = ShellStep().execute(
    {"id": "ver", "type": "shell", "run": "specify version"},
    StepContext(project_root="."),
)
print(result.status, result.error)
  1. Or run a shell step with output_format: json whose command writes UTF-8 JSON ({"name": "café", "status": "✓ → 🎯"}) and inspect result.output["data"].

Expected Behavior

ShellStep decodes valid UTF-8 stdout and stderr cleanly (preserving universal newlines and exact Unicode text in output.stdout, output.stderr, and output.data) regardless of whether Python runs in UTF-8 mode or on a legacy locale code page, while still falling back to the locale code page for native tools (such as cmd.exe emitting cp1252/cp936/cp932) and still failing cleanly when bytes are invalid in both UTF-8 and the locale code page.

Actual Behavior

  1. specify version and specify check fail in ShellStep on default Windows with StepStatus.FAILED and Shell command stdout could not be decoded: 'charmap' codec can't decode byte 0x90 in position 199: character maps to <undefined>.
  2. UTF-8 JSON containing café / ✓ / → / 🎯 is silently mojibaked in output.stdout and output.data ("café", "✓ → 🎯").

Specify CLI Version

1.4.5.dev0 (main @ f323a4d)

AI Agent

opencode

Operating System

Windows 11 (10.0.26220)

Python Version

Python 3.14.7

Error Logs

preferred: cp1252 getencoding: cp1252
specify version -> status: StepStatus.FAILED error: Shell command stdout could not be decoded: 'charmap' codec can't decode byte 0x90 in position 199: character maps to <undefined>
specify check -> status: StepStatus.FAILED error: Shell command stdout could not be decoded: 'charmap' codec can't decode byte 0x90 in position 199: character maps to <undefined>

Additional Context

Follow-up to #5064 and #5209. #5209 added a UTF-8 -> locale.getencoding() fallback when Python runs in UTF-8 mode (PYTHONUTF8=1, default in Python 3.15+); trying strict UTF-8 first and falling back to distinct preferred/system locale encodings unifies default Python 3.11-3.14 behavior with UTF-8 mode while preserving legacy code-page decoding and invalid-byte failure handling.

AI Disclosure

Detected, reproduced, and filed with OpenCode (model: Gemini 4 Neon, autonomous mode; code inspection and local reproduction on Windows).

Activity

  1. added a commit that references this issue on Nov 26, 2026
    c9f0a55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions