Skip to content

[Bug] multiline splits emoji and other astral characters (corrupt value, Enter twice stops submitting) #640

Open

Description

@kuldeepsinh19

Environment

  • OS: Linux (Docker)
  • Node Version: v24.22.1
  • Package: @clack/core (through @clack/prompts)
  • Package Version: @clack/prompts 1.9.0, @clack/core 1.6.0, same on main (5f27460)

Describe the bug

In a multiline prompt, typing an emoji (or any other character that JavaScript stores as a surrogate pair) leaves the cursor between its two halves. After that:

  • the emoji is drawn as two �, because the cursor highlight covers one half of it;
  • the next typed character is inserted inside the emoji, so the returned string contains lone surrogates;
  • pressing Enter twice no longer submits, because the cursor never reaches the end of the input;
  • Backspace and Delete remove half of the emoji, and Left and Right stop in the middle of it.

Node delivers such a character as one keypress whose char has length 2, but the key handler advances the cursor by one after inserting it (multi-line.ts#L139-L145). Backspace, Delete, the arrow keys and userInputWithCursor also work one UTF-16 code unit at a time. Typing in text is fine, since readline tracks the cursor there.

To Reproduce

// repro.mjs, with "@clack/prompts": "1.9.0"
import { multiline } from '@clack/prompts';

const value = await multiline({ message: 'Notes', showSubmit: true });
console.log(JSON.stringify(value), value.isWellFormed());
  • Run node repro.mjs.
  • Type 👍, then a, press Tab, then Enter.

It prints "\ud83da\udc4d" false.

More sequences, fed to the prompt as the raw bytes a terminal sends (👍 is f0 9f 91 8d):

Keys Returned
👍, Enter, Enter nothing, the prompt stays open and keeps adding newlines
👍, Backspace, Tab, Enter (showSubmit, defaultValue: 'none') "\udc4d"
👍, Left, Delete, Tab, Enter (showSubmit, defaultValue: 'none') "\udc4d"
a, 👍, b, Left ×3, x, Tab, Enter (showSubmit) "xa\ud83db\udc4d"

Expected behavior

"👍a" true. An emoji counts as one character when typing after it, deleting it and moving the cursor over it, and Enter twice submits as usual.

Additional Information

This is only about surrogate pairs, which is where the value gets corrupted. Joined emoji such as 👨‍👩‍👧 arrive as several keypresses and would still be stepped through one part at a time.

I have a fix with tests and will link the PR here.

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions