Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dart:io] - stdin.readLineSync on Windows Powershell keeps waiting until something being typed #54588

Closed
codesculpture opened this issue Jan 11, 2024 · 14 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io

Comments

@codesculpture
Copy link
Contributor

codesculpture commented Jan 11, 2024

void main() {
    final a = stdin.readLineSync();
    print(a);
}

If The above program executed on windows powershell, and if u press enter it would just move the cursor to next line and keep repeatedly do this until u type something, this not letting to pass empty input. Originally Founded from mason_logger felangel/mason#1189

@eseidel
Copy link
Contributor

eseidel commented Jan 11, 2024

https://github.com/dart-lang/sdk/blob/main/sdk/lib/io/stdio.dart#L56 is the code in Dart, it looks like it tries to support Windows, maybe powershell acts different from cmd 🤷

@codesculpture
Copy link
Contributor Author

https://github.com/dart-lang/sdk/blob/main/sdk/lib/io/stdio.dart#L56 is the code in Dart, it looks like it tries to support Windows, maybe powershell acts different from cmd 🤷

Yes powershell is exceptional for everything🥲

@eseidel
Copy link
Contributor

eseidel commented Jan 11, 2024

Thanks for looking at this @codesculpture! That's a pretty small amount of code, which should be possible to lift into a stand-alone Dart file and debug. Let me know if you need any help getting set up to debug. We're reachable on various Flutter related Discords including https://discord.gg/shorebird

@codesculpture
Copy link
Contributor Author

Hmm, the enter being interpted as 10 in non-powershell and 13 in powershell which making this issue.

@codesculpture
Copy link
Contributor Author

codesculpture commented Jan 11, 2024

#include<windows.h>
#include<stdint.h>
#include<iostream>

bool ReadByte(int* byte) {
  HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
  uint8_t buffer[1];
  DWORD read = 0;
  BOOL success = ReadFile(h, buffer, 1, &read, nullptr);
  if (!success && (GetLastError() != ERROR_BROKEN_PIPE)) {
    return false;
  }
  *byte = (read == 1) ? buffer[0] : -1;
  return true;
}

int main() {
        int a;
        ReadByte(&a);
        std::cout << a;
}

After Executing this, pressing enter in powershell would print
13 but
10 in
git bash and command prompt(which is right?, i believe)

(This is the reproduction of reading stdio as per the implementation of readByte which is used in dart side https://github.com/dart-lang/sdk/blob/main/runtime/bin/stdio_win.cc#L23-L33 )

Edit: seems command prompt also works similarly to powershell, had a confusion here. Since there were new "cmd prompt" in windows 11 which i believe created using console mode whereas old one is not i guess.

@codesculpture
Copy link
Contributor Author

https://github.com/dart-lang/sdk/blob/main/sdk/lib/io/stdio.dart#L77-L87

I think this is what supposed to get executed but crNewLine is evaluated to false.

Is crNewLine to detect carriage return with new line specifcally for powershell?

@srawlins srawlins added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io labels Jan 12, 2024
@codesculpture
Copy link
Contributor Author

codesculpture commented Jan 12, 2024

https://github.com/dart-lang/sdk/blob/main/runtime/bin/stdio_win.cc#L78

lineMode needs to be false on poweshell? Right now, its being true i guess.

Sure lineMode is being true in powershell

@codesculpture
Copy link
Contributor Author

codesculpture commented Jan 12, 2024

When a console is created, all input modes except ENABLE_WINDOW_INPUT and ENABLE_VIRTUAL_TERMINAL_INPUT are enabled by default.

From https://learn.microsoft.com/en-us/windows/console/getconsolemode

This makes always ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT to be true. But we both needed to be disabled (atleast ENABLE_LINE_INPUT).

I think we don't need to bother about lineMode since i think it is not necessarily to check while doing readLineSync while the stdioType is Terminal ?

@a-siva
Copy link
Contributor

a-siva commented Jan 13, 2024

//cc @brianquinlan

@codesculpture
Copy link
Contributor Author

codesculpture commented Jan 13, 2024

The name itself readLineSync referring to reading a single line not multi line, so we need to return the data when switched to new line (receiving carrier Return or Line return) regardless of lineMode

@codesculpture
Copy link
Contributor Author

@brianquinlan 🙂 just reminding you to take a look whenever u have spare time.

@codesculpture
Copy link
Contributor Author

If ENABLE_LINE_INPUT is disabled then ENABLE_ECHO_INPUT must disabled, but when ENABLE_ECHO_INPUT is disabled, user cant see what they typed, usually it used for password entering purposes. So considering these, why we need to expect lineMode == false to determine crIsNewLine🤔

@mraleph
Copy link
Member

mraleph commented Jan 22, 2024

I was answering @codesculpture question on Discord which made me a bit curious about this problem. I could not reproduce this problem on my Windows 10 box even with newest Powershell. Eventually I have discovered that it only reproduces with Windows Terminal application, which I did not have installed.

If you play with reading byte-by-byte from stdin in C under Windows Terminal you will immediately notice that it is utterly broken: sometimes you press enter and the reader gets CR LF, sometimes you press enter and the reader only gets CR and LF is mysteriously dropped.

Which lead me to discover microsoft/terminal#16223 - so this is a bug in Windows Terminal and not in Dart.

@mraleph mraleph closed this as completed Jan 22, 2024
@codesculpture
Copy link
Contributor Author

This is no longer an issue (tested in windows terminal v1.18.2) 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants