Skip to content

Commit

Permalink
FIX: StdOut filled buffer blocking call
Browse files Browse the repository at this point in the history
  • Loading branch information
Omnicrash authored and xaviersolau committed Jun 27, 2024
1 parent 41b2a43 commit 0073821
Showing 1 changed file with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using Microsoft.CodeAnalysis;
using SoloX.GeneratorTools.Core.CSharp.Model;
using SoloX.GeneratorTools.Core.CSharp.Utils;
Expand Down Expand Up @@ -202,28 +204,65 @@ private string RunTarget(string target, Dictionary<string, string> properties)
}
}

var processStartInfo = new ProcessStartInfo(DotNet, $"msbuild -t:{target} {propertiesArgs} -nologo")
using (var process = new Process())
{
WorkingDirectory = this.ProjectPath,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
using (var process = Process.Start(processStartInfo))
{
process.WaitForExit();
var stdOutput = process.StandardOutput.ReadToEnd();
if (process.ExitCode != 0)
var output = new StringBuilder();
var error = new StringBuilder();

using (var outputWaitHandle = new AutoResetEvent(false))
using (var errorWaitHandle = new AutoResetEvent(false))
{
var rawError = process.StandardError.ReadToEnd();
throw new FormatException(
$"Unable to load project file: dotnet exit code is {process.ExitCode}\n" +
$"Standard output:\n" +
$"{stdOutput})\n" +
$"Standard error:\n" +
$"{rawError})");
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};

process.StartInfo.FileName = DotNet;
process.StartInfo.Arguments = $"msbuild -t:{target} {propertiesArgs} -nologo";
process.StartInfo.WorkingDirectory = this.ProjectPath;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();

process.WaitForExit();
outputWaitHandle.WaitOne();
errorWaitHandle.WaitOne();

var stdOutput = output.ToString();
if (process.ExitCode != 0)
{
var rawError = error.ToString();
throw new FormatException(
$"Unable to load project file: dotnet exit code is {process.ExitCode}\n" +
$"Standard output:\n" +
$"{stdOutput})\n" +
$"Standard error:\n" +
$"{rawError})");
}

return stdOutput;
}

return stdOutput;
}
}

Expand Down

0 comments on commit 0073821

Please sign in to comment.