dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.24k stars 4.72k forks source link

Console.IsInputRedirected returns true on git bash when there is no redirected input #95079

Open josefpihrt opened 11 months ago

josefpihrt commented 11 months ago

Description

Hi,

I have command-line tool (roslynator) that tries to read redirected input and use it as an input for the program (in my case it's a list of solutions to be analyzed).

Example code:

get-solution-paths | roslynator analyze

For this purpose I use Console.IsInputRedirected which works fine on Windows Terminal with Powershell.

The problem is that when I run just roslynator analyze on git bash (git-bash.exe) Console.IsInputRedirected returns true and then the execution is blocked when program tries to read input from console (when there is no redirected input):

https://github.com/dotnet/roslynator/blob/e030192b1dc5359df48e5dd15af79b5c807f481c/src/CommandLine/ConsoleHelpers.cs#L20

Thanks for help in advance.

Reproduction Steps

  1. dotnet tool install roslynator.dotnet.cli -g --version 0.7.2
  2. run git-bash.exe from git installation folder
  3. from git bash run roslynator analyze <PATH-TO-SOLUTION> -v diag
  4. The execution is blocked when program tries to read redirected input

Expected behavior

Console.IsInputRedirected should return false when there is no redirected input

Actual behavior

Console.IsInputRedirected returns true on git bash when there is no redirected input.

Regression?

No response

Known Workarounds

Set timeout for a task that tries to read redirected input:

Task<IEnumerable<string>> task = ConsoleHelpers.ReadRedirectedInputAsLines();

if (task.Wait(TimeSpan.FromMilliseconds(1000)))
{
}

Configuration

Other information

No response

ghost commented 11 months ago

Tagging subscribers to this area: @dotnet/area-system-console See info in area-owners.md if you want to be subscribed.

Issue Details
### Description Hi, I have command-line tool (roslynator) that tries to read redirected input and use it as an input for the program (in my case it's a list of solutions to be analyzed). Example code: ``` get-solution-paths | roslynator analyze ``` For this purpose I use `Console.IsInputRedirected` which works fine on Windows Terminal with Powershell. The problem is that when I run just `roslynator analyze` on git bash (`git-bash.exe`) `Console.IsInputRedirected` returns true and then the execution is blocked when program tries to read input from console (when there is no redirected input): https://github.com/dotnet/roslynator/blob/e030192b1dc5359df48e5dd15af79b5c807f481c/src/CommandLine/ConsoleHelpers.cs#L20 Thanks for help in advance. ### Reproduction Steps 1. `dotnet tool install roslynator.dotnet.cli -g --version 0.7.2` 1. run `git-bash.exe` from git installation folder 1. from git bash run `roslynator analyze -v diag` 1. The execution is blocked when program tries to read redirected input ### Expected behavior Console.IsInputRedirected should return false when there is no redirected input ### Actual behavior Console.IsInputRedirected returns true on git bash when there is no redirected input. ### Regression? _No response_ ### Known Workarounds Set timeout for a task that tries to read redirected input: ```csharp Task> task = ConsoleHelpers.ReadRedirectedInputAsLines(); if (task.Wait(TimeSpan.FromMilliseconds(1000))) { } ``` ### Configuration - Windows 10 Pro x64 - .NET SDK 8.0.100 - git 2.42.0.windows.2 ### Other information _No response_
Author: josefpihrt
Assignees: -
Labels: `area-System.Console`
Milestone: -
adamsitnik commented 11 months ago

I've tried to reproduce the problem and failed to do so.

image

namespace ConsoleInputRedirected
{
    internal class Program
    {
        static void Main() => Console.WriteLine($"Console.IsInputRedirected={Console.IsInputRedirected}");
    }
}

image

adamsitnik commented 11 months ago

image

ghost commented 11 months ago

This issue has been marked needs-author-action and may be missing some important information.

josefpihrt commented 11 months ago

Thank you for your answer @adamsitnik.

I used your console app and run it in git-bash with following result:

image

I have the latest version of git (2.43.0) so the only difference between yours and mine configuration could be the operation system. I have Windows 10 Pro 10.0.19045.

adamsitnik commented 11 months ago

@josefpihrt Is there any chance you could debug it? You could add System.Diagnostics.Debugger.Launch(); and step into framework code with F11.

I am mostly interested in what happens here:

https://github.com/dotnet/runtime/blob/721c445e494a1d85d2d12e7a99ac2cd94bb898fa/src/libraries/System.Console/src/System/ConsolePal.Windows.cs#L147-L156

josefpihrt commented 11 months ago

I was able to debug the process, here are the results:

Let me know if you need more data.

josefpihrt commented 10 months ago

@adamsitnik ping

KalleOlaviNiemitalo commented 1 month ago

The behavior might depend on whether Git Bash is using the Windows pseudoconsole feature (MSYS=enable_pcon in git-bash.config).