From 28961b672ee6c49056786e60a356d556921c86c2 Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Wed, 13 Mar 2024 00:24:38 +0100 Subject: [PATCH] (#11747) GlobalToolShim: fix argument passing The previous approach was losing the quotes around the arguments, and could easily mess up the arguments with spaces or embedded quotes. The new approach is more robust since it relies on .NET Process API properly re-packing the arguments to be passed to a child process. --- src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs b/src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs index c8e95628687..e75fa283d95 100644 --- a/src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs +++ b/src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs @@ -30,9 +30,10 @@ public static int Main(string[] args) string platformFolder = isWindows ? WinFolderName : UnixFolderName; - string argsString = args.Length > 0 ? string.Join(" ", args) : null; + var arguments = new List(args.Length + 1); var pwshPath = Path.Combine(currentPath, platformFolder, PwshDllName); - string processArgs = string.IsNullOrEmpty(argsString) ? $"\"{pwshPath}\"" : $"\"{pwshPath}\" {argsString}"; + arguments.Add(pwshPath); + arguments.AddRange(args); if (File.Exists(pwshPath)) { @@ -41,7 +42,7 @@ public static int Main(string[] args) e.Cancel = true; }; - var process = System.Diagnostics.Process.Start("dotnet", processArgs); + var process = System.Diagnostics.Process.Start("dotnet", arguments); process.WaitForExit(); return process.ExitCode; }