orca icon indicating copy to clipboard operation
orca copied to clipboard

fix(terminal): paste OS-copied files as full shell-escaped paths

Open hongdroid94 opened this issue 4 months ago • 1 comments

Summary

Fixes #7777 — on macOS, copying a file in Finder (Cmd+C) and pasting into a terminal pane inserted only the display name instead of the file's full path.

Root cause: terminal-clipboard-paste.ts reads clipboard text first. When a file is OS-copied, macOS synthesizes the file's display name as clipboard text, so the text branch won before the real file reference was ever read. Drag-and-drop already inserts shell-escaped paths — paste now does the same.

What changed

  • src/main/window/clipboard-file-read.ts (new) — the READ mirror of the existing clipboard-file-copy.ts. Reads the OS file references on the clipboard and returns absolute paths, cross-platform:
    • macOS: prefers NSFilenamesPboardType (a plist array of real POSIX paths — also covers multi-select). Falls back to public.file-url, skipping opaque file:///.file/id=… reference URLs that POSIX path resolution can't resolve.
    • Windows: Get-Clipboard -Format FileDropList via PowerShell (mirrors the Set-Clipboard write path).
    • Linux: text/uri-list / x-special/gnome-copied-files, desktop-dependent, mirroring the copy module.
    • Injected deps so platform branching is unit-testable; never throws.
  • clipboard-ipc-handlers.ts — new clipboard:readFilePaths IPC (trusted-sender gated).
  • preload / api-types / web stub — expose readClipboardFilePaths (web returns [], no OS file refs in a browser).
  • terminal-clipboard-paste.ts — reads file references before the text branch; when present, pastes paths.map(p => shellEscapePath(p, targetShell)).join(' ') (reusing the drop path escaper) and returns a new { status:'pasted', kind:'file-path' }. Ordinary text/image copies fall through unchanged.
  • terminal-paste-target-shell.ts (new) — resolves the paste targetShell by delegating to the exact same resolver drag-and-drop uses, so paste and drop quote paths identically (local, SSH, runtime).

Testing

  • pnpm typecheck ✅, pnpm oxlint (changed files) ✅
  • Unit tests: clipboard-file-read.test.ts (macOS plist/fallback/reference-skip, Windows, Linux) + extended terminal-clipboard-paste.test.ts — file-path branch wins over synthesized text; empty result keeps text/image behavior.
  • Manually verified on macOS 26.2: Finder copy → paste yields the full shell-escaped path (spaces / non-ASCII quoted correctly), including multi-file selection.

Notes

  • Windows/Linux paths are covered by mocked-deps unit tests; CI on those OSes is the final gate.
  • Web client can't read OS file references (browser sandbox), so paste falls through to text/image there.

🤖 Generated with Claude Code

hongdroid94 avatar Jul 08 '26 08:07 hongdroid94

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a main-process module that reads absolute file paths from the OS clipboard on macOS, Windows, and Linux with platform-specific parsing and non-throwing fallbacks. Exposes this via a new clipboard:readFilePaths IPC handler, preload API surface, and a web preload stub. Updates terminal paste logic to prefer clipboard-copied file paths, shell-escaping them for the target shell before falling back to text/image paste, and adds a helper to resolve the terminal paste target shell based on worktree/connection context. Includes corresponding test coverage.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers summary, testing, and notes, but omits required Screenshots, AI Review Report, and Security Audit sections. Add the missing template sections, including a screenshots note, cross-platform AI review details, and a security audit summary.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR implements #7777 by reading file clipboard entries, converting them to paths, shell-escaping them, and preferring them over plain text.
Out of Scope Changes check ✅ Passed The added tests, shell-target resolver, IPC plumbing, and web stub all support the file-path paste feature and appear in scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title is concise and accurately summarizes the main change: pasting OS-copied files as shell-escaped paths.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 08 '26 08:07 coderabbitai[bot]