nodejs / nodejs/node

Odd behavior with asynchronous console I/O in REPL

Offen
#43,074 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

confirmed-bug repl
Vorherrschende Sprache
JavaScript
Sterne
122k
Forks
37.3k
Ø Merge
4 T. 2 Std.
Gemergte PRs (30 T.)
283

Beschreibung

Version

v16.15.0

Platform

Microsoft Windows NT 10.0.19044.0 x64

Subsystem

No response

What steps will reproduce the bug?
  1. Open the REPL.
  2. Run setTimeout(() => console.log('h'), 1000) and wait for the timer to expire. The letter h will print where the cursor was, and push the cursor down to the line after the prompt.
  3. Type another letter which results in autofill, such as g. This already looks somewhat broken, with the letter appearing a few columns to the left of the cursor.
  4. Hit tab to autofill. This is even more broken, as autofill picks up from the cursor rather than from the first letter.
  5. Hit backspace and watch the word magically fix itself, with a new prompt in the same line as the cursor.
How often does it reproduce? Is there a required condition?

It always occurs when console.log is called asynchronously after the prompt is rendered. process.stdout.write also has somewhat odd behavior, but is noticeably less broken than console.log, possibly because of the lack of a newline character on the end.

What is the expected behavior?

It should be consistent with Promise.reject:

> Promise.reject('h')
Promise {
  <rejected> 'h',
  [Symbol(async_id_symbol)]: 96,
  [Symbol(trigger_async_id_symbol)]: 5,
  [Symbol(destroyed)]: { destroyed: false }
}
> Uncaught 'h'
>

Note that, although this does print Uncaught 'h' after the prompt, it then prints a new prompt immediately. With setTimeout and console.log, the new prompt isn't printed until backspace is pressed.

What do you see instead?

After step 3:
image
After step 4:
image
After step 5:
image

Additional information

Although I discovered this in v16.15.0, it also appears to happen in v14.19.2, v17.9.0, and v18.1.0.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Problem in der Node.js REPL mit setTimeout und console.log und verfolge anschließend die asynchrone Konsolen-E/A der REPL sowie die Prompt-Verarbeitung. Als erledigt gilt die Aufgabe, wenn die asynchrone Ausgabe einen konsistenten Prompt hinterlässt und das Verhalten beim Tippen, bei der Autofill-Vervollständigung und bei der Rücktaste wie im Beispiel Promise.reject beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.