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

Fix reading of redirected input #1265

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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))

## [4.6.2] - 2023-11-10

Expand Down
15 changes: 11 additions & 4 deletions src/CommandLine/ConsoleHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading.Tasks;

namespace Roslynator.CommandLine;

internal static class ConsoleHelpers
{
public static IEnumerable<string> ReadRedirectedInputAsLines()
public static async Task<ImmutableArray<string>> ReadRedirectedInputAsLines()
{
if (Console.IsInputRedirected)
{
ImmutableArray<string>.Builder lines = ImmutableArray.CreateBuilder<string>();

using (Stream stream = Console.OpenStandardInput())
using (var streamReader = new StreamReader(stream, Console.InputEncoding))
{
string line;

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

return lines.ToImmutableArray();
}

return ImmutableArray<string>.Empty;
}
}
24 changes: 18 additions & 6 deletions src/CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,14 +790,26 @@ private static bool TryParsePaths(IEnumerable<string> values, out ImmutableArray

if (Console.IsInputRedirected)
{
if (!TryEnsureFullPath(
ConsoleHelpers.ReadRedirectedInputAsLines().Where(f => !string.IsNullOrEmpty(f)),
out ImmutableArray<string> paths2))
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)))
{
return false;
}
IEnumerable<string> paths1 = task.Result.Where(f => !string.IsNullOrEmpty(f));

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

paths = paths.AddRange(ImmutableArray.CreateRange(paths2, f => new PathInfo(f, PathOrigin.PipedInput)));
if (!TryEnsureFullPath(paths1, out ImmutableArray<string> paths2))
return false;

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