Skip to content

fix(tui): copy text on WSL through Windows interop - #66310

Closed
PhonezM wants to merge 1 commit into
anomalyco:devfrom
PhonezM:fix/wsl-clipboard-write-interop
Closed

PhonezM wants to merge 1 commit into
anomalyco:devfrom
PhonezM:fix/wsl-clipboard-write-interop

Conversation

@PhonezM

@PhonezM PhonezM commented Nov 25, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #65913

Type of change

  • Bug fix

What does this PR do?

On WSL the TUI can report a successful copy while the Windows clipboard keeps its previous content. Measured here (WSLg + Windows Terminal, default DISPLAY/WAYLAND_DISPLAY): OpenTUI's host write returns {"status":"written"} in about 5 ms and the text appears on the Wayland selection (wl-paste shows it), but the Windows clipboard is unchanged; the OSC 52 write alone did not reach it either.

This mirrors every copy made under WSL through powershell.exe interop (Set-Clipboard over UTF-8 stdin, 3 s timeout) in parallel with the normal write, and treats the copy as successful when interop succeeds. If interop is unavailable or fails, it falls back to the previous behaviour: the host reported written, or an attempted OSC 52 write unless the terminal reports it as unsupported. Tests inject the Windows writer, so they never touch the real clipboard.

How did you verify your code works?

  • WSL2 + Windows Terminal, default environment: with the change, write() resolved in ~310 ms and the Windows clipboard held the exact text; on the base commit it resolved without changing the clipboard.
  • Same check with DISPLAY/WAYLAND_DISPLAY unset: before, Clipboard write failed (host: unsupported, terminal: not-attempted); after, resolved and the clipboard held the text.
  • 90 KB of text (newlines, CJK, emoji, quotes, backticks) round-tripped byte-for-byte through interop.
  • cd packages/tui && bun test test/clipboard.test.ts (11 pass), bun run typecheck, Prettier.

Screenshots / recordings

N/A (no UI change)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #59664, #49895

@opencodejr opencodejr Bot 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.

Thanks for this. The interop writer itself looks solid, but I'm not convinced it fixes the reported case, and it can make copies fail that may work today.

How I checked: I have no WSL here. I ran the default writer against a fake powershell.exe on PATH, using the "host timed out, terminal attempted" result. The UTF-8 text arrived exactly, and a non-zero exit, a hang (killed at about 3 s) and a missing powershell.exe all rejected with clear errors. Outside WSL it behaves as before. bun test test/clipboard.test.ts passes and tsc -b is clean.

Public API: the only addition is the optional options.windowsClipboard argument on createClipboardAdapter, which is internal to packages/tui. Nothing outside depends on it, and it is backwards compatible.

Concerns:

  1. The reported failure may not be what this fixes. Your manual check (host unsupported, terminal not-attempted) already throws on the base, so it shows an error rather than the false "Copied to clipboard" toast from #65913. The reporter has WSLg with DISPLAY and WAYLAND_DISPLAY set. If the host write reports written there (taken by WSLg but not synced to Windows), this change returns early and does nothing. The regression also lines up with the OpenTUI 0.8 → 0.9 bump (#63989), since clipboard.ts hasn't changed since #61094, so the root cause may be in OpenTUI. Could you show what result.host and result.terminal actually are in the reporter's setup (WSLg + Windows Terminal, default env)?
  2. Copies that may work today now fail. Windows Terminal does support OSC 52 copy. On WSL with no usable interop (appendWindowsPath=false, interop off) and terminal capability unknown, a copy that may well have worked on the base now shows an error (see the inline comment).
  3. Latency. Every WSL copy whose host write fails now waits for powershell.exe to start (often around a second) before the toast appears, even when OSC 52 already worked.

Related: #59664, #49895

Comment thread packages/tui/src/clipboard.ts Outdated
if (result.host.status === "written") return
// The native host clipboard can time out or fail on WSL, and Windows Terminal ignores
// OSC 52, so write through Windows interop before trusting an "attempted" terminal write.
if (isWSL()) {

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.

When the interop write fails, this only keeps the terminal result if capability === "supported". With "unknown", which may be what Windows Terminal often reports, it now throws. Windows Terminal does handle OSC 52 writes, so on WSL without a usable powershell.exe (appendWindowsPath=false, interop disabled, PATH stripped) a copy that may well have worked on the base becomes an error toast. Could the interop failure fall back to the old attempted handling unless the capability is "unsupported"? That keeps this a pure improvement, not a new way to fail. It also deserves a test with capability "unknown", since that is the common case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, both points were right. I measured the reporter's setup (WSLg + Windows Terminal, default DISPLAY=:0, WAYLAND_DISPLAY=wayland-0): the host write returns {"status":"written"} in 4-5 ms and wl-paste shows the text on the Wayland selection, but the Windows clipboard keeps its previous content. So the early return was skipping the fix for the reported case.

The WSL path now mirrors every copy through powershell.exe in parallel with the normal write, and interop succeeding is what makes it a success; if interop is unavailable or fails it falls back to the old result, with unknown treated as before and covered by a test. The cost is one interop process per WSL copy (~310 ms here); I could not find a cheaper way to tell whether a host written actually reached Windows. The measured statuses are in the PR description.

WSLg can report a successful host clipboard write that never reaches the
Windows clipboard, and Windows Terminal may ignore OSC 52, so the TUI
reported a copy that did not happen. Mirror WSL copies through
powershell.exe interop in parallel, and only report a failure when no
route can have copied the text.
@PhonezM
PhonezM force-pushed the fix/wsl-clipboard-write-interop branch from ad9ad90 to 831ad10 Compare November 25, 2026 07:49
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@opencodejr opencodejr Bot 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.

The new commits address my earlier point. When interop fails, copies fall back to the earlier result: the host reported written, or the terminal attempted OSC 52 and does not report it as unsupported. Copies that might have worked no longer turn into errors, and the unknown case now has a test. Running interop in parallel on every WSL copy also covers the case you measured, where WSLg reports written but the Windows clipboard doesn't change.

I can't run WSL here. With a fake powershell.exe, the mirror ran and received the exact text even when the host reported written. Without interop, the copy only failed when OSC 52 was reported as unsupported. Outside WSL nothing changes. bun test test/clipboard.test.ts passes (11) and tsc -b is clean.

Public API unchanged: the only addition is the internal optional windowsClipboard option on createClipboardAdapter.

No further concerns.

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.

tui: v2.4.1 on WSL2 shows "Copied to clipboard" on select, but Ctrl+V paste does nothing; disabling mouse breaks scrollback

1 participant