Odd behavior with asynchronous console I/O in REPL
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.4k
- 平均合并
- 4 天 3 小时
- 30 天内合并 PR
- 272
描述
Version
v16.15.0
Platform
Microsoft Windows NT 10.0.19044.0 x64
Subsystem
No response
What steps will reproduce the bug?
- Open the REPL.
- Run
setTimeout(() => console.log('h'), 1000)and wait for the timer to expire. The letterhwill print where the cursor was, and push the cursor down to the line after the prompt. - 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. - Hit tab to autofill. This is even more broken, as autofill picks up from the cursor rather than from the first letter.
- 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:

After step 4:

After step 5:

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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
在 Node.js REPL 中使用 setTimeout 和 console.log 重现该问题,然后跟踪 REPL 的异步控制台 I/O 和提示符处理。完成的标准是:异步输出留下一个一致的提示符,并像 Promise.reject 示例一样保留输入、自动填充和退格行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript
- 领域
- cli
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100