Skip to content

fix(tui): preserve UTF-8 in PowerShell clipboard writes (CJK/emoji)#35222

Merged
teknium1 merged 2 commits into
mainfrom
hermes/hermes-04338dd9
May 30, 2026
Merged

fix(tui): preserve UTF-8 in PowerShell clipboard writes (CJK/emoji)#35222
teknium1 merged 2 commits into
mainfrom
hermes/hermes-04338dd9

Conversation

@teknium1

@teknium1 teknium1 commented May 30, 2026

Copy link
Copy Markdown
Contributor

Summary

/copy now preserves CJK, emoji, and accented text in WSL2 and native Windows. Previously the TUI piped UTF-8 bytes to powershell.exe Set-Clipboard -Value $input, but PowerShell decodes piped stdin with the system ANSI code page (CP936 on Chinese Windows), not UTF-8 — so 你好世界 became 浣犲ソ涓栫晫.

Fixes #35107.

Changes

  • ui-tui/src/lib/clipboard.ts: PowerShell write paths (win32 + WSL) base64-encode the UTF-8 bytes and pass them as a -Command argument, decoding with [System.Text.Encoding]::UTF8.GetString(...). This removes the stdin-encoding variable entirely (also immune to BOM injection on redirect). All other backends (pbcopy, wl-copy, xclip, xsel) keep reading UTF-8 stdin natively via a stdin flag.
  • Test coverage for the CJK + emoji round-trip and that PowerShell entries don't use stdin.

Validation

Before After
你好世界,测试中文 🎉 café copied in WSL2 浣犲ソ涓栫晫锛屾祴璇曚腑鏂… 你好世界,测试中文 🎉 café

E2E: base64 round-trip recovers the exact original; decoding the same UTF-8 bytes as CP936 reproduces the issue's reported garble. clipboard.test.ts 19/19 pass; src/lib/clipboard.ts type-checks clean (the 18 pre-existing execFileNoThrow.ts type errors are unrelated and present on main).

Credit

Salvaged from #35124 (@annguyenNous — base64-via-argument mechanism, authorship preserved) with the duplicate #35122 (@BROCCOLO1D — first to report + UTF-8 console-encoding approach) co-credited. Follow-up commit collapses the per-entry psScript callback to a shared script + stdin flag.

Known sibling (not in scope)

The read path (Get-Clipboard -Raw → Node reads stdout as utf8) has the symmetric encoding gap in the Windows→TUI direction. The issue is write-only; read needs a different mechanism (can't base64 stdout without wrapping) and is left for a follow-up.

Infographic

utf8-clipboard-writes

annguyenNous and others added 2 commits May 30, 2026 00:31
…erve UTF-8

When writing text to the clipboard via PowerShell (WSL2 and native Windows),
the previous implementation piped text through stdin using `Set-Clipboard
-Value $input`. PowerShell reads stdin using the Windows system's default
ANSI code page (e.g. CP936 for Chinese Windows), causing all non-ASCII
characters (CJK, emoji, accented) to become garbled.

Fix: encode the text as base64 in Node.js and pass it as a command argument.
PowerShell decodes it from base64 using explicit UTF-8, bypassing the code
page issue entirely.

Fixes #35107
The per-entry psScript callback was identical for every PowerShell entry,
so the function-valued union member added structure without behavior. Collapse
WriteCmd to a plain stdin boolean and apply the one shared base64 script in the
write loop. Document the CP936 root cause inline.

Co-authored-by: BROCCOLO1D <279959838+BROCCOLO1D@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

🔎 Lint report: hermes/hermes-04338dd9 vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 9475 on HEAD, 9475 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 4923 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@alt-glitch alt-glitch added type/bug Something isn't working comp/tui Terminal UI (ui-tui/ + tui_gateway/) P2 Medium — degraded but workaround exists labels May 30, 2026
@teknium1 teknium1 merged commit 16882cf into main May 30, 2026
20 checks passed
@teknium1 teknium1 deleted the hermes/hermes-04338dd9 branch May 30, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tui Terminal UI (ui-tui/ + tui_gateway/) P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/copy in TUI produces garbled CJK text on WSL2

3 participants