denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
96.99k stars 5.35k forks source link

Closing stdin causes `prompt()` to output a newline, in contrast to `confirm()` #22956

Open solb opened 7 months ago

solb commented 7 months ago

Running the following command and pressing Ctrl+D to close the standard input stream reveals that the built-in prompt() function outputs a newline in this case:

$ deno eval 'prompt()'
Prompt
$

Surprisingly, confirm() does not exhibit this behavior (note the location of the second $ prompt):

$ deno eval 'confirm()'
Confirm [y/N] $

I would argue that the latter response is more correct, by analogy with C. Consider the following POSIX.1-2008–compliant program:

#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    fputs("Prompt ", stdout);

    char *buf = NULL;
    size_t len = 0;
    getline(&buf, &len, stdin);
    free(buf);

    return 0;
}

Hitting Ctrl+D elicits the confirm()-style behavior on my GNU/Linux system:

$ c99 -o getline getline.c && ./getline
Prompt $

I noticed this inconsistency when filing #22955: it is responsible for the extra newline in the final command-line example in that issue. I haven't traced the issue deeper than prompt()'s use of op_read_line_prompt() (in contrast to confirm()'s readLineFromStdinSync()) in runtime/js/41_prompt.js.

Tested on this official release build:

$ deno --version
deno 1.41.0 (release, x86_64-unknown-linux-gnu)
v8 12.1.285.27
typescript 5.3.3
yazan-abdalrahman commented 1 month ago

@lucacasonato @solb image it was resolved.

solb commented 4 weeks ago

My earlier test was on Debian, and I just saw the same issue with Deno 2 RC7 on Termux atop Android. Your screenshot makes it look like you tested on Windows; are you sure it hasn't only been fixed there?

yazan-abdalrahman commented 4 weeks ago

@solb I tried it in another version before and noticed the issue, however in the newest it was fixed, so you can pull the latest source and try.

Maybe it is still not released in the newest release.