Skip to content

copyToClipboard() reports success even when the copy actually failed #17

Description

@aakashrajput

What's broken

copyToClipboard() in src/screens/repl.ts:803-815 always returns true unless the spawnSync call itself throws — it never checks the exit status or error field of the result:

function copyToClipboard(text: string): boolean {
  try {
    if (process.platform === "darwin") {
      spawnSync("pbcopy", [], { input: text, encoding: "utf-8" });
    } else if (process.platform === "win32") {
      spawnSync("clip", [], { input: text, encoding: "utf-8", shell: true });
    } else {
      const r = spawnSync("xclip", ["-selection", "clipboard"], { input: text, encoding: "utf-8" });
      if (r.error) spawnSync("xsel", ["--clipboard", "--input"], { input: text, encoding: "utf-8" });
    }
    return true;   // <-- always true, even if xclip AND xsel are both missing
  } catch { return false; }
}

On a Linux box with neither xclip nor xsel installed, both spawn calls fail silently (non-zero exit / ENOENT), but the function still returns true. Every caller (/copy command, "copy last response", clipboard menu actions) then shows "Copied to clipboard." when nothing was copied.

Suggested fix

Check the actual result before returning success:

const r1 = spawnSync("xclip", ["-selection", "clipboard"], { input: text, encoding: "utf-8" });
if (!r1.error && r1.status === 0) return true;
const r2 = spawnSync("xsel", ["--clipboard", "--input"], { input: text, encoding: "utf-8" });
return !r2.error && r2.status === 0;

Same pattern for the darwin/win32 branches (pbcopy/clip — check status === 0 before returning true).

Acceptance criteria

  • Returns false when the platform clipboard tool is missing or exits non-zero.
  • Existing successful-copy behavior unchanged on macOS/Windows/Linux-with-xclip.
  • A test or two covering the failure path (mock spawnSync to return a non-zero status / error).

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions