-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
console redirection is broken #22314
Comments
ugh, please forget the 'stdin redirection' part. |
Triage:
|
To add some more details: For stdout redirection, not only is the input not displayed, the output isn't fine either - it contains the user input + lines written by Console.WriteLine(Console.ReadLine()); If the user enters "abc" when prompted, it'd behave like this
More context on this issue's impact: We're rewriting a CLI app from Go to C#, and it's a requirement that user input isn't written to stdout, because one main use case for this app is that user will run it inside |
In case this issue won't be fixed soon, any comment on this potential workaround on Linux & Mac? var stdinReader= new StreamReader(new FileStream(
new Microsoft.Win32.SafeHandles.SafeFileHandle(0, ownsHandle: false),
FileAccess.Read
));
stdinReader.ReadLine(); Should |
|
.NET incorrectly copies user's console input into stdout on Unix-y platforms, which messes up `bmx print`, so we have to work around it. See dotnet/runtime#22314
I might have an idea of what's going on with user terminal input when stdout is redirected. When
then read input byte-by-byte, and prints the characters back out using Console.Write :
This of course means that user input will end up in stdout (redirected or not) and not on the terminal (when stdout is redirected). It's a similar process for other read methods like Might be a naive suggestion, but can the runtime prints user input back out to /dev/tty instead of stdout to resolve the first part of this issue? |
Just noticed that the "user input ending up in stdout" problem can occur on Windows too, though to a much smaller extent - only when runtime/src/libraries/System.Console/src/System/ConsolePal.Windows.cs Lines 397 to 398 in 9f8f653
|
That's right. These writes are are meant to emulate a terminal ECHO. When the output is redirected, we shouldn't write these characters. @cfbao do you want to make a PR that implements that change? |
To confirm - When stdout is redirected, I believe users would still expect whatever they type on the terminal to appear on screen (just not end up in stdout), so do we want to write their input to
I'm interested, but will probably need some handholding 😅 |
Yes, that is desired.
If you want to have a go at it, I'll assist you. The documentation here explains how you can build and test on your machine. |
@cfbao would be much appreciated to get your help 😄 |
@cfbao feel free to pick this up, and I'll provide guidance. |
I've made a PR that addresses the stdout redirection issue: #94414. The stdin redirection issue is not a real issue. The problem is that the |
From @Spongman on June 15, 2017 21:5
stdout redirection:
test command:
dotnet run > test.txt
expected behavior is to show typed text in the console/tty, and to write the typed lines out to
test.txt
on windows: works fine.
on linux: writes stdin to the file ok, but the typed text is not echoed to tty.
stdin redirection:
test command:
dotnet run < test.txt
(test.txt exists and contains some lines of text)expected behavior is to write the contents of
test.txt
to the console/tty.on both windows & linux: console clears and the CPU is pegged!!
version: 1.0.4
Copied from original issue: dotnet/coreclr#12308
The text was updated successfully, but these errors were encountered: