Skip to content

SuggestionStore::GetCompletions to check exit code of invoked applica… #2160

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
15 changes: 13 additions & 2 deletions src/System.CommandLine.Suggest/SuggestionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace System.CommandLine.Suggest
Expand Down Expand Up @@ -41,10 +42,20 @@ public string GetCompletions(string exeFileName, string suggestionTargetArgument
})
{
process.Start();

process.BeginOutputReadLine();

var stringBuilder = new StringBuilder();
process.OutputDataReceived += (sender, eventArgs) =>
{
if (eventArgs.Data != null)
{
stringBuilder.AppendLine(eventArgs.Data);
Copy link

@KalleOlaviNiemitalo KalleOlaviNiemitalo Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be just Append, not AppendLine.
There may be a thread safety problem if there is still more data coming in during the stringBuilder.ToString() call; although the child process has exited, grandchild processes might still write to the pipe.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, might be the mistake is here! I hope it is my last. Otherwise I am completely at loss right now on why the test keeps faling.
Regarding Append/AppendLine: seems not the case here - I have tested it, the eventArgs.Data comes without any line terminator.

}
};

if (process.WaitForExit(timeout) && process.ExitCode == 0)
{
result = process.StandardOutput.ReadToEnd();
result = stringBuilder.ToString();
}
else
{
Expand Down