From 240f3ed54eed48b014900c9eb98ad3109bab8bce Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Thu, 8 Aug 2024 14:43:55 +0100 Subject: [PATCH] Fix std out/err in InvokeProcess --- .../Exceptions/ProcessFailedException.cs | 8 ++++---- src/vpk/Velopack.Packaging/Exe.cs | 13 ++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/vpk/Velopack.Packaging/Exceptions/ProcessFailedException.cs b/src/vpk/Velopack.Packaging/Exceptions/ProcessFailedException.cs index 61daf02a..f903802b 100644 --- a/src/vpk/Velopack.Packaging/Exceptions/ProcessFailedException.cs +++ b/src/vpk/Velopack.Packaging/Exceptions/ProcessFailedException.cs @@ -8,16 +8,16 @@ public class ProcessFailedException : Exception public string Command { get; } public string StdOutput { get; } - public ProcessFailedException(string command, string stdOutput) - : base($"Process failed: '{command}'{Environment.NewLine}Output was -{Environment.NewLine}{stdOutput}") + public ProcessFailedException(string command, string stdOutput, string stdErr) + : base($"Process failed: '{command}'{Environment.NewLine}Output was -{Environment.NewLine}{stdOutput}{Environment.NewLine}StdErr was -{Environment.NewLine}{stdErr}") { Command = command; StdOutput = stdOutput; } - public static void ThrowIfNonZero((int ExitCode, string StdOutput, string Command) result) + public static void ThrowIfNonZero((int ExitCode, string StdOutput, string StdErr, string Command) result) { if (result.ExitCode != 0) - throw new ProcessFailedException(result.Command, result.StdOutput); + throw new ProcessFailedException(result.Command, result.StdOutput, result.StdErr); } } diff --git a/src/vpk/Velopack.Packaging/Exe.cs b/src/vpk/Velopack.Packaging/Exe.cs index 8cbc7e26..c4e881f5 100644 --- a/src/vpk/Velopack.Packaging/Exe.cs +++ b/src/vpk/Velopack.Packaging/Exe.cs @@ -12,7 +12,7 @@ public static class Exe if (VelopackRuntimeInfo.IsWindows) { var output = InvokeAndThrowIfNonZero("where", new[] { binaryName }, null); if (String.IsNullOrWhiteSpace(output) || !File.Exists(output)) - throw new ProcessFailedException("", ""); + throw new ProcessFailedException("", "", ""); } else if (VelopackRuntimeInfo.IsOSX) { InvokeAndThrowIfNonZero("command", new[] { "-v", binaryName }, null); } else if (VelopackRuntimeInfo.IsLinux) { @@ -65,7 +65,7 @@ public static class Exe process.WaitForExit(); var stdout = Utility.Retry(() => File.ReadAllText(outputFile).Trim(), 10, 1000); - var result = (process.ExitCode, stdout, command); + var result = (process.ExitCode, stdout, "", command); ProcessFailedException.ThrowIfNonZero(result); return result.Item2; } @@ -103,7 +103,7 @@ public static class Exe return result.StdOutput; } - public static (int ExitCode, string StdOutput) InvokeProcess(ProcessStartInfo psi, CancellationToken ct) + public static (int ExitCode, string StdOutput, string StdErr) InvokeProcess(ProcessStartInfo psi, CancellationToken ct) { var process = new Process(); process.StartInfo = psi; @@ -134,11 +134,10 @@ public static class Exe ct.ThrowIfCancellationRequested(); } - var all = (sOut.ToString().Trim()) + Environment.NewLine + (sOut.ToString().Trim()); - return (process.ExitCode, all.Trim()); + return (process.ExitCode, sOut.ToString().Trim(), sErr.ToString().Trim()); } - public static (int ExitCode, string StdOutput, string Command) InvokeProcess(string fileName, IEnumerable args, string workingDirectory, CancellationToken ct = default, + public static (int ExitCode, string StdOutput, string StdErr, string Command) InvokeProcess(string fileName, IEnumerable args, string workingDirectory, CancellationToken ct = default, IDictionary envVar = null) { var psi = CreateProcessStartInfo(fileName, workingDirectory); @@ -150,7 +149,7 @@ public static class Exe psi.AppendArgumentListSafe(args, out var argString); var p = InvokeProcess(psi, ct); - return (p.ExitCode, p.StdOutput, $"{fileName} {argString}"); + return (p.ExitCode, p.StdOutput, p.StdErr, $"{fileName} {argString}"); } public static ProcessStartInfo CreateProcessStartInfo(string fileName, string workingDirectory)