Skip to content

Commit

Permalink
Fix redirected input (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Nov 23, 2023
1 parent 72e07fa commit 954e773
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ChangeLog.md
Expand Up @@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix analyzer [RCS1196](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1196) ([PR](https://github.com/dotnet/roslynator/pull/1235))
- Fix analyzer [RCS1257](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1257) ([PR](https://github.com/dotnet/roslynator/pull/1264))
- Fix analyzer [RCS1259](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1259) ([PR](https://github.com/dotnet/roslynator/pull/1268))
- [CLI] Fix reading of non-existent redirected input on git bash ([PR](https://github.com/dotnet/roslynator/pull/1265))
- [CLI] Fix reading of non-existent redirected input on git bash ([PR](https://github.com/dotnet/roslynator/pull/1265), [PR](https://github.com/dotnet/roslynator/pull/1274))
- [CLI] Fix exit code for `roslynator --version` ([PR](https://github.com/dotnet/roslynator/pull/1273))

## [4.6.2] - 2023-11-10
Expand Down
13 changes: 11 additions & 2 deletions src/CommandLine/ConsoleHelpers.cs
Expand Up @@ -9,7 +9,7 @@ namespace Roslynator.CommandLine;

internal static class ConsoleHelpers
{
public static async Task<ImmutableArray<string>> ReadRedirectedInputAsLines()
public static ImmutableArray<string> ReadRedirectedInputAsLines()
{
if (Console.IsInputRedirected)
{
Expand All @@ -18,9 +18,18 @@ public static async Task<ImmutableArray<string>> ReadRedirectedInputAsLines()
using (Stream stream = Console.OpenStandardInput())
using (var streamReader = new StreamReader(stream, Console.InputEncoding))
{
Task<string> readLineTask = streamReader.ReadLineAsync();

// https://github.com/dotnet/runtime/issues/95079
if (!readLineTask.Wait(TimeSpan.FromMilliseconds(500)))
return default;

if (readLineTask.Result is null)
return ImmutableArray<string>.Empty;

string line;

while ((line = await streamReader.ReadLineAsync()) is not null)
while ((line = streamReader.ReadLine()) is not null)
lines.Add(line);
}

Expand Down
17 changes: 8 additions & 9 deletions src/CommandLine/Program.cs
Expand Up @@ -842,14 +842,17 @@ private static bool TryParsePaths(IEnumerable<string> values, out ImmutableArray

if (Console.IsInputRedirected)
{
Task<ImmutableArray<string>> task = ConsoleHelpers.ReadRedirectedInputAsLines();

WriteLine("Reading redirected input...", Verbosity.Diagnostic);

// https://github.com/dotnet/runtime/issues/95079
if (task.Wait(TimeSpan.FromMilliseconds(500)))
ImmutableArray<string> lines = ConsoleHelpers.ReadRedirectedInputAsLines();

if (lines.IsDefault)
{
WriteLine("Unable to read redirected input", Verbosity.Diagnostic);
}
else
{
IEnumerable<string> paths1 = task.Result.Where(f => !string.IsNullOrEmpty(f));
IEnumerable<string> paths1 = lines.Where(f => !string.IsNullOrEmpty(f));

WriteLine("Successfully read redirected input:" + Environment.NewLine + " " + string.Join(Environment.NewLine + " ", paths1), Verbosity.Diagnostic);

Expand All @@ -858,10 +861,6 @@ private static bool TryParsePaths(IEnumerable<string> values, out ImmutableArray

paths = paths.AddRange(ImmutableArray.CreateRange(paths2, f => new PathInfo(f, PathOrigin.PipedInput)));
}
else
{
WriteLine("Unable to read redirected input", Verbosity.Diagnostic);
}
}

if (!paths.IsEmpty)
Expand Down

0 comments on commit 954e773

Please sign in to comment.